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 ()
82 unsigned char bmPlanes;
83 unsigned char bmBitsPixel;
85 unsigned long sizeimage;
87 long filesize, pixoff;
88 long bmisize, compression;
91 unsigned long m_bytesRead = 0;
95 fp = fopen (gbmp.name, "rb");
100 rc = fread(&m1, 1, 1, fp);
108 rc = fread(&m2, 1, 1, fp);
110 if ((m1 != 'B') || (m2 != 'M'))
116 rc = fread((long*)&(filesize),4,1,fp); m_bytesRead+=4;
117 if (rc != 1) { fclose(fp); return NULL; }
119 rc = fread((int*)&(res1),2,1,fp); m_bytesRead+=2;
120 if (rc != 1) { fclose(fp); return NULL; }
122 rc = fread((int*)&(res2),2,1,fp); m_bytesRead+=2;
123 if (rc != 1) { fclose(fp); return NULL; }
125 rc = fread((long*)&(pixoff),4,1,fp); m_bytesRead+=4;
126 if (rc != 1) { fclose(fp); return NULL; }
128 rc = fread((long*)&(bmisize),4,1,fp); m_bytesRead+=4;
129 if (rc != 1) { fclose(fp); return NULL; }
131 rc = fread((long *)&(bmWidth),4,1,fp); m_bytesRead+=4;
132 if (rc != 1) { fclose(fp); return NULL; }
134 rc = fread((long*)&(bmHeight),4,1,fp); m_bytesRead+=4;
135 if (rc != 1) { fclose(fp); return NULL; }
137 rc = fread((int*)&(bmPlanes),2,1,fp); m_bytesRead+=2;
138 if (rc != 1) { fclose(fp); return NULL; }
140 rc = fread((int*)&(bmBitsPixel),2,1,fp); m_bytesRead+=2;
141 if (rc != 1) { fclose(fp); return NULL; }
143 rc = fread((long*)&(compression),4,1,fp); m_bytesRead+=4;
144 if (rc != 1) { fclose(fp); return NULL; }
146 rc = fread((long*)&(sizeimage),4,1,fp); m_bytesRead+=4;
147 if (rc != 1) {fclose(fp); return NULL; }
149 rc = fread((long*)&(xscale),4,1,fp); m_bytesRead+=4;
150 if (rc != 1) { fclose(fp); return NULL; }
152 rc = fread((long*)&(yscale),4,1,fp); m_bytesRead+=4;
153 if (rc != 1) { fclose(fp); return NULL; }
155 rc = fread((long*)&(colors),4,1,fp); m_bytesRead+=4;
156 if (rc != 1) { fclose(fp); return NULL; }
158 rc = fread((long*)&(impcol),4,1,fp); m_bytesRead+=4;
159 if (rc != 1) { fclose(fp); return NULL; }
161 if (bmBitsPixel != 8)
163 g_FuncTable.m_pfnMessageBox (g_pWnd, "This is not an 8-bit image. GenSurf can't use it.",
164 "Bitmap", eMB_OK, eMB_ICONWARNING);
170 colors = 1 << bmBitsPixel;
172 if (bmBitsPixel != 24)
175 for (i = 0; i < colors; i++)
177 unsigned char r ,g, b, dummy;
179 rc = fread(&b, 1, 1, fp);
187 rc = fread(&g, 1, 1, fp);
195 rc = fread(&r, 1, 1, fp);
203 rc = fread(&dummy, 1, 1, fp);
213 if ((long)m_bytesRead > pixoff)
219 while ((long)m_bytesRead < pixoff)
222 fread(&dummy,1,1,fp);
229 // set the output params
230 image = (unsigned char*)malloc(w*h);
236 unsigned char* outbuf = image;
240 if (compression == 0) // BI_RGB
242 for (row = 0; row < bmHeight; row++)
244 // which row are we working on?
245 rowOffset = (long unsigned)row*w;
248 // pixels are packed as 1 , 4 or 8 bit vals. need to unpack them
250 unsigned long mask = (1 << bmBitsPixel) - 1;
251 unsigned char inbyte = 0;
253 for (int col=0;col<w;col++)
257 // if we need another byte
261 if (fread(&inbyte,1,1,fp) != 1)
270 // keep track of where we are in the bytes
271 bit_count -= bmBitsPixel;
272 pix = ( inbyte >> bit_count) & mask;
274 // lookup the color from the colormap - stuff it in our buffer
276 *(outbuf + rowOffset + col) = pix;
279 // read DWORD padding
280 while ((m_bytesRead-pixoff)&3)
283 if (fread(&dummy,1,1,fp)!=1)
294 else // compression != 0
297 unsigned char c, c1 = 0, *pp;
301 if (bmBitsPixel == 8)
303 while (row < bmHeight)
311 for (i = 0; i < c; x++, i++)
318 // c==0x00, escape codes
321 if (c == 0x00) // end of line
325 pp = outbuf + row*bmWidth;
329 else if (c == 0x02) // delta
335 pp = outbuf + x + row*bmWidth;
337 else // absolute mode
339 for (i = 0; i < c; x++, i++)
346 getc(fp); // odd length run: read an extra pad byte
351 else if (bmBitsPixel == 4)
353 while (row < bmHeight)
361 for (i = 0; i < c; x++, i++)
363 *pp = (i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f); pp++;
368 // c==0x00, escape codes
371 if (c == 0x00) // end of line
375 pp = outbuf + bmHeight*bmWidth;
379 else if (c == 0x02) // delta
385 pp = outbuf + x + row*bmWidth;
387 else // absolute mode
389 for (i = 0; i < c; x++, i++)
393 *pp = (i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f); pp++;
396 if (((c&3) == 1) || ((c&3) == 2))
397 getc(fp); // odd length run: read an extra pad byte
413 gbmp.colors = OpenBitmapFile ();
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);