/[cvs]/joko/Uni/BSArch/04/bmp_fractal.c
ViewVC logotype

Annotation of /joko/Uni/BSArch/04/bmp_fractal.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.9 - (hide annotations)
Sun Jul 2 00:24:17 2006 UTC (18 years, 2 months ago) by joko
Branch: MAIN
Changes since 1.8: +44 -46 lines
File MIME type: text/plain
cleanup

1 joko 1.1 /* Betriebssystem & Middleware
2     *
3     * Betriebssystemarchitektur SS 2006
4     *
5     * Uebung 4.4
6     */
7 joko 1.3
8 joko 1.9 // $Id: bmp_fractal.c,v 1.8 2006/07/01 22:10:42 joko Exp $
9 joko 1.3
10     #include <windows.h>
11 joko 1.1 #include <stdio.h>
12     #include <errno.h>
13    
14     #define XSIZE 500
15     #define YSIZE 500
16     #include "algorithm.h"
17    
18 joko 1.9 BOOL VERBOSE = FALSE;
19    
20 joko 1.3
21 joko 1.2 /* BMP Header */
22 joko 1.1 unsigned char header[54]={0x42,0x4d, // signature BM
23     0xe6,0x71,0x0b,0x0, // filesize 750054
24     0x0,0x0,0x0,0x0, // reserved
25     0x36,0x0,0x0,0x0, // image offset 54
26     0x28,0x0,0x0,0x0, // size of header follows 40
27     0xf4,0x1,0x0,0x0, // with of image 500
28     0xf4,0x1,0x0,0x0, // height of image 500
29     0x1,0x0, // number of planes 1
30     0x18,0x0, // number of pixel 24
31     0x0,0x0,0x0,0x0, // compression
32     0xb0,0x71,0x0b,0x0, // size of image 750000
33     0x0,0x0,0x0,0x0, // xres
34     0x0,0x0,0x0,0x0, // yres
35     0x0,0x0,0x0,0x0, // number of colortables
36     0x0,0x0,0x0,0x0 // number of important colors
37     };
38    
39 joko 1.3
40     void printErrorAndExit(const char *msg, DWORD err) {
41     LPSTR lpMsgBuf;
42     if(FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
43     FORMAT_MESSAGE_FROM_SYSTEM |
44     FORMAT_MESSAGE_IGNORE_INSERTS,
45     NULL,
46     err,
47     MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
48     (LPTSTR) &lpMsgBuf,
49     0,
50     NULL ))
51     {
52     fprintf(stderr,"%s : %s\n",msg,lpMsgBuf);
53     LocalFree(lpMsgBuf);
54     }
55     else
56     {
57     fprintf(stderr,"Error at FormatMesage: %d\n",err=GetLastError());
58     }
59     exit(err);
60     }
61    
62    
63     void write_blank_file(char *filename) {
64    
65     FILE *fd;
66     int len, i, img_size;
67    
68     // open file handle
69     fd = fopen(filename, "wb+");
70     if (NULL == fd) {
71     perror("open");
72     exit(1);
73     }
74    
75     // write bmp header to file
76     len = fwrite(header, 1, sizeof(header), fd);
77    
78     // error checking
79     if (-1 == len || len != sizeof(header)) {
80     perror("write");
81     exit(2);
82     }
83    
84     // write three null-bytes for each pixel to file to create a black picture
85     img_size = XSIZE * YSIZE;
86     for (i = 0; i < img_size; i++) {
87     len = fwrite("\0\0\0", 1, 3, fd);
88     if (-1 == len || len != 3) {
89     perror("write");
90     exit(4);
91 joko 1.1 }
92 joko 1.3 }
93    
94     // close file handle
95     fclose(fd);
96     }
97    
98    
99 joko 1.6 // arguments for each thread
100     typedef struct _WorkerArguments {
101 joko 1.7 int start_row;
102     int number_of_rows;
103 joko 1.6 unsigned char * pBitmap;
104     } WORKERARGS, *PWORKERARGS;
105    
106     // worker thread - main entry function
107     DWORD WINAPI fractal_thread (LPVOID lpParam) {
108    
109     // thread stuff
110     int thread_id;
111     PWORKERARGS args;
112 joko 1.7 unsigned char *pDataBitmapSegment;
113 joko 1.6
114     // fractal calculation
115     int x, y;
116     char bgr[3];
117    
118     thread_id = GetCurrentThreadId();
119    
120     // get worker arguments
121     args = (PWORKERARGS)lpParam;
122 joko 1.7
123     // calculate pointer to beginning of segment
124     pDataBitmapSegment = (unsigned char *)((INT_PTR)args->pBitmap + (YSIZE - (args->start_row + args->number_of_rows)) * 3 * XSIZE);
125 joko 1.9
126     // debugging
127     if (VERBOSE) {
128     printf("----------------------------------------------\n");
129     printf("thread_id: %i\n", thread_id);
130     printf("arg.start_row: %i\n", args->start_row);
131     printf("arg.number_of_rows: %i\n", args->number_of_rows);
132     printf("segment_start: %p\n", pDataBitmapSegment);
133     }
134 joko 1.6
135     // calculate fractal
136     for (y = (args->start_row + args->number_of_rows) - 1; y >= args->start_row; y--) {
137 joko 1.7 //printf("calc: thread=%i; y=%i limits: %i,%i p: %p\n", thread_id, y, args->start_row, args->number_of_rows, pDataBitmapSegment);
138 joko 1.6 for (x = 0; x < XSIZE; x++) {
139     getColorValuesAt(x * (2.0 / XSIZE) - 1.5, y * (2.0 / YSIZE) - 1.0, &bgr[2], &bgr[1], &bgr[0]);
140    
141     // transfer color values to current pixel
142 joko 1.7 pDataBitmapSegment[0] = bgr[0];
143     pDataBitmapSegment[1] = bgr[1];
144     pDataBitmapSegment[2] = bgr[2];
145 joko 1.6
146     // move pointer to next pixel
147 joko 1.7 pDataBitmapSegment += 3;
148 joko 1.6
149     }
150 joko 1.9 //no padding required because 1500%4 =0 ???
151 joko 1.6 }
152    
153 joko 1.9 if (VERBOSE)
154     printf("thread finished: %i\n", thread_id);
155 joko 1.6 return 0;
156    
157     }
158    
159    
160 joko 1.3 int main(int argc, char *argv[]) {
161    
162 joko 1.4 // MMF support
163     DWORD err;
164 joko 1.3 HANDLE hMap, hFile;
165     LPVOID pData;
166 joko 1.9 unsigned char *pDataBitmap;
167 joko 1.4
168 joko 1.5 // workers
169 joko 1.9 int workers = 50;
170 joko 1.7 int worker_index, worker_rows, worker_startrow;
171 joko 1.6 HANDLE *worker_handles;
172     PWORKERARGS worker_args;
173    
174 joko 1.3
175 joko 1.4 // create empty bmp-file (black background)
176 joko 1.3 write_blank_file("test.bmp");
177    
178     /* open file for reading and writing */
179     hFile = CreateFile("test.bmp", GENERIC_WRITE|GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
180     if (INVALID_HANDLE_VALUE == hFile) {
181     err = GetLastError();
182     printErrorAndExit("Error at CreateFile",err);
183     }
184    
185     /* create the file mapping object */
186     hMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, NULL);
187     if (NULL == hMap) {
188     printErrorAndExit("Error at CreateFileMapping", GetLastError());
189     }
190    
191     /* map the whole file into the process context */
192     pData = MapViewOfFile(hMap, FILE_MAP_WRITE, 0, 0, 0);
193     if (NULL == pData) {
194     printErrorAndExit("Error at MapViewOfFile", GetLastError());
195     }
196    
197    
198     // calculate pointer to beginning of bitmap
199     pDataBitmap = (unsigned char *)((INT_PTR)pData + sizeof(header));
200    
201     // debugging
202 joko 1.9 if (VERBOSE) {
203     printf("pos. of file: %p\n", pData);
204     printf("pos. of bitmap: %p\n", pDataBitmap);
205     }
206 joko 1.3
207     /*
208 joko 1.9 // turn bitmap into white canvas
209 joko 1.3 for (offset = 0; offset < 500 * 500 * 3; offset++) {
210 joko 1.9 *pDataBitmap = 255;
211     pDataBitmap++;
212 joko 1.3 }
213 joko 1.9 exit(0);
214 joko 1.3 */
215    
216 joko 1.9 // allocate memory for bitmap
217     /*
218     if ((pDataBitmap = malloc(XSIZE * YSIZE * 3 * sizeof(pDataBitmap[0]))) == NULL)
219     perror("malloc"), exit(1);
220     */
221    
222 joko 1.6 // allocate memory for table of all worker handles
223 joko 1.9 if ((worker_handles = malloc(workers * sizeof(worker_handles[0]))) == NULL)
224 joko 1.6 perror("malloc"), exit(1);
225    
226     // allocate memory for table of all worker arguments
227 joko 1.9 if ((worker_args = malloc(workers * sizeof(worker_args[0]))) == NULL)
228 joko 1.6 perror("malloc"), exit(1);
229 joko 1.8
230 joko 1.6
231     // calculate segments of bitmap for worker threads/processes and start them
232 joko 1.5 worker_rows = YSIZE / workers;
233 joko 1.9 if (VERBOSE)
234     printf("rows for each worker: %i\n", worker_rows);
235 joko 1.5 for (worker_index = 0; worker_index < workers; worker_index++) {
236    
237 joko 1.9 // debugging: just run single thread
238 joko 1.7 //if (worker_index == 1)
239     // continue;
240    
241 joko 1.5 // number of row to start for each worker
242     worker_startrow = worker_index * worker_rows;
243    
244     // recalculate number of rows for last worker if (YSIZE mod workers) != 0
245     if (worker_index == workers - 1) {
246     worker_rows = YSIZE - worker_startrow;
247 joko 1.9 if (VERBOSE)
248     printf("rows for last worker: %i\n", worker_rows);
249 joko 1.5 }
250 joko 1.6
251     worker_args[worker_index].start_row = worker_startrow;
252     worker_args[worker_index].number_of_rows = worker_rows;
253     worker_args[worker_index].pBitmap = pDataBitmap;
254    
255     worker_handles[worker_index] = CreateThread(
256     NULL, // LPSECURITY_ATTRIBUTES lpThreadAttributes,
257     0, // SIZE_T dwStackSize,
258 joko 1.9 &fractal_thread, // LPTHREAD_START_ROUTINE lpStartAddress,
259 joko 1.6 &worker_args[worker_index], // LPVOID lpParameter,
260     0, // DWORD dwCreationFlags,
261     NULL // LPDWORD lpThreadId
262     );
263    
264 joko 1.5 }
265    
266 joko 1.6 // wait for all threads
267 joko 1.9 if (VERBOSE)
268     printf("waiting...\n");
269 joko 1.6 if (WaitForMultipleObjects(workers, worker_handles, TRUE, INFINITE) == WAIT_FAILED)
270     perror("WaitForMultipleObjects");
271 joko 1.7
272 joko 1.9 // debugging: just run single thread
273 joko 1.7 //if (WaitForSingleObject(worker_handles[0], INFINITE) == WAIT_FAILED)
274     // perror("WaitForSingleObject");
275    
276     // close all worker handles
277     for (worker_index = 0; worker_index < workers; worker_index++)
278     CloseHandle(worker_handles[worker_index]);
279    
280 joko 1.3 /* write the result into the file */
281     if (!FlushViewOfFile(pData, 0)) {
282     err = GetLastError();
283     printErrorAndExit("Error at UnmapViewOfFile", err);
284     }
285    
286     /* remove the mapped file */
287     if (!UnmapViewOfFile(pData)) {
288     err = GetLastError();
289     printErrorAndExit("Error at UnmapViewOfFile", err);
290     exit(err);
291     }
292    
293     /* cleanup handles */
294     if (!CloseHandle(hMap) || !CloseHandle(hFile) ) {
295     err = GetLastError();
296     printErrorAndExit("Error at CloseHandle", err);
297     }
298 joko 1.1
299 joko 1.9 free(worker_args);
300     free(worker_handles);
301 joko 1.8
302     return 0;
303    
304 joko 1.1 }

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed