]> de.git.xonotic.org Git - voretournament/voretournament.git/blob - misc/mediasource/extra/netradiant-src/contrib/gtkgensurf/bitmap.cpp
Include netRadiant source in this GIT
[voretournament/voretournament.git] / misc / mediasource / extra / netradiant-src / contrib / gtkgensurf / bitmap.cpp
1 /*
2 GenSurf plugin for GtkRadiant
3 Copyright (C) 2001 David Hyde, Loki software and qeradiant.com
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <math.h>
23 #include "gensurf.h"
24
25 void GenerateBitmapMapping ()
26 {
27   double              value;
28   double              C0, C1;
29   double              x, y;
30   int                 i, j;
31   int                 O00,O01,O10,O11;
32   int                 r0, r1, c0, c1;
33   int                 color;
34   unsigned char       *colors;
35
36   if (!gbmp.colors)
37     return;
38
39   colors = gbmp.colors;
40
41   for (j=0; j<=NV; j++)
42   {
43     y  = (double)(j*(gbmp.height-1))/(double)NV;
44     r0 = (int)floor(y);
45     r1 = (int)ceil(y);
46     for (i=0; i<=NH; i++)
47     {
48       x = (double)(i*(gbmp.width-1))/(double)NH;
49       c0 = (int)floor(x);
50       c1 = (int)ceil(x);
51       O00 = r0*gbmp.width + c0;
52       O01 = r0*gbmp.width + c1;
53       O10 = r1*gbmp.width + c0;
54       O11 = r1*gbmp.width + c1;
55       C0 = (double)colors[O00] + (double)(colors[O01]-colors[O00])*(x-(double)c0);
56       C1 = (double)colors[O10] + (double)(colors[O11]-colors[O10])*(x-(double)c0);
57       color = (int)(C0 + (C1-C0)*(y-r0));
58
59       value = CalculateSnapValue(gbmp.black_value + color*((gbmp.white_value-gbmp.black_value)/255.));
60
61       switch(Plane)
62       {
63       case PLANE_XZ0:
64       case PLANE_XZ1:
65         xyz[i][j].p[1] = value;
66         break;
67       case PLANE_YZ0:
68       case PLANE_YZ1:
69         xyz[i][j].p[0] = value;
70         break;
71       default:
72         xyz[i][j].p[2] = value;
73       }
74     }
75   }
76 }
77
78 static unsigned char* OpenBitmapFile ()
79 {
80 #define INVALID_FORMAT do{\
81   fprintf(stderr,"%s:%d: Error file '%s' is malformed.\n",__FILE__,__LINE__,gbmp.name);\
82   fclose(fp);\
83   return NULL;\
84 }while(0);
85
86   int32_t bmWidth;
87   int32_t bmHeight;
88   uint16_t bmPlanes;
89   uint16_t bmBitsPixel;
90   uint8_t m1,m2;
91   uint32_t sizeimage;
92   int16_t res1,res2;
93   int32_t filesize, pixoff;
94   int32_t bmisize, compression;
95   int32_t xscale, yscale;
96   int32_t colors, impcol;
97   uint32_t m_bytesRead = 0;
98   unsigned char *image;
99   FILE *fp;
100
101   fp = fopen (gbmp.name, "rb");
102   if (fp == NULL)
103   {
104           fprintf(stderr,"Error: Invalid filename '%s'\n",gbmp.name);
105           return NULL;
106   }
107
108   long rc;
109   rc = fread(&m1, 1, 1, fp);
110   m_bytesRead++;
111   if (rc == -1)
112   INVALID_FORMAT;
113
114   rc = fread(&m2, 1, 1, fp);
115   m_bytesRead++;
116   if ((m1 != 'B') || (m2 != 'M'))
117   INVALID_FORMAT;
118
119   rc = fread((uint32_t*)&(filesize),4,1,fp); m_bytesRead+=4;
120   if (rc != 1) INVALID_FORMAT;
121
122   rc = fread((uint16_t*)&(res1),2,1,fp); m_bytesRead+=2;
123   if (rc != 1) INVALID_FORMAT;
124
125   rc = fread((uint16_t*)&(res2),2,1,fp); m_bytesRead+=2;
126   if (rc != 1) INVALID_FORMAT;
127
128   rc = fread((uint32_t*)&(pixoff),4,1,fp); m_bytesRead+=4;
129   if (rc != 1) INVALID_FORMAT;
130
131   rc = fread((uint32_t*)&(bmisize),4,1,fp); m_bytesRead+=4;
132   if (rc != 1) INVALID_FORMAT;
133
134   rc = fread((uint32_t  *)&(bmWidth),4,1,fp); m_bytesRead+=4;
135   if (rc != 1) INVALID_FORMAT;
136
137   rc = fread((uint32_t*)&(bmHeight),4,1,fp); m_bytesRead+=4;
138   if (rc != 1) INVALID_FORMAT;
139
140   rc = fread((uint16_t*)&(bmPlanes),2,1,fp); m_bytesRead+=2;
141   if (rc != 1) INVALID_FORMAT;
142
143   rc = fread((uint16_t*)&(bmBitsPixel),2,1,fp); m_bytesRead+=2;
144   if (rc != 1) INVALID_FORMAT;
145
146   rc = fread((uint32_t*)&(compression),4,1,fp); m_bytesRead+=4;
147   if (rc != 1) INVALID_FORMAT;
148
149   rc = fread((uint32_t*)&(sizeimage),4,1,fp); m_bytesRead+=4;
150   if (rc != 1) INVALID_FORMAT;
151
152   rc = fread((uint32_t*)&(xscale),4,1,fp); m_bytesRead+=4;
153   if (rc != 1) INVALID_FORMAT;
154
155   rc = fread((uint32_t*)&(yscale),4,1,fp); m_bytesRead+=4;
156   if (rc != 1) INVALID_FORMAT;
157
158   rc = fread((uint32_t*)&(colors),4,1,fp); m_bytesRead+=4;
159   if (rc != 1) INVALID_FORMAT;
160
161   rc = fread((uint32_t*)&(impcol),4,1,fp); m_bytesRead+=4;
162   if (rc != 1) INVALID_FORMAT;
163
164   if (bmBitsPixel != 8)
165   {
166     g_FuncTable.m_pfnMessageBox (g_pWnd, "This is not an 8-bit image. GenSurf can't use it.",
167                                  "Bitmap", eMB_OK, eMB_ICONWARNING);
168     fclose(fp);
169     return NULL; 
170   }
171
172   if (colors == 0)
173     colors = 1 << bmBitsPixel;
174
175   if (bmBitsPixel != 24)
176   {
177     int i;
178     for (i = 0; i < colors; i++)
179     {
180       unsigned char r ,g, b, dummy;
181
182       rc = fread(&b, 1, 1, fp);
183       m_bytesRead++;
184       if (rc!=1)
185       {
186                INVALID_FORMAT;
187       }
188
189       rc = fread(&g, 1, 1, fp); 
190       m_bytesRead++;
191       if (rc!=1)
192       {
193                INVALID_FORMAT;
194       }
195
196       rc = fread(&r, 1, 1, fp); 
197       m_bytesRead++;
198       if (rc != 1)
199       {
200                INVALID_FORMAT;
201       }
202
203       rc = fread(&dummy, 1, 1, fp); 
204       m_bytesRead++;
205       if (rc != 1)
206       {
207                INVALID_FORMAT;
208       }
209     }
210   }
211
212   if ((long)m_bytesRead > pixoff)
213   {
214            INVALID_FORMAT;
215   }
216
217   while ((long)m_bytesRead < pixoff)
218   {
219     char dummy;
220     fread(&dummy,1,1,fp);
221     m_bytesRead++;
222   }
223
224   int w = bmWidth;
225   int h = bmHeight;
226
227   // set the output params
228   image = (unsigned char*)malloc(w*h);
229
230   if (image != NULL) 
231   {
232     unsigned char* outbuf = image;
233     long row = 0;
234     long rowOffset = 0;
235
236     if (compression == 0) // BI_RGB
237     {
238       for (row = 0; row < bmHeight; row++)
239       {
240         // which row are we working on?
241         rowOffset = (long unsigned)row*w;                                                     
242
243         {
244           // pixels are packed as 1 , 4 or 8 bit vals. need to unpack them
245           int bit_count = 0;
246           unsigned long mask = (1 << bmBitsPixel) - 1;
247           unsigned char inbyte = 0;
248
249           for (int col=0;col<w;col++)
250           {
251             int pix = 0;
252
253             // if we need another byte
254             if (bit_count <= 0)
255             {
256               bit_count = 8;
257               if (fread(&inbyte,1,1,fp) != 1)
258               {
259                 free(image);
260                  INVALID_FORMAT;
261               }
262               m_bytesRead++;
263             }
264
265             // keep track of where we are in the bytes
266             bit_count -= bmBitsPixel;
267             pix = ( inbyte >> bit_count) & mask;
268
269             // lookup the color from the colormap - stuff it in our buffer
270             // swap red and blue
271             *(outbuf + rowOffset + col) = pix;
272           }
273
274           // read DWORD padding
275           while ((m_bytesRead-pixoff)&3)
276           {
277             char dummy;
278             if (fread(&dummy,1,1,fp)!=1)
279             {
280               free(image);
281                INVALID_FORMAT;
282             }
283             m_bytesRead++;
284           }
285         }
286       }
287     }
288     else    // compression != 0
289     {
290       int i, x = 0;
291       unsigned char c, c1 = 0, *pp;
292       row = 0;
293       pp = outbuf;
294
295       if (bmBitsPixel == 8)
296       {
297         while (row < bmHeight)
298         {
299           c = getc(fp);
300
301           if (c)
302           {
303             // encoded mode
304             c1 = getc(fp);
305             for (i = 0; i < c; x++, i++)
306             {
307               *pp = c1; pp++;
308             }
309           }
310           else
311           {
312             // c==0x00,  escape codes
313             c = getc(fp);
314
315             if (c == 0x00) // end of line
316             {
317               row++;
318               x = 0;
319               pp = outbuf + row*bmWidth;
320             }
321             else if (c == 0x01)
322               break; // end of pic
323             else if (c == 0x02) // delta
324             {
325               c = getc(fp);
326               x += c;
327               c = getc(fp);
328               row += c;
329               pp = outbuf + x + row*bmWidth;
330             }
331             else // absolute mode
332             {
333               for (i = 0; i < c; x++, i++)
334               {
335                 c1 = getc(fp);
336                 *pp = c1; pp++;
337               }
338
339               if (c & 1)
340                 getc(fp); // odd length run: read an extra pad byte
341             }
342           }
343         }
344       }
345       else if (bmBitsPixel == 4)
346       {
347         while (row < bmHeight)
348         {
349           c = getc(fp);
350
351           if (c)
352           {
353             // encoded mode
354             c1 = getc(fp);
355             for (i = 0; i < c; x++, i++)
356             {
357               *pp = (i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f); pp++;
358             }
359           }
360           else
361           {
362             // c==0x00,  escape codes
363             c = getc(fp);
364
365             if (c == 0x00) // end of line
366             {
367               row++;
368               x = 0;
369               pp = outbuf + bmHeight*bmWidth;
370             }
371             else if (c == 0x01)
372               break; // end of pic
373             else if (c == 0x02) // delta
374             {
375               c = getc(fp);
376               x += c;
377               c = getc(fp);
378               row += c;
379               pp = outbuf + x + row*bmWidth;
380             }
381             else // absolute mode
382             {
383               for (i = 0; i < c; x++, i++)
384               {
385                 if ((i&1) == 0)
386                   c1 = getc(fp);
387                 *pp = (i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f); pp++;
388               }
389
390               if (((c&3) == 1) || ((c&3) == 2))
391                 getc(fp); // odd length run: read an extra pad byte
392             }
393           }
394         }
395       }
396     }
397   }
398   fclose(fp);
399
400   gbmp.width = w;
401   gbmp.height = h;
402   if(gbmp.colors)
403         free(gbmp.colors);
404   gbmp.colors = image;
405   return image;
406
407
408 }
409
410 bool OpenBitmap ()
411 {
412
413   OpenBitmapFile ();
414
415   if (!gbmp.colors)
416   {
417     char Text[256];
418
419     sprintf (Text, "Error opening %s", gbmp.name);
420     g_FuncTable.m_pfnMessageBox (g_pWnd, Text, "Bitmap", eMB_OK, eMB_ICONWARNING);
421     strcpy (gbmp.name, "");
422   }
423
424   if (g_pWnd)
425   {
426     gtk_entry_set_text (GTK_ENTRY (g_object_get_data (G_OBJECT (g_pWnd), "bmp_file")), gbmp.name);
427     gtk_widget_set_sensitive (GTK_WIDGET (g_object_get_data (G_OBJECT (g_pWnd), "bmp_reload")),
428                               strlen (gbmp.name) ? TRUE : FALSE);
429
430     UpdatePreview (true);
431   }
432
433   return (gbmp.colors != NULL);
434 }