/[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.4 - (hide annotations)
Sat Jul 1 11:58:49 2006 UTC (18 years, 2 months ago) by joko
Branch: MAIN
Changes since 1.3: +8 -12 lines
File MIME type: text/plain
some 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.4 // $Id: bmp_fractal.c,v 1.3 2006/07/01 11:45:35 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.3
19 joko 1.2 /* BMP Header */
20 joko 1.1 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 joko 1.3
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 joko 1.1 }
90 joko 1.3 }
91    
92     // close file handle
93     fclose(fd);
94     }
95    
96    
97     int main(int argc, char *argv[]) {
98    
99 joko 1.4 // MMF support
100     DWORD err;
101 joko 1.3 HANDLE hMap, hFile;
102     LPVOID pData;
103     unsigned char *pDataBitmap, *pDataBitmapCurrent;
104 joko 1.4
105     // fractal calculation
106     int x, y;
107     char bgr[3];
108 joko 1.3
109    
110 joko 1.4 // create empty bmp-file (black background)
111 joko 1.3 write_blank_file("test.bmp");
112    
113     /* open file for reading and writing */
114     hFile = CreateFile("test.bmp", GENERIC_WRITE|GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
115     if (INVALID_HANDLE_VALUE == hFile) {
116     err = GetLastError();
117     printErrorAndExit("Error at CreateFile",err);
118     }
119    
120     /* create the file mapping object */
121     hMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, NULL);
122     if (NULL == hMap) {
123     printErrorAndExit("Error at CreateFileMapping", GetLastError());
124     }
125    
126     /* map the whole file into the process context */
127     pData = MapViewOfFile(hMap, FILE_MAP_WRITE, 0, 0, 0);
128     if (NULL == pData) {
129     printErrorAndExit("Error at MapViewOfFile", GetLastError());
130     }
131    
132    
133     // calculate pointer to beginning of bitmap
134     pDataBitmap = (unsigned char *)((INT_PTR)pData + sizeof(header));
135    
136     // debugging
137     printf("pos. of file: %p\n", pData);
138     printf("pos. of bitmap: %p\n", pDataBitmap);
139    
140     // pointer to current pixel
141     pDataBitmapCurrent = pDataBitmap;
142    
143     /*
144     // turn bitmap into white wand
145     for (offset = 0; offset < 500 * 500 * 3; offset++) {
146     //pDataBitmap[offset] = 255;
147     *pDataBitmapCurrent = 255;
148     pDataBitmapCurrent++;
149     }
150     */
151    
152     // calculate fractal
153     for (y=YSIZE-1; y>=0; y--) {
154     for (x=0; x<XSIZE; x++) {
155     getColorValuesAt(x * (2.0 / XSIZE) - 1.5, y * (2.0 / YSIZE) - 1.0, &bgr[2], &bgr[1], &bgr[0]);
156    
157     // debugging
158     //printf("pointer: %p\n", pDataBitmapCurrent);
159    
160     // transfer color values to current pixel
161     pDataBitmapCurrent[0] = bgr[0];
162     pDataBitmapCurrent[1] = bgr[1];
163     pDataBitmapCurrent[2] = bgr[2];
164    
165     // move pointer to next pixel
166     pDataBitmapCurrent += 3;
167    
168     }
169     //no padding required because 1500%4 =0
170     }
171    
172    
173     /* write the result into the file */
174     if (!FlushViewOfFile(pData, 0)) {
175     err = GetLastError();
176     printErrorAndExit("Error at UnmapViewOfFile", err);
177     }
178    
179     /* remove the mapped file */
180     if (!UnmapViewOfFile(pData)) {
181     err = GetLastError();
182     printErrorAndExit("Error at UnmapViewOfFile", err);
183     exit(err);
184     }
185    
186     /* cleanup handles */
187     if (!CloseHandle(hMap) || !CloseHandle(hFile) ) {
188     err = GetLastError();
189     printErrorAndExit("Error at CloseHandle", err);
190     }
191 joko 1.1
192     }

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