--- joko/Uni/BSArch/04/bmp_fractal.c 2006/07/01 11:58:49 1.4 +++ joko/Uni/BSArch/04/bmp_fractal.c 2006/07/02 00:24:17 1.9 @@ -5,7 +5,7 @@ * Uebung 4.4 */ -// $Id: bmp_fractal.c,v 1.4 2006/07/01 11:58:49 joko Exp $ +// $Id: bmp_fractal.c,v 1.9 2006/07/02 00:24:17 joko Exp $ #include #include @@ -15,6 +15,8 @@ #define YSIZE 500 #include "algorithm.h" +BOOL VERBOSE = FALSE; + /* BMP Header */ unsigned char header[54]={0x42,0x4d, // signature BM @@ -94,18 +96,81 @@ } +// arguments for each thread +typedef struct _WorkerArguments { + int start_row; + int number_of_rows; + unsigned char * pBitmap; +} WORKERARGS, *PWORKERARGS; + +// worker thread - main entry function +DWORD WINAPI fractal_thread (LPVOID lpParam) { + + // thread stuff + int thread_id; + PWORKERARGS args; + unsigned char *pDataBitmapSegment; + + // fractal calculation + int x, y; + char bgr[3]; + + thread_id = GetCurrentThreadId(); + + // get worker arguments + args = (PWORKERARGS)lpParam; + + // calculate pointer to beginning of segment + pDataBitmapSegment = (unsigned char *)((INT_PTR)args->pBitmap + (YSIZE - (args->start_row + args->number_of_rows)) * 3 * XSIZE); + + // debugging + if (VERBOSE) { + printf("----------------------------------------------\n"); + printf("thread_id: %i\n", thread_id); + printf("arg.start_row: %i\n", args->start_row); + printf("arg.number_of_rows: %i\n", args->number_of_rows); + printf("segment_start: %p\n", pDataBitmapSegment); + } + + // calculate fractal + for (y = (args->start_row + args->number_of_rows) - 1; y >= args->start_row; y--) { + //printf("calc: thread=%i; y=%i limits: %i,%i p: %p\n", thread_id, y, args->start_row, args->number_of_rows, pDataBitmapSegment); + 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]); + + // transfer color values to current pixel + pDataBitmapSegment[0] = bgr[0]; + pDataBitmapSegment[1] = bgr[1]; + pDataBitmapSegment[2] = bgr[2]; + + // move pointer to next pixel + pDataBitmapSegment += 3; + + } + //no padding required because 1500%4 =0 ??? + } + + if (VERBOSE) + printf("thread finished: %i\n", thread_id); + return 0; + +} + + int main(int argc, char *argv[]) { // MMF support DWORD err; HANDLE hMap, hFile; LPVOID pData; - unsigned char *pDataBitmap, *pDataBitmapCurrent; + unsigned char *pDataBitmap; + + // workers + int workers = 50; + int worker_index, worker_rows, worker_startrow; + HANDLE *worker_handles; + PWORKERARGS worker_args; - // fractal calculation - int x, y; - char bgr[3]; - // create empty bmp-file (black background) write_blank_file("test.bmp"); @@ -134,42 +199,84 @@ pDataBitmap = (unsigned char *)((INT_PTR)pData + sizeof(header)); // debugging - printf("pos. of file: %p\n", pData); - printf("pos. of bitmap: %p\n", pDataBitmap); - - // pointer to current pixel - pDataBitmapCurrent = pDataBitmap; + if (VERBOSE) { + printf("pos. of file: %p\n", pData); + printf("pos. of bitmap: %p\n", pDataBitmap); + } /* - // turn bitmap into white wand + // turn bitmap into white canvas for (offset = 0; offset < 500 * 500 * 3; offset++) { - //pDataBitmap[offset] = 255; - *pDataBitmapCurrent = 255; - pDataBitmapCurrent++; + *pDataBitmap = 255; + pDataBitmap++; } + exit(0); */ - // calculate fractal - for (y=YSIZE-1; y>=0; y--) { - for (x=0; x