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()
31 int O00, O01, O10, O11;
34 unsigned char *colors;
42 for (j = 0; j <= NV; j++) {
43 y = (double) (j * (gbmp.height - 1)) / (double) NV;
46 for (i = 0; i <= NH; i++) {
47 x = (double) (i * (gbmp.width - 1)) / (double) NH;
50 O00 = r0 * gbmp.width + c0;
51 O01 = r0 * gbmp.width + c1;
52 O10 = r1 * gbmp.width + c0;
53 O11 = r1 * gbmp.width + c1;
54 C0 = (double) colors[O00] + (double) (colors[O01] - colors[O00]) * (x - (double) c0);
55 C1 = (double) colors[O10] + (double) (colors[O11] - colors[O10]) * (x - (double) c0);
56 color = (int) (C0 + (C1 - C0) * (y - r0));
58 value = CalculateSnapValue(gbmp.black_value + color * ((gbmp.white_value - gbmp.black_value) / 255.));
63 xyz[i][j].p[1] = value;
67 xyz[i][j].p[0] = value;
70 xyz[i][j].p[2] = value;
76 static unsigned char *OpenBitmapFile()
78 #define INVALID_FORMAT do { \
79 fprintf( stderr,"%s:%d: Error file '%s' is malformed.\n",__FILE__,__LINE__,gbmp.name ); \
91 int32_t filesize, pixoff;
92 int32_t bmisize, compression;
93 int32_t xscale, yscale;
94 int32_t colors, impcol;
95 uint32_t m_bytesRead = 0;
99 fp = fopen(gbmp.name, "rb");
101 fprintf(stderr, "Error: Invalid filename '%s'\n", gbmp.name);
106 rc = fread(&m1, 1, 1, fp);
112 rc = fread(&m2, 1, 1, fp);
114 if ((m1 != 'B') || (m2 != 'M')) {
118 rc = fread((uint32_t * ) & (filesize), 4, 1, fp);
124 rc = fread((uint16_t * ) & (res1), 2, 1, fp);
130 rc = fread((uint16_t * ) & (res2), 2, 1, fp);
136 rc = fread((uint32_t * ) & (pixoff), 4, 1, fp);
142 rc = fread((uint32_t * ) & (bmisize), 4, 1, fp);
148 rc = fread((uint32_t * ) & (bmWidth), 4, 1, fp);
154 rc = fread((uint32_t * ) & (bmHeight), 4, 1, fp);
160 rc = fread((uint16_t * ) & (bmPlanes), 2, 1, fp);
166 rc = fread((uint16_t * ) & (bmBitsPixel), 2, 1, fp);
172 rc = fread((uint32_t * ) & (compression), 4, 1, fp);
178 rc = fread((uint32_t * ) & (sizeimage), 4, 1, fp);
184 rc = fread((uint32_t * ) & (xscale), 4, 1, fp);
190 rc = fread((uint32_t * ) & (yscale), 4, 1, fp);
196 rc = fread((uint32_t * ) & (colors), 4, 1, fp);
202 rc = fread((uint32_t * ) & (impcol), 4, 1, fp);
208 if (bmBitsPixel != 8) {
209 g_FuncTable.m_pfnMessageBox(g_pWnd, "This is not an 8-bit image. GenSurf can't use it.",
210 "Bitmap", eMB_OK, eMB_ICONWARNING);
216 colors = 1 << bmBitsPixel;
219 if (bmBitsPixel != 24) {
221 for (i = 0; i < colors; i++) {
222 unsigned char r, g, b, dummy;
224 rc = fread(&b, 1, 1, fp);
230 rc = fread(&g, 1, 1, fp);
236 rc = fread(&r, 1, 1, fp);
242 rc = fread(&dummy, 1, 1, fp);
250 if ((long) m_bytesRead > pixoff) {
254 while ((long) m_bytesRead < pixoff) {
256 fread(&dummy, 1, 1, fp);
263 // set the output params
264 image = (unsigned char *) malloc(w * h);
267 unsigned char *outbuf = image;
271 if (compression == 0) { // BI_RGB
272 for (row = 0; row < bmHeight; row++) {
273 // which row are we working on?
274 rowOffset = (long unsigned) row * w;
277 // pixels are packed as 1 , 4 or 8 bit vals. need to unpack them
279 unsigned long mask = (1 << bmBitsPixel) - 1;
280 unsigned char inbyte = 0;
282 for (int col = 0; col < w; col++) {
285 // if we need another byte
286 if (bit_count <= 0) {
288 if (fread(&inbyte, 1, 1, fp) != 1) {
295 // keep track of where we are in the bytes
296 bit_count -= bmBitsPixel;
297 pix = (inbyte >> bit_count) & mask;
299 // lookup the color from the colormap - stuff it in our buffer
301 *(outbuf + rowOffset + col) = pix;
304 // read DWORD padding
305 while ((m_bytesRead - pixoff) & 3) {
307 if (fread(&dummy, 1, 1, fp) != 1) {
315 } else // compression != 0
318 unsigned char c, c1 = 0, *pp;
322 if (bmBitsPixel == 8) {
323 while (row < bmHeight) {
329 for (i = 0; i < c; x++, i++) {
334 // c==0x00, escape codes
337 if (c == 0x00) { // end of line
340 pp = outbuf + row * bmWidth;
341 } else if (c == 0x01) {
343 } else if (c == 0x02) { // delta
348 pp = outbuf + x + row * bmWidth;
349 } else // absolute mode
351 for (i = 0; i < c; x++, i++) {
358 getc(fp); // odd length run: read an extra pad byte
363 } else if (bmBitsPixel == 4) {
364 while (row < bmHeight) {
370 for (i = 0; i < c; x++, i++) {
371 *pp = (i & 1) ? (c1 & 0x0f) : ((c1 >> 4) & 0x0f);
375 // c==0x00, escape codes
378 if (c == 0x00) { // end of line
381 pp = outbuf + bmHeight * bmWidth;
382 } else if (c == 0x01) {
384 } else if (c == 0x02) { // delta
389 pp = outbuf + x + row * bmWidth;
390 } else // absolute mode
392 for (i = 0; i < c; x++, i++) {
396 *pp = (i & 1) ? (c1 & 0x0f) : ((c1 >> 4) & 0x0f);
400 if (((c & 3) == 1) || ((c & 3) == 2)) {
401 getc(fp); // odd length run: read an extra pad byte
430 sprintf(Text, "Error opening %s", gbmp.name);
431 g_FuncTable.m_pfnMessageBox(g_pWnd, Text, "Bitmap", eMB_OK, eMB_ICONWARNING);
432 strcpy(gbmp.name, "");
436 gtk_entry_set_text(GTK_ENTRY(g_object_get_data(G_OBJECT(g_pWnd), "bmp_file")), gbmp.name);
437 gtk_widget_set_sensitive(g_object_get_data(G_OBJECT(g_pWnd, "bmp_reload")),
438 strlen(gbmp.name) ? TRUE : FALSE);
443 return (gbmp.colors != NULL);