2 GenSurf plugin for GtkRadiant
3 Copyright (C) 2001 David Hyde, Loki software and qeradiant.com
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.
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.
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
25 typedef __int32 int32_t;
26 typedef unsigned __int16 uint16_t;
27 typedef unsigned __int8 uint8_t;
28 typedef unsigned __int32 uint32_t;
29 typedef __int16 int16_t;
35 void GenerateBitmapMapping ()
44 unsigned char *colors;
53 y = (double)(j*(gbmp.height-1))/(double)NV;
58 x = (double)(i*(gbmp.width-1))/(double)NH;
61 O00 = r0*gbmp.width + c0;
62 O01 = r0*gbmp.width + c1;
63 O10 = r1*gbmp.width + c0;
64 O11 = r1*gbmp.width + c1;
65 C0 = (double)colors[O00] + (double)(colors[O01]-colors[O00])*(x-(double)c0);
66 C1 = (double)colors[O10] + (double)(colors[O11]-colors[O10])*(x-(double)c0);
67 color = (int)(C0 + (C1-C0)*(y-r0));
69 value = CalculateSnapValue(gbmp.black_value + color*((gbmp.white_value-gbmp.black_value)/255.));
75 xyz[i][j].p[1] = value;
79 xyz[i][j].p[0] = value;
82 xyz[i][j].p[2] = value;
88 static unsigned char* OpenBitmapFile ()
90 #define INVALID_FORMAT do{\
91 fprintf(stderr,"%s:%d: Error file '%s' is malformed.\n",__FILE__,__LINE__,gbmp.name);\
103 int32_t filesize, pixoff;
104 int32_t bmisize, compression;
105 int32_t xscale, yscale;
106 int32_t colors, impcol;
107 uint32_t m_bytesRead = 0;
108 unsigned char *image;
111 fp = fopen (gbmp.name, "rb");
114 fprintf(stderr,"Error: Invalid filename '%s'\n",gbmp.name);
119 rc = fread(&m1, 1, 1, fp);
127 rc = fread(&m2, 1, 1, fp);
129 if ((m1 != 'B') || (m2 != 'M'))
132 rc = fread((uint32_t*)&(filesize),4,1,fp); m_bytesRead+=4;
133 if (rc != 1) INVALID_FORMAT;
135 rc = fread((uint16_t*)&(res1),2,1,fp); m_bytesRead+=2;
136 if (rc != 1) INVALID_FORMAT;
138 rc = fread((uint16_t*)&(res2),2,1,fp); m_bytesRead+=2;
139 if (rc != 1) INVALID_FORMAT;
141 rc = fread((uint32_t*)&(pixoff),4,1,fp); m_bytesRead+=4;
142 if (rc != 1) INVALID_FORMAT;
144 rc = fread((uint32_t*)&(bmisize),4,1,fp); m_bytesRead+=4;
145 if (rc != 1) INVALID_FORMAT;
147 rc = fread((uint32_t *)&(bmWidth),4,1,fp); m_bytesRead+=4;
148 if (rc != 1) INVALID_FORMAT;
150 rc = fread((uint32_t*)&(bmHeight),4,1,fp); m_bytesRead+=4;
151 if (rc != 1) INVALID_FORMAT;
153 rc = fread((uint16_t*)&(bmPlanes),2,1,fp); m_bytesRead+=2;
154 if (rc != 1) INVALID_FORMAT;
156 rc = fread((uint16_t*)&(bmBitsPixel),2,1,fp); m_bytesRead+=2;
157 if (rc != 1) INVALID_FORMAT;
159 rc = fread((uint32_t*)&(compression),4,1,fp); m_bytesRead+=4;
160 if (rc != 1) INVALID_FORMAT;
162 rc = fread((uint32_t*)&(sizeimage),4,1,fp); m_bytesRead+=4;
163 if (rc != 1) INVALID_FORMAT;
165 rc = fread((uint32_t*)&(xscale),4,1,fp); m_bytesRead+=4;
166 if (rc != 1) INVALID_FORMAT;
168 rc = fread((uint32_t*)&(yscale),4,1,fp); m_bytesRead+=4;
169 if (rc != 1) INVALID_FORMAT;
171 rc = fread((uint32_t*)&(colors),4,1,fp); m_bytesRead+=4;
172 if (rc != 1) INVALID_FORMAT;
174 rc = fread((uint32_t*)&(impcol),4,1,fp); m_bytesRead+=4;
175 if (rc != 1) INVALID_FORMAT;
177 if (bmBitsPixel != 8)
179 g_FuncTable.m_pfnMessageBox (g_pWnd, "This is not an 8-bit image. GenSurf can't use it.",
180 "Bitmap", MB_ICONEXCLAMATION, NULL);
185 colors = 1 << bmBitsPixel;
187 if (bmBitsPixel != 24)
190 for (i = 0; i < colors; i++)
192 unsigned char r ,g, b, dummy;
194 rc = fread(&b, 1, 1, fp);
201 rc = fread(&g, 1, 1, fp);
208 rc = fread(&r, 1, 1, fp);
215 rc = fread(&dummy, 1, 1, fp);
224 if ((long)m_bytesRead > pixoff)
229 while ((long)m_bytesRead < pixoff)
232 fread(&dummy,1,1,fp);
239 // set the output params
240 image = (unsigned char*)malloc(w*h);
244 unsigned char* outbuf = image;
248 if (compression == 0) // BI_RGB
250 for (row = 0; row < bmHeight; row++)
252 // which row are we working on?
253 rowOffset = (long unsigned)row*w;
256 // pixels are packed as 1 , 4 or 8 bit vals. need to unpack them
258 unsigned long mask = (1 << bmBitsPixel) - 1;
259 unsigned char inbyte = 0;
261 for (int col=0;col<w;col++)
265 // if we need another byte
269 if (fread(&inbyte,1,1,fp) != 1)
277 // keep track of where we are in the bytes
278 bit_count -= bmBitsPixel;
279 pix = ( inbyte >> bit_count) & mask;
281 // lookup the color from the colormap - stuff it in our buffer
283 *(outbuf + rowOffset + col) = pix;
286 // read DWORD padding
287 while ((m_bytesRead-pixoff)&3)
290 if (fread(&dummy,1,1,fp)!=1)
300 else // compression != 0
303 unsigned char c, c1 = 0, *pp;
307 if (bmBitsPixel == 8)
309 while (row < bmHeight)
317 for (i = 0; i < c; x++, i++)
324 // c==0x00, escape codes
327 if (c == 0x00) // end of line
331 pp = outbuf + row*bmWidth;
335 else if (c == 0x02) // delta
341 pp = outbuf + x + row*bmWidth;
343 else // absolute mode
345 for (i = 0; i < c; x++, i++)
352 getc(fp); // odd length run: read an extra pad byte
357 else if (bmBitsPixel == 4)
359 while (row < bmHeight)
367 for (i = 0; i < c; x++, i++)
369 *pp = (i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f); pp++;
374 // c==0x00, escape codes
377 if (c == 0x00) // end of line
381 pp = outbuf + bmHeight*bmWidth;
385 else if (c == 0x02) // delta
391 pp = outbuf + x + row*bmWidth;
393 else // absolute mode
395 for (i = 0; i < c; x++, i++)
399 *pp = (i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f); pp++;
402 if (((c&3) == 1) || ((c&3) == 2))
403 getc(fp); // odd length run: read an extra pad byte
429 g_print("failed to load file gbmp %s\n",gbmp.name);
432 sprintf (Text, "Error opening %s", gbmp.name);
433 g_FuncTable.m_pfnMessageBox (g_pWnd, Text, "Bitmap", MB_ICONEXCLAMATION, NULL);
434 strcpy (gbmp.name, "");
439 gtk_entry_set_text (GTK_ENTRY (g_object_get_data (G_OBJECT (g_pWnd), "bmp_file")), gbmp.name);
440 gtk_widget_set_sensitive (GTK_WIDGET (g_object_get_data (G_OBJECT (g_pWnd), "bmp_reload")),
441 strlen (gbmp.name) ? TRUE : FALSE);
443 UpdatePreview (true);
446 return (gbmp.colors != NULL);