2 GenSurf plugin for GtkRadiant
\r
3 Copyright (C) 2001 David Hyde, Loki software and qeradiant.com
\r
5 This library is free software; you can redistribute it and/or
\r
6 modify it under the terms of the GNU Lesser General Public
\r
7 License as published by the Free Software Foundation; either
\r
8 version 2.1 of the License, or (at your option) any later version.
\r
10 This library is distributed in the hope that it will be useful,
\r
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
\r
13 Lesser General Public License for more details.
\r
15 You should have received a copy of the GNU Lesser General Public
\r
16 License along with this library; if not, write to the Free Software
\r
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
23 #include "gensurf.h"
\r
25 void GenerateBitmapMapping ()
\r
31 int O00,O01,O10,O11;
\r
34 unsigned char *colors;
\r
39 colors = gbmp.colors;
\r
41 for (j=0; j<=NV; j++)
\r
43 y = (double)(j*(gbmp.height-1))/(double)NV;
\r
46 for (i=0; i<=NH; i++)
\r
48 x = (double)(i*(gbmp.width-1))/(double)NH;
\r
51 O00 = r0*gbmp.width + c0;
\r
52 O01 = r0*gbmp.width + c1;
\r
53 O10 = r1*gbmp.width + c0;
\r
54 O11 = r1*gbmp.width + c1;
\r
55 C0 = (double)colors[O00] + (double)(colors[O01]-colors[O00])*(x-(double)c0);
\r
56 C1 = (double)colors[O10] + (double)(colors[O11]-colors[O10])*(x-(double)c0);
\r
57 color = (int)(C0 + (C1-C0)*(y-r0));
\r
59 value = CalculateSnapValue(gbmp.black_value + color*((gbmp.white_value-gbmp.black_value)/255.));
\r
65 xyz[i][j].p[1] = value;
\r
69 xyz[i][j].p[0] = value;
\r
72 xyz[i][j].p[2] = value;
\r
78 static unsigned char* OpenBitmapFile ()
\r
82 unsigned char bmPlanes;
\r
83 unsigned char bmBitsPixel;
\r
84 unsigned char m1,m2;
\r
85 unsigned long sizeimage;
\r
87 long filesize, pixoff;
\r
88 long bmisize, compression;
\r
89 long xscale, yscale;
\r
90 long colors, impcol;
\r
91 unsigned long m_bytesRead = 0;
\r
92 unsigned char *image;
\r
95 fp = fopen (gbmp.name, "rb");
\r
100 rc = fread(&m1, 1, 1, fp);
\r
108 rc = fread(&m2, 1, 1, fp);
\r
110 if ((m1 != 'B') || (m2 != 'M'))
\r
116 rc = fread((long*)&(filesize),4,1,fp); m_bytesRead+=4;
\r
117 if (rc != 1) { fclose(fp); return NULL; }
\r
119 rc = fread((int*)&(res1),2,1,fp); m_bytesRead+=2;
\r
120 if (rc != 1) { fclose(fp); return NULL; }
\r
122 rc = fread((int*)&(res2),2,1,fp); m_bytesRead+=2;
\r
123 if (rc != 1) { fclose(fp); return NULL; }
\r
125 rc = fread((long*)&(pixoff),4,1,fp); m_bytesRead+=4;
\r
126 if (rc != 1) { fclose(fp); return NULL; }
\r
128 rc = fread((long*)&(bmisize),4,1,fp); m_bytesRead+=4;
\r
129 if (rc != 1) { fclose(fp); return NULL; }
\r
131 rc = fread((long *)&(bmWidth),4,1,fp); m_bytesRead+=4;
\r
132 if (rc != 1) { fclose(fp); return NULL; }
\r
134 rc = fread((long*)&(bmHeight),4,1,fp); m_bytesRead+=4;
\r
135 if (rc != 1) { fclose(fp); return NULL; }
\r
137 rc = fread((int*)&(bmPlanes),2,1,fp); m_bytesRead+=2;
\r
138 if (rc != 1) { fclose(fp); return NULL; }
\r
140 rc = fread((int*)&(bmBitsPixel),2,1,fp); m_bytesRead+=2;
\r
141 if (rc != 1) { fclose(fp); return NULL; }
\r
143 rc = fread((long*)&(compression),4,1,fp); m_bytesRead+=4;
\r
144 if (rc != 1) { fclose(fp); return NULL; }
\r
146 rc = fread((long*)&(sizeimage),4,1,fp); m_bytesRead+=4;
\r
147 if (rc != 1) {fclose(fp); return NULL; }
\r
149 rc = fread((long*)&(xscale),4,1,fp); m_bytesRead+=4;
\r
150 if (rc != 1) { fclose(fp); return NULL; }
\r
152 rc = fread((long*)&(yscale),4,1,fp); m_bytesRead+=4;
\r
153 if (rc != 1) { fclose(fp); return NULL; }
\r
155 rc = fread((long*)&(colors),4,1,fp); m_bytesRead+=4;
\r
156 if (rc != 1) { fclose(fp); return NULL; }
\r
158 rc = fread((long*)&(impcol),4,1,fp); m_bytesRead+=4;
\r
159 if (rc != 1) { fclose(fp); return NULL; }
\r
161 if (bmBitsPixel != 8)
\r
163 g_FuncTable.m_pfnMessageBox (g_pWnd, "This is not an 8-bit image. GenSurf can't use it.",
\r
164 "Bitmap", MB_ICONEXCLAMATION);
\r
170 colors = 1 << bmBitsPixel;
\r
172 if (bmBitsPixel != 24)
\r
175 for (i = 0; i < colors; i++)
\r
177 unsigned char r ,g, b, dummy;
\r
179 rc = fread(&b, 1, 1, fp);
\r
187 rc = fread(&g, 1, 1, fp);
\r
195 rc = fread(&r, 1, 1, fp);
\r
203 rc = fread(&dummy, 1, 1, fp);
\r
213 if ((long)m_bytesRead > pixoff)
\r
219 while ((long)m_bytesRead < pixoff)
\r
222 fread(&dummy,1,1,fp);
\r
229 // set the output params
\r
230 image = (unsigned char*)malloc(w*h);
\r
232 if (image != NULL)
\r
236 unsigned char* outbuf = image;
\r
238 long rowOffset = 0;
\r
240 if (compression == 0) // BI_RGB
\r
242 for (row = 0; row < bmHeight; row++)
\r
244 // which row are we working on?
\r
245 rowOffset = (long unsigned)row*w;
\r
248 // pixels are packed as 1 , 4 or 8 bit vals. need to unpack them
\r
250 unsigned long mask = (1 << bmBitsPixel) - 1;
\r
251 unsigned char inbyte = 0;
\r
253 for (int col=0;col<w;col++)
\r
257 // if we need another byte
\r
258 if (bit_count <= 0)
\r
261 if (fread(&inbyte,1,1,fp) != 1)
\r
270 // keep track of where we are in the bytes
\r
271 bit_count -= bmBitsPixel;
\r
272 pix = ( inbyte >> bit_count) & mask;
\r
274 // lookup the color from the colormap - stuff it in our buffer
\r
275 // swap red and blue
\r
276 *(outbuf + rowOffset + col) = pix;
\r
279 // read DWORD padding
\r
280 while ((m_bytesRead-pixoff)&3)
\r
283 if (fread(&dummy,1,1,fp)!=1)
\r
294 else // compression != 0
\r
297 unsigned char c, c1 = 0, *pp;
\r
301 if (bmBitsPixel == 8)
\r
303 while (row < bmHeight)
\r
311 for (i = 0; i < c; x++, i++)
\r
318 // c==0x00, escape codes
\r
321 if (c == 0x00) // end of line
\r
325 pp = outbuf + row*bmWidth;
\r
327 else if (c == 0x01)
\r
328 break; // end of pic
\r
329 else if (c == 0x02) // delta
\r
335 pp = outbuf + x + row*bmWidth;
\r
337 else // absolute mode
\r
339 for (i = 0; i < c; x++, i++)
\r
346 getc(fp); // odd length run: read an extra pad byte
\r
351 else if (bmBitsPixel == 4)
\r
353 while (row < bmHeight)
\r
361 for (i = 0; i < c; x++, i++)
\r
363 *pp = (i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f); pp++;
\r
368 // c==0x00, escape codes
\r
371 if (c == 0x00) // end of line
\r
375 pp = outbuf + bmHeight*bmWidth;
\r
377 else if (c == 0x01)
\r
378 break; // end of pic
\r
379 else if (c == 0x02) // delta
\r
385 pp = outbuf + x + row*bmWidth;
\r
387 else // absolute mode
\r
389 for (i = 0; i < c; x++, i++)
\r
393 *pp = (i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f); pp++;
\r
396 if (((c&3) == 1) || ((c&3) == 2))
\r
397 getc(fp); // odd length run: read an extra pad byte
\r
411 free (gbmp.colors);
\r
413 gbmp.colors = OpenBitmapFile ();
\r
419 sprintf (Text, "Error opening %s", gbmp.name);
\r
420 g_FuncTable.m_pfnMessageBox (g_pWnd, Text, "Bitmap", MB_ICONEXCLAMATION);
\r
421 strcpy (gbmp.name, "");
\r
426 gtk_entry_set_text (GTK_ENTRY (g_object_get_data (G_OBJECT (g_pWnd), "bmp_file")), gbmp.name);
\r
427 gtk_widget_set_sensitive (GTK_WIDGET (g_object_get_data (G_OBJECT (g_pWnd), "bmp_reload")),
\r
428 strlen (gbmp.name) ? TRUE : FALSE);
\r
430 UpdatePreview (true);
\r
433 return (gbmp.colors != NULL);
\r