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 void GenerateBitmapMapping ()
34 unsigned char *colors;
43 y = (double)(j*(gbmp.height-1))/(double)NV;
48 x = (double)(i*(gbmp.width-1))/(double)NH;
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));
59 value = CalculateSnapValue(gbmp.black_value + color*((gbmp.white_value-gbmp.black_value)/255.));
65 xyz[i][j].p[1] = value;
69 xyz[i][j].p[0] = value;
72 xyz[i][j].p[2] = value;
78 static unsigned char* OpenBitmapFile ()
80 #define INVALID_FORMAT do{\
81 fprintf(stderr,"%s:%d: Error file '%s' is malformed.\n",__FILE__,__LINE__,gbmp.name);\
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;
101 fp = fopen (gbmp.name, "rb");
104 fprintf(stderr,"Error: Invalid filename '%s'\n",gbmp.name);
109 rc = fread(&m1, 1, 1, fp);
114 rc = fread(&m2, 1, 1, fp);
116 if ((m1 != 'B') || (m2 != 'M'))
119 rc = fread((uint32_t*)&(filesize),4,1,fp); m_bytesRead+=4;
120 if (rc != 1) INVALID_FORMAT;
122 rc = fread((uint16_t*)&(res1),2,1,fp); m_bytesRead+=2;
123 if (rc != 1) INVALID_FORMAT;
125 rc = fread((uint16_t*)&(res2),2,1,fp); m_bytesRead+=2;
126 if (rc != 1) INVALID_FORMAT;
128 rc = fread((uint32_t*)&(pixoff),4,1,fp); m_bytesRead+=4;
129 if (rc != 1) INVALID_FORMAT;
131 rc = fread((uint32_t*)&(bmisize),4,1,fp); m_bytesRead+=4;
132 if (rc != 1) INVALID_FORMAT;
134 rc = fread((uint32_t *)&(bmWidth),4,1,fp); m_bytesRead+=4;
135 if (rc != 1) INVALID_FORMAT;
137 rc = fread((uint32_t*)&(bmHeight),4,1,fp); m_bytesRead+=4;
138 if (rc != 1) INVALID_FORMAT;
140 rc = fread((uint16_t*)&(bmPlanes),2,1,fp); m_bytesRead+=2;
141 if (rc != 1) INVALID_FORMAT;
143 rc = fread((uint16_t*)&(bmBitsPixel),2,1,fp); m_bytesRead+=2;
144 if (rc != 1) INVALID_FORMAT;
146 rc = fread((uint32_t*)&(compression),4,1,fp); m_bytesRead+=4;
147 if (rc != 1) INVALID_FORMAT;
149 rc = fread((uint32_t*)&(sizeimage),4,1,fp); m_bytesRead+=4;
150 if (rc != 1) INVALID_FORMAT;
152 rc = fread((uint32_t*)&(xscale),4,1,fp); m_bytesRead+=4;
153 if (rc != 1) INVALID_FORMAT;
155 rc = fread((uint32_t*)&(yscale),4,1,fp); m_bytesRead+=4;
156 if (rc != 1) INVALID_FORMAT;
158 rc = fread((uint32_t*)&(colors),4,1,fp); m_bytesRead+=4;
159 if (rc != 1) INVALID_FORMAT;
161 rc = fread((uint32_t*)&(impcol),4,1,fp); m_bytesRead+=4;
162 if (rc != 1) INVALID_FORMAT;
164 if (bmBitsPixel != 8)
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);
173 colors = 1 << bmBitsPixel;
175 if (bmBitsPixel != 24)
178 for (i = 0; i < colors; i++)
180 unsigned char r ,g, b, dummy;
182 rc = fread(&b, 1, 1, fp);
189 rc = fread(&g, 1, 1, fp);
196 rc = fread(&r, 1, 1, fp);
203 rc = fread(&dummy, 1, 1, fp);
212 if ((long)m_bytesRead > pixoff)
217 while ((long)m_bytesRead < pixoff)
220 fread(&dummy,1,1,fp);
227 // set the output params
228 image = (unsigned char*)malloc(w*h);
232 unsigned char* outbuf = image;
236 if (compression == 0) // BI_RGB
238 for (row = 0; row < bmHeight; row++)
240 // which row are we working on?
241 rowOffset = (long unsigned)row*w;
244 // pixels are packed as 1 , 4 or 8 bit vals. need to unpack them
246 unsigned long mask = (1 << bmBitsPixel) - 1;
247 unsigned char inbyte = 0;
249 for (int col=0;col<w;col++)
253 // if we need another byte
257 if (fread(&inbyte,1,1,fp) != 1)
265 // keep track of where we are in the bytes
266 bit_count -= bmBitsPixel;
267 pix = ( inbyte >> bit_count) & mask;
269 // lookup the color from the colormap - stuff it in our buffer
271 *(outbuf + rowOffset + col) = pix;
274 // read DWORD padding
275 while ((m_bytesRead-pixoff)&3)
278 if (fread(&dummy,1,1,fp)!=1)
288 else // compression != 0
291 unsigned char c, c1 = 0, *pp;
295 if (bmBitsPixel == 8)
297 while (row < bmHeight)
305 for (i = 0; i < c; x++, i++)
312 // c==0x00, escape codes
315 if (c == 0x00) // end of line
319 pp = outbuf + row*bmWidth;
323 else if (c == 0x02) // delta
329 pp = outbuf + x + row*bmWidth;
331 else // absolute mode
333 for (i = 0; i < c; x++, i++)
340 getc(fp); // odd length run: read an extra pad byte
345 else if (bmBitsPixel == 4)
347 while (row < bmHeight)
355 for (i = 0; i < c; x++, i++)
357 *pp = (i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f); pp++;
362 // c==0x00, escape codes
365 if (c == 0x00) // end of line
369 pp = outbuf + bmHeight*bmWidth;
373 else if (c == 0x02) // delta
379 pp = outbuf + x + row*bmWidth;
381 else // absolute mode
383 for (i = 0; i < c; x++, i++)
387 *pp = (i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f); pp++;
390 if (((c&3) == 1) || ((c&3) == 2))
391 getc(fp); // odd length run: read an extra pad byte
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, "");
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);
430 UpdatePreview (true);
433 return (gbmp.colors != NULL);