4 |
* |
* |
5 |
* Uebung 4.4 |
* Uebung 4.4 |
6 |
*/ |
*/ |
7 |
|
|
8 |
|
// $Id$ |
9 |
|
|
10 |
|
#include <windows.h> |
11 |
#include <stdio.h> |
#include <stdio.h> |
12 |
#include <errno.h> |
#include <errno.h> |
13 |
|
|
15 |
#define YSIZE 500 |
#define YSIZE 500 |
16 |
#include "algorithm.h" |
#include "algorithm.h" |
17 |
|
|
18 |
|
|
19 |
/* BMP Header */ |
/* BMP Header */ |
20 |
unsigned char header[54]={0x42,0x4d, // signature BM |
unsigned char header[54]={0x42,0x4d, // signature BM |
21 |
0xe6,0x71,0x0b,0x0, // filesize 750054 |
0xe6,0x71,0x0b,0x0, // filesize 750054 |
34 |
0x0,0x0,0x0,0x0 // number of important colors |
0x0,0x0,0x0,0x0 // number of important colors |
35 |
}; |
}; |
36 |
|
|
|
int main(int argc, char *argv[]) |
|
|
{ |
|
|
FILE *fd; |
|
|
int len,x,y; |
|
|
char bgr[3]; |
|
|
short svalue; |
|
|
int lvalue; |
|
|
//unsigned char header[54],*ptr=&header[0]; |
|
|
|
|
|
fd=fopen("test.bmp","wb+"); |
|
|
if(NULL==fd) |
|
|
{ |
|
|
perror("open"); exit(1); |
|
|
} |
|
37 |
|
|
38 |
len=fwrite(header,1,sizeof(header),fd); //write header |
void printErrorAndExit(const char *msg, DWORD err) { |
39 |
|
LPSTR lpMsgBuf; |
40 |
if(-1==len || len!=sizeof(header)) |
if(FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER| |
41 |
{ |
FORMAT_MESSAGE_FROM_SYSTEM | |
42 |
perror("write"); |
FORMAT_MESSAGE_IGNORE_INSERTS, |
43 |
exit(2); |
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 |
|
|
99 |
|
// MMF support |
100 |
|
DWORD err; |
101 |
|
HANDLE hMap, hFile; |
102 |
|
LPVOID pData; |
103 |
|
unsigned char *pDataBitmap, *pDataBitmapCurrent; |
104 |
|
|
105 |
|
// workers |
106 |
|
unsigned int workers = 3; |
107 |
|
unsigned int worker_index, worker_rows, worker_startrow; |
108 |
|
|
109 |
|
// fractal calculation |
110 |
|
int x, y; |
111 |
|
char bgr[3]; |
112 |
|
|
113 |
|
|
114 |
|
// create empty bmp-file (black background) |
115 |
|
write_blank_file("test.bmp"); |
116 |
|
|
117 |
|
/* open file for reading and writing */ |
118 |
|
hFile = CreateFile("test.bmp", GENERIC_WRITE|GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
119 |
|
if (INVALID_HANDLE_VALUE == hFile) { |
120 |
|
err = GetLastError(); |
121 |
|
printErrorAndExit("Error at CreateFile",err); |
122 |
|
} |
123 |
|
|
124 |
|
/* create the file mapping object */ |
125 |
|
hMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, NULL); |
126 |
|
if (NULL == hMap) { |
127 |
|
printErrorAndExit("Error at CreateFileMapping", GetLastError()); |
128 |
|
} |
129 |
|
|
130 |
|
/* map the whole file into the process context */ |
131 |
|
pData = MapViewOfFile(hMap, FILE_MAP_WRITE, 0, 0, 0); |
132 |
|
if (NULL == pData) { |
133 |
|
printErrorAndExit("Error at MapViewOfFile", GetLastError()); |
134 |
|
} |
135 |
|
|
136 |
|
|
137 |
|
// calculate pointer to beginning of bitmap |
138 |
|
pDataBitmap = (unsigned char *)((INT_PTR)pData + sizeof(header)); |
139 |
|
|
140 |
|
// debugging |
141 |
|
printf("pos. of file: %p\n", pData); |
142 |
|
printf("pos. of bitmap: %p\n", pDataBitmap); |
143 |
|
|
144 |
|
// pointer to current pixel |
145 |
|
pDataBitmapCurrent = pDataBitmap; |
146 |
|
|
147 |
|
/* |
148 |
|
// turn bitmap into white wand |
149 |
|
for (offset = 0; offset < 500 * 500 * 3; offset++) { |
150 |
|
//pDataBitmap[offset] = 255; |
151 |
|
*pDataBitmapCurrent = 255; |
152 |
|
pDataBitmapCurrent++; |
153 |
|
} |
154 |
|
*/ |
155 |
|
|
156 |
|
// calculate segments of bitmap for worker threads/processes |
157 |
|
worker_rows = YSIZE / workers; |
158 |
|
printf("rows for each worker: %i\n", worker_rows); |
159 |
|
for (worker_index = 0; worker_index < workers; worker_index++) { |
160 |
|
|
161 |
for(y=YSIZE-1;y>=0;y--) |
// number of row to start for each worker |
162 |
{ |
worker_startrow = worker_index * worker_rows; |
|
for(x=0;x<XSIZE;x++) |
|
|
{ |
|
|
getColorValuesAt(x * (2.0 / XSIZE) - 1.5, y * (2.0 / YSIZE) - 1.0,&bgr[2],&bgr[1],&bgr[0]); |
|
|
|
|
|
len=fwrite(bgr,1,3,fd); |
|
|
if(-1==len || len!=3) |
|
|
{ |
|
|
perror("write"); |
|
|
exit(4); |
|
|
} |
|
|
} |
|
|
/*no padding required because 1500%4 =0*/ |
|
|
} |
|
|
fclose(fd); |
|
163 |
|
|
164 |
|
// recalculate number of rows for last worker if (YSIZE mod workers) != 0 |
165 |
|
if (worker_index == workers - 1) { |
166 |
|
worker_rows = YSIZE - worker_startrow; |
167 |
|
printf("rows for last worker: %i\n", worker_rows); |
168 |
|
} |
169 |
|
} |
170 |
|
|
171 |
|
// calculate fractal |
172 |
|
for (y=YSIZE-1; y>=0; y--) { |
173 |
|
for (x=0; x<XSIZE; x++) { |
174 |
|
getColorValuesAt(x * (2.0 / XSIZE) - 1.5, y * (2.0 / YSIZE) - 1.0, &bgr[2], &bgr[1], &bgr[0]); |
175 |
|
|
176 |
|
// debugging |
177 |
|
//printf("pointer: %p\n", pDataBitmapCurrent); |
178 |
|
|
179 |
|
// transfer color values to current pixel |
180 |
|
pDataBitmapCurrent[0] = bgr[0]; |
181 |
|
pDataBitmapCurrent[1] = bgr[1]; |
182 |
|
pDataBitmapCurrent[2] = bgr[2]; |
183 |
|
|
184 |
|
// move pointer to next pixel |
185 |
|
pDataBitmapCurrent += 3; |
186 |
|
|
187 |
|
} |
188 |
|
//no padding required because 1500%4 =0 |
189 |
|
} |
190 |
|
|
191 |
|
|
192 |
|
/* write the result into the file */ |
193 |
|
if (!FlushViewOfFile(pData, 0)) { |
194 |
|
err = GetLastError(); |
195 |
|
printErrorAndExit("Error at UnmapViewOfFile", err); |
196 |
|
} |
197 |
|
|
198 |
|
/* remove the mapped file */ |
199 |
|
if (!UnmapViewOfFile(pData)) { |
200 |
|
err = GetLastError(); |
201 |
|
printErrorAndExit("Error at UnmapViewOfFile", err); |
202 |
|
exit(err); |
203 |
|
} |
204 |
|
|
205 |
|
/* cleanup handles */ |
206 |
|
if (!CloseHandle(hMap) || !CloseHandle(hFile) ) { |
207 |
|
err = GetLastError(); |
208 |
|
printErrorAndExit("Error at CloseHandle", err); |
209 |
|
} |
210 |
|
|
211 |
} |
} |