1 |
/* Betriebssystem & Middleware |
2 |
* |
3 |
* Betriebssystemarchitektur SS 2006 |
4 |
* |
5 |
* Uebung 4.4 |
6 |
*/ |
7 |
|
8 |
// $Id$ |
9 |
|
10 |
#include <windows.h> |
11 |
#include <stdio.h> |
12 |
#include <errno.h> |
13 |
|
14 |
#define XSIZE 500 |
15 |
#define YSIZE 500 |
16 |
#include "algorithm.h" |
17 |
|
18 |
|
19 |
/* BMP Header */ |
20 |
unsigned char header[54]={0x42,0x4d, // signature BM |
21 |
0xe6,0x71,0x0b,0x0, // filesize 750054 |
22 |
0x0,0x0,0x0,0x0, // reserved |
23 |
0x36,0x0,0x0,0x0, // image offset 54 |
24 |
0x28,0x0,0x0,0x0, // size of header follows 40 |
25 |
0xf4,0x1,0x0,0x0, // with of image 500 |
26 |
0xf4,0x1,0x0,0x0, // height of image 500 |
27 |
0x1,0x0, // number of planes 1 |
28 |
0x18,0x0, // number of pixel 24 |
29 |
0x0,0x0,0x0,0x0, // compression |
30 |
0xb0,0x71,0x0b,0x0, // size of image 750000 |
31 |
0x0,0x0,0x0,0x0, // xres |
32 |
0x0,0x0,0x0,0x0, // yres |
33 |
0x0,0x0,0x0,0x0, // number of colortables |
34 |
0x0,0x0,0x0,0x0 // number of important colors |
35 |
}; |
36 |
|
37 |
|
38 |
void printErrorAndExit(const char *msg, DWORD err) { |
39 |
LPSTR lpMsgBuf; |
40 |
if(FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER| |
41 |
FORMAT_MESSAGE_FROM_SYSTEM | |
42 |
FORMAT_MESSAGE_IGNORE_INSERTS, |
43 |
NULL, |
44 |
err, |
45 |
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language |
46 |
(LPTSTR) &lpMsgBuf, |
47 |
0, |
48 |
NULL )) |
49 |
{ |
50 |
fprintf(stderr,"%s : %s\n",msg,lpMsgBuf); |
51 |
LocalFree(lpMsgBuf); |
52 |
} |
53 |
else |
54 |
{ |
55 |
fprintf(stderr,"Error at FormatMesage: %d\n",err=GetLastError()); |
56 |
} |
57 |
exit(err); |
58 |
} |
59 |
|
60 |
|
61 |
void write_blank_file(char *filename) { |
62 |
|
63 |
FILE *fd; |
64 |
int len, i, img_size; |
65 |
|
66 |
// open file handle |
67 |
fd = fopen(filename, "wb+"); |
68 |
if (NULL == fd) { |
69 |
perror("open"); |
70 |
exit(1); |
71 |
} |
72 |
|
73 |
// write bmp header to file |
74 |
len = fwrite(header, 1, sizeof(header), fd); |
75 |
|
76 |
// error checking |
77 |
if (-1 == len || len != sizeof(header)) { |
78 |
perror("write"); |
79 |
exit(2); |
80 |
} |
81 |
|
82 |
// write three null-bytes for each pixel to file to create a black picture |
83 |
img_size = XSIZE * YSIZE; |
84 |
for (i = 0; i < img_size; i++) { |
85 |
len = fwrite("\0\0\0", 1, 3, fd); |
86 |
if (-1 == len || len != 3) { |
87 |
perror("write"); |
88 |
exit(4); |
89 |
} |
90 |
} |
91 |
|
92 |
// close file handle |
93 |
fclose(fd); |
94 |
} |
95 |
|
96 |
|
97 |
int main(int argc, char *argv[]) { |
98 |
int len,x,y; |
99 |
char bgr[3]; |
100 |
short svalue; |
101 |
int lvalue; |
102 |
|
103 |
HANDLE hMap, hFile; |
104 |
DWORD err, size; |
105 |
int elems; |
106 |
|
107 |
LPVOID pData; |
108 |
unsigned char *pDataBitmap, *pDataBitmapCurrent; |
109 |
|
110 |
int offset; |
111 |
|
112 |
// create empty file (black background) |
113 |
write_blank_file("test.bmp"); |
114 |
|
115 |
|
116 |
/* open file for reading and writing */ |
117 |
hFile = CreateFile("test.bmp", GENERIC_WRITE|GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
118 |
if (INVALID_HANDLE_VALUE == hFile) { |
119 |
err = GetLastError(); |
120 |
printErrorAndExit("Error at CreateFile",err); |
121 |
} |
122 |
|
123 |
/* create the file mapping object */ |
124 |
hMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, NULL); |
125 |
if (NULL == hMap) { |
126 |
printErrorAndExit("Error at CreateFileMapping", GetLastError()); |
127 |
} |
128 |
|
129 |
/* map the whole file into the process context */ |
130 |
pData = MapViewOfFile(hMap, FILE_MAP_WRITE, 0, 0, 0); |
131 |
if (NULL == pData) { |
132 |
printErrorAndExit("Error at MapViewOfFile", GetLastError()); |
133 |
} |
134 |
|
135 |
|
136 |
// calculate pointer to beginning of bitmap |
137 |
pDataBitmap = (unsigned char *)((INT_PTR)pData + sizeof(header)); |
138 |
|
139 |
// debugging |
140 |
printf("pos. of file: %p\n", pData); |
141 |
printf("pos. of bitmap: %p\n", pDataBitmap); |
142 |
|
143 |
// pointer to current pixel |
144 |
pDataBitmapCurrent = pDataBitmap; |
145 |
|
146 |
/* |
147 |
// turn bitmap into white wand |
148 |
for (offset = 0; offset < 500 * 500 * 3; offset++) { |
149 |
//pDataBitmap[offset] = 255; |
150 |
*pDataBitmapCurrent = 255; |
151 |
pDataBitmapCurrent++; |
152 |
} |
153 |
*/ |
154 |
|
155 |
// calculate fractal |
156 |
for (y=YSIZE-1; y>=0; y--) { |
157 |
for (x=0; x<XSIZE; x++) { |
158 |
getColorValuesAt(x * (2.0 / XSIZE) - 1.5, y * (2.0 / YSIZE) - 1.0, &bgr[2], &bgr[1], &bgr[0]); |
159 |
|
160 |
// debugging |
161 |
//printf("pointer: %p\n", pDataBitmapCurrent); |
162 |
|
163 |
// transfer color values to current pixel |
164 |
//*pDataBitmapCurrent = (unsigned char *)bgr; |
165 |
pDataBitmapCurrent[0] = bgr[0]; |
166 |
pDataBitmapCurrent[1] = bgr[1]; |
167 |
pDataBitmapCurrent[2] = bgr[2]; |
168 |
|
169 |
// move pointer to next pixel |
170 |
pDataBitmapCurrent += 3; |
171 |
|
172 |
} |
173 |
//no padding required because 1500%4 =0 |
174 |
} |
175 |
|
176 |
|
177 |
/* write the result into the file */ |
178 |
if (!FlushViewOfFile(pData, 0)) { |
179 |
err = GetLastError(); |
180 |
printErrorAndExit("Error at UnmapViewOfFile", err); |
181 |
} |
182 |
|
183 |
/* remove the mapped file */ |
184 |
if (!UnmapViewOfFile(pData)) { |
185 |
err = GetLastError(); |
186 |
printErrorAndExit("Error at UnmapViewOfFile", err); |
187 |
exit(err); |
188 |
} |
189 |
|
190 |
/* cleanup handles */ |
191 |
if (!CloseHandle(hMap) || !CloseHandle(hFile) ) { |
192 |
err = GetLastError(); |
193 |
printErrorAndExit("Error at CloseHandle", err); |
194 |
} |
195 |
|
196 |
} |