]> de.git.xonotic.org Git - xonotic/netradiant.git/blobdiff - contrib/gtkgensurf/bitmap.cpp
GTK: wrap GTK_WIDGET
[xonotic/netradiant.git] / contrib / gtkgensurf / bitmap.cpp
index 93dcd2820d56413f454b33e6bf857326f4d52eed..68eea5c2c43ee611a0bec432075a855bf2dc8cf6 100644 (file)
-/*\r
-GenSurf plugin for GtkRadiant\r
-Copyright (C) 2001 David Hyde, Loki software and qeradiant.com\r
-\r
-This library is free software; you can redistribute it and/or\r
-modify it under the terms of the GNU Lesser General Public\r
-License as published by the Free Software Foundation; either\r
-version 2.1 of the License, or (at your option) any later version.\r
-\r
-This library is distributed in the hope that it will be useful,\r
-but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
-Lesser General Public License for more details.\r
-\r
-You should have received a copy of the GNU Lesser General Public\r
-License along with this library; if not, write to the Free Software\r
-Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
-*/\r
-\r
-#include <stdio.h>\r
-#include <stdlib.h>\r
-#include <math.h>\r
-#include "gensurf.h"\r
-\r
-void GenerateBitmapMapping ()\r
-{\r
-  double              value;\r
-  double              C0, C1;\r
-  double              x, y;\r
-  int                 i, j;\r
-  int                 O00,O01,O10,O11;\r
-  int                 r0, r1, c0, c1;\r
-  int                 color;\r
-  unsigned char       *colors;\r
-\r
-  if (!gbmp.colors)\r
-    return;\r
-\r
-  colors = gbmp.colors;\r
-\r
-  for (j=0; j<=NV; j++)\r
-  {\r
-    y  = (double)(j*(gbmp.height-1))/(double)NV;\r
-    r0 = (int)floor(y);\r
-    r1 = (int)ceil(y);\r
-    for (i=0; i<=NH; i++)\r
-    {\r
-      x = (double)(i*(gbmp.width-1))/(double)NH;\r
-      c0 = (int)floor(x);\r
-      c1 = (int)ceil(x);\r
-      O00 = r0*gbmp.width + c0;\r
-      O01 = r0*gbmp.width + c1;\r
-      O10 = r1*gbmp.width + c0;\r
-      O11 = r1*gbmp.width + c1;\r
-      C0 = (double)colors[O00] + (double)(colors[O01]-colors[O00])*(x-(double)c0);\r
-      C1 = (double)colors[O10] + (double)(colors[O11]-colors[O10])*(x-(double)c0);\r
-      color = (int)(C0 + (C1-C0)*(y-r0));\r
-\r
-      value = CalculateSnapValue(gbmp.black_value + color*((gbmp.white_value-gbmp.black_value)/255.));\r
-\r
-      switch(Plane)\r
-      {\r
-      case PLANE_XZ0:\r
-      case PLANE_XZ1:\r
-        xyz[i][j].p[1] = value;\r
-        break;\r
-      case PLANE_YZ0:\r
-      case PLANE_YZ1:\r
-        xyz[i][j].p[0] = value;\r
-        break;\r
-      default:\r
-        xyz[i][j].p[2] = value;\r
-      }\r
-    }\r
-  }\r
-}\r
-\r
-static unsigned char* OpenBitmapFile ()\r
-{\r
-  int bmWidth;\r
-  int bmHeight;\r
-  unsigned char bmPlanes;\r
-  unsigned char bmBitsPixel;\r
-  unsigned char m1,m2;\r
-  unsigned long sizeimage;\r
-  short res1,res2;\r
-  long filesize, pixoff;\r
-  long bmisize, compression;\r
-  long xscale, yscale;\r
-  long colors, impcol;\r
-  unsigned long m_bytesRead = 0;\r
-  unsigned char *image;\r
-  FILE *fp;\r
-\r
-  fp = fopen (gbmp.name, "rb");\r
-  if (fp == NULL)\r
-    return NULL;\r
-\r
-  long rc;\r
-  rc = fread(&m1, 1, 1, fp);\r
-  m_bytesRead++;\r
-  if (rc == -1)\r
-  {\r
-    fclose(fp);\r
-    return NULL;\r
-  }\r
-\r
-  rc = fread(&m2, 1, 1, fp);\r
-  m_bytesRead++;\r
-  if ((m1 != 'B') || (m2 != 'M'))\r
-  {\r
-    fclose(fp);\r
-    return NULL;\r
-  }\r
-\r
-  rc = fread((long*)&(filesize),4,1,fp); m_bytesRead+=4;\r
-  if (rc != 1) { fclose(fp); return NULL; }\r
-\r
-  rc = fread((int*)&(res1),2,1,fp); m_bytesRead+=2;\r
-  if (rc != 1) { fclose(fp); return NULL; }\r
-\r
-  rc = fread((int*)&(res2),2,1,fp); m_bytesRead+=2;\r
-  if (rc != 1) { fclose(fp); return NULL; }\r
-\r
-  rc = fread((long*)&(pixoff),4,1,fp); m_bytesRead+=4;\r
-  if (rc != 1) { fclose(fp); return NULL; }\r
-\r
-  rc = fread((long*)&(bmisize),4,1,fp); m_bytesRead+=4;\r
-  if (rc != 1) { fclose(fp); return NULL; }\r
-\r
-  rc = fread((long  *)&(bmWidth),4,1,fp); m_bytesRead+=4;\r
-  if (rc != 1) { fclose(fp); return NULL; }\r
-\r
-  rc = fread((long*)&(bmHeight),4,1,fp); m_bytesRead+=4;\r
-  if (rc != 1) { fclose(fp); return NULL; }\r
-\r
-  rc = fread((int*)&(bmPlanes),2,1,fp); m_bytesRead+=2;\r
-  if (rc != 1) { fclose(fp); return NULL; }\r
-\r
-  rc = fread((int*)&(bmBitsPixel),2,1,fp); m_bytesRead+=2;\r
-  if (rc != 1) { fclose(fp); return NULL; }\r
-\r
-  rc = fread((long*)&(compression),4,1,fp); m_bytesRead+=4;\r
-  if (rc != 1) { fclose(fp); return NULL; }\r
-\r
-  rc = fread((long*)&(sizeimage),4,1,fp); m_bytesRead+=4;\r
-  if (rc != 1) {fclose(fp); return NULL; }\r
-\r
-  rc = fread((long*)&(xscale),4,1,fp); m_bytesRead+=4;\r
-  if (rc != 1) { fclose(fp); return NULL; }\r
-\r
-  rc = fread((long*)&(yscale),4,1,fp); m_bytesRead+=4;\r
-  if (rc != 1) { fclose(fp); return NULL; }\r
-\r
-  rc = fread((long*)&(colors),4,1,fp); m_bytesRead+=4;\r
-  if (rc != 1) { fclose(fp); return NULL; }\r
-\r
-  rc = fread((long*)&(impcol),4,1,fp); m_bytesRead+=4;\r
-  if (rc != 1) { fclose(fp); return NULL; }\r
-\r
-  if (bmBitsPixel != 8)\r
-  {\r
-    g_FuncTable.m_pfnMessageBox (g_pWnd, "This is not an 8-bit image. GenSurf can't use it.",\r
-                                 "Bitmap", MB_ICONEXCLAMATION);\r
-    fclose(fp);\r
-    return NULL; \r
-  }\r
-\r
-  if (colors == 0)\r
-    colors = 1 << bmBitsPixel;\r
-\r
-  if (bmBitsPixel != 24)\r
-  {\r
-    int i;\r
-    for (i = 0; i < colors; i++)\r
-    {\r
-      unsigned char r ,g, b, dummy;\r
-\r
-      rc = fread(&b, 1, 1, fp);\r
-      m_bytesRead++;\r
-      if (rc!=1)\r
-      {\r
-        fclose(fp);\r
-        return NULL;\r
-      }\r
-\r
-      rc = fread(&g, 1, 1, fp); \r
-      m_bytesRead++;\r
-      if (rc!=1)\r
-      {\r
-        fclose(fp);\r
-        return NULL;\r
-      }\r
-\r
-      rc = fread(&r, 1, 1, fp); \r
-      m_bytesRead++;\r
-      if (rc != 1)\r
-      {\r
-        fclose(fp);\r
-        return NULL;\r
-      }\r
-\r
-      rc = fread(&dummy, 1, 1, fp); \r
-      m_bytesRead++;\r
-      if (rc != 1)\r
-      {\r
-        fclose(fp);\r
-        return NULL;\r
-      }\r
-    }\r
-  }\r
-\r
-  if ((long)m_bytesRead > pixoff)\r
-  {\r
-    fclose(fp);\r
-    return NULL;\r
-  }\r
-\r
-  while ((long)m_bytesRead < pixoff)\r
-  {\r
-    char dummy;\r
-    fread(&dummy,1,1,fp);\r
-    m_bytesRead++;\r
-  }\r
-\r
-  int w = bmWidth;\r
-  int h = bmHeight;\r
-\r
-  // set the output params\r
-  image = (unsigned char*)malloc(w*h);\r
-\r
-  if (image != NULL) \r
-  {\r
-    gbmp.width = w;\r
-    gbmp.height = h;\r
-    unsigned char* outbuf = image;\r
-    long row = 0;\r
-    long rowOffset = 0;\r
-\r
-    if (compression == 0) // BI_RGB\r
-    {\r
-      for (row = 0; row < bmHeight; row++)\r
-      {\r
-        // which row are we working on?\r
-        rowOffset = (long unsigned)row*w;                                                    \r
-\r
-        {\r
-          // pixels are packed as 1 , 4 or 8 bit vals. need to unpack them\r
-          int bit_count = 0;\r
-          unsigned long mask = (1 << bmBitsPixel) - 1;\r
-          unsigned char inbyte = 0;\r
-\r
-          for (int col=0;col<w;col++)\r
-          {\r
-            int pix = 0;\r
-\r
-            // if we need another byte\r
-            if (bit_count <= 0)\r
-            {\r
-              bit_count = 8;\r
-              if (fread(&inbyte,1,1,fp) != 1)\r
-              {\r
-                free(image);\r
-                fclose(fp);\r
-                return NULL;\r
-              }\r
-              m_bytesRead++;\r
-            }\r
-\r
-            // keep track of where we are in the bytes\r
-            bit_count -= bmBitsPixel;\r
-            pix = ( inbyte >> bit_count) & mask;\r
-\r
-            // lookup the color from the colormap - stuff it in our buffer\r
-            // swap red and blue\r
-            *(outbuf + rowOffset + col) = pix;\r
-          }\r
-\r
-          // read DWORD padding\r
-          while ((m_bytesRead-pixoff)&3)\r
-          {\r
-            char dummy;\r
-            if (fread(&dummy,1,1,fp)!=1)\r
-            {\r
-              free(image);\r
-              fclose(fp);\r
-              return NULL;\r
-            }\r
-            m_bytesRead++;\r
-          }\r
-        }\r
-      }\r
-    }\r
-    else    // compression != 0\r
-    {\r
-      int i, x = 0;\r
-      unsigned char c, c1 = 0, *pp;\r
-      row = 0;\r
-      pp = outbuf;\r
-\r
-      if (bmBitsPixel == 8)\r
-      {\r
-        while (row < bmHeight)\r
-        {\r
-          c = getc(fp);\r
-\r
-          if (c)\r
-          {\r
-            // encoded mode\r
-            c1 = getc(fp);\r
-            for (i = 0; i < c; x++, i++)\r
-            {\r
-              *pp = c1; pp++;\r
-            }\r
-          }\r
-          else\r
-          {\r
-            // c==0x00,  escape codes\r
-            c = getc(fp);\r
-\r
-            if (c == 0x00) // end of line\r
-            {\r
-              row++;\r
-              x = 0;\r
-              pp = outbuf + row*bmWidth;\r
-            }\r
-            else if (c == 0x01)\r
-              break; // end of pic\r
-            else if (c == 0x02) // delta\r
-            {\r
-              c = getc(fp);\r
-              x += c;\r
-              c = getc(fp);\r
-              row += c;\r
-              pp = outbuf + x + row*bmWidth;\r
-            }\r
-            else // absolute mode\r
-            {\r
-              for (i = 0; i < c; x++, i++)\r
-              {\r
-                c1 = getc(fp);\r
-                *pp = c1; pp++;\r
-              }\r
-\r
-              if (c & 1)\r
-                getc(fp); // odd length run: read an extra pad byte\r
-            }\r
-          }\r
-        }\r
-      }\r
-      else if (bmBitsPixel == 4)\r
-      {\r
-        while (row < bmHeight)\r
-        {\r
-          c = getc(fp);\r
-\r
-          if (c)\r
-          {\r
-            // encoded mode\r
-            c1 = getc(fp);\r
-            for (i = 0; i < c; x++, i++)\r
-            {\r
-              *pp = (i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f); pp++;\r
-            }\r
-          }\r
-          else\r
-          {\r
-            // c==0x00,  escape codes\r
-            c = getc(fp);\r
-\r
-            if (c == 0x00) // end of line\r
-            {\r
-              row++;\r
-              x = 0;\r
-              pp = outbuf + bmHeight*bmWidth;\r
-            }\r
-            else if (c == 0x01)\r
-              break; // end of pic\r
-            else if (c == 0x02) // delta\r
-            {\r
-              c = getc(fp);\r
-              x += c;\r
-              c = getc(fp);\r
-              row += c;\r
-              pp = outbuf + x + row*bmWidth;\r
-            }\r
-            else // absolute mode\r
-            {\r
-              for (i = 0; i < c; x++, i++)\r
-              {\r
-                if ((i&1) == 0)\r
-                  c1 = getc(fp);\r
-                *pp = (i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f); pp++;\r
-              }\r
-\r
-              if (((c&3) == 1) || ((c&3) == 2))\r
-                getc(fp); // odd length run: read an extra pad byte\r
-            }\r
-          }\r
-        }\r
-      }\r
-    }\r
-  }\r
-  fclose(fp);\r
-  return image;\r
-}\r
-\r
-bool OpenBitmap ()\r
-{\r
-  if (gbmp.colors)\r
-    free (gbmp.colors);\r
-\r
-  gbmp.colors = OpenBitmapFile ();\r
-\r
-  if (!gbmp.colors)\r
-  {\r
-    char Text[256];\r
-\r
-    sprintf (Text, "Error opening %s", gbmp.name);\r
-    g_FuncTable.m_pfnMessageBox (g_pWnd, Text, "Bitmap", MB_ICONEXCLAMATION);\r
-    strcpy (gbmp.name, "");\r
-  }\r
-\r
-  if (g_pWnd)\r
-  {\r
-    gtk_entry_set_text (GTK_ENTRY (g_object_get_data (G_OBJECT (g_pWnd), "bmp_file")), gbmp.name);\r
-    gtk_widget_set_sensitive (GTK_WIDGET (g_object_get_data (G_OBJECT (g_pWnd), "bmp_reload")),\r
-                              strlen (gbmp.name) ? TRUE : FALSE);\r
-\r
-    UpdatePreview (true);\r
-  }\r
-\r
-  return (gbmp.colors != NULL);\r
-}\r
+/*
+   GenSurf plugin for GtkRadiant
+   Copyright (C) 2001 David Hyde, Loki software and qeradiant.com
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with this library; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <math.h>
+#include "gensurf.h"
+
+void GenerateBitmapMapping(){
+       double value;
+       double C0, C1;
+       double x, y;
+       int i, j;
+       int O00,O01,O10,O11;
+       int r0, r1, c0, c1;
+       int color;
+       unsigned char       *colors;
+
+       if ( !gbmp.colors ) {
+               return;
+       }
+
+       colors = gbmp.colors;
+
+       for ( j = 0; j <= NV; j++ )
+       {
+               y  = (double)( j * ( gbmp.height - 1 ) ) / (double)NV;
+               r0 = (int)floor( y );
+               r1 = (int)ceil( y );
+               for ( i = 0; i <= NH; i++ )
+               {
+                       x = (double)( i * ( gbmp.width - 1 ) ) / (double)NH;
+                       c0 = (int)floor( x );
+                       c1 = (int)ceil( x );
+                       O00 = r0 * gbmp.width + c0;
+                       O01 = r0 * gbmp.width + c1;
+                       O10 = r1 * gbmp.width + c0;
+                       O11 = r1 * gbmp.width + c1;
+                       C0 = (double)colors[O00] + (double)( colors[O01] - colors[O00] ) * ( x - (double)c0 );
+                       C1 = (double)colors[O10] + (double)( colors[O11] - colors[O10] ) * ( x - (double)c0 );
+                       color = (int)( C0 + ( C1 - C0 ) * ( y - r0 ) );
+
+                       value = CalculateSnapValue( gbmp.black_value + color * ( ( gbmp.white_value - gbmp.black_value ) / 255. ) );
+
+                       switch ( Plane )
+                       {
+                       case PLANE_XZ0:
+                       case PLANE_XZ1:
+                               xyz[i][j].p[1] = value;
+                               break;
+                       case PLANE_YZ0:
+                       case PLANE_YZ1:
+                               xyz[i][j].p[0] = value;
+                               break;
+                       default:
+                               xyz[i][j].p[2] = value;
+                       }
+               }
+       }
+}
+
+static unsigned char* OpenBitmapFile(){
+#define INVALID_FORMAT do {    \
+               fprintf( stderr,"%s:%d: Error file '%s' is malformed.\n",__FILE__,__LINE__,gbmp.name ); \
+               fclose( fp ); \
+               return NULL; \
+} while ( 0 );
+
+       int32_t bmWidth;
+       int32_t bmHeight;
+       uint16_t bmPlanes;
+       uint16_t bmBitsPixel;
+       uint8_t m1,m2;
+       uint32_t sizeimage;
+       int16_t res1,res2;
+       int32_t filesize, pixoff;
+       int32_t bmisize, compression;
+       int32_t xscale, yscale;
+       int32_t colors, impcol;
+       uint32_t m_bytesRead = 0;
+       unsigned char *image;
+       FILE *fp;
+
+       fp = fopen( gbmp.name, "rb" );
+       if ( fp == NULL ) {
+               fprintf( stderr,"Error: Invalid filename '%s'\n",gbmp.name );
+               return NULL;
+       }
+
+       long rc;
+       rc = fread( &m1, 1, 1, fp );
+       m_bytesRead++;
+       if ( rc == -1 ) {
+               INVALID_FORMAT;
+       }
+
+       rc = fread( &m2, 1, 1, fp );
+       m_bytesRead++;
+       if ( ( m1 != 'B' ) || ( m2 != 'M' ) ) {
+               INVALID_FORMAT;
+       }
+
+       rc = fread( (uint32_t*)&( filesize ),4,1,fp ); m_bytesRead += 4;
+       if ( rc != 1 ) {
+               INVALID_FORMAT;
+       }
+
+       rc = fread( (uint16_t*)&( res1 ),2,1,fp ); m_bytesRead += 2;
+       if ( rc != 1 ) {
+               INVALID_FORMAT;
+       }
+
+       rc = fread( (uint16_t*)&( res2 ),2,1,fp ); m_bytesRead += 2;
+       if ( rc != 1 ) {
+               INVALID_FORMAT;
+       }
+
+       rc = fread( (uint32_t*)&( pixoff ),4,1,fp ); m_bytesRead += 4;
+       if ( rc != 1 ) {
+               INVALID_FORMAT;
+       }
+
+       rc = fread( (uint32_t*)&( bmisize ),4,1,fp ); m_bytesRead += 4;
+       if ( rc != 1 ) {
+               INVALID_FORMAT;
+       }
+
+       rc = fread( (uint32_t  *)&( bmWidth ),4,1,fp ); m_bytesRead += 4;
+       if ( rc != 1 ) {
+               INVALID_FORMAT;
+       }
+
+       rc = fread( (uint32_t*)&( bmHeight ),4,1,fp ); m_bytesRead += 4;
+       if ( rc != 1 ) {
+               INVALID_FORMAT;
+       }
+
+       rc = fread( (uint16_t*)&( bmPlanes ),2,1,fp ); m_bytesRead += 2;
+       if ( rc != 1 ) {
+               INVALID_FORMAT;
+       }
+
+       rc = fread( (uint16_t*)&( bmBitsPixel ),2,1,fp ); m_bytesRead += 2;
+       if ( rc != 1 ) {
+               INVALID_FORMAT;
+       }
+
+       rc = fread( (uint32_t*)&( compression ),4,1,fp ); m_bytesRead += 4;
+       if ( rc != 1 ) {
+               INVALID_FORMAT;
+       }
+
+       rc = fread( (uint32_t*)&( sizeimage ),4,1,fp ); m_bytesRead += 4;
+       if ( rc != 1 ) {
+               INVALID_FORMAT;
+       }
+
+       rc = fread( (uint32_t*)&( xscale ),4,1,fp ); m_bytesRead += 4;
+       if ( rc != 1 ) {
+               INVALID_FORMAT;
+       }
+
+       rc = fread( (uint32_t*)&( yscale ),4,1,fp ); m_bytesRead += 4;
+       if ( rc != 1 ) {
+               INVALID_FORMAT;
+       }
+
+       rc = fread( (uint32_t*)&( colors ),4,1,fp ); m_bytesRead += 4;
+       if ( rc != 1 ) {
+               INVALID_FORMAT;
+       }
+
+       rc = fread( (uint32_t*)&( impcol ),4,1,fp ); m_bytesRead += 4;
+       if ( rc != 1 ) {
+               INVALID_FORMAT;
+       }
+
+       if ( bmBitsPixel != 8 ) {
+               g_FuncTable.m_pfnMessageBox( g_pWnd, "This is not an 8-bit image. GenSurf can't use it.",
+                                                                        "Bitmap", eMB_OK, eMB_ICONWARNING );
+               fclose( fp );
+               return NULL;
+       }
+
+       if ( colors == 0 ) {
+               colors = 1 << bmBitsPixel;
+       }
+
+       if ( bmBitsPixel != 24 ) {
+               int i;
+               for ( i = 0; i < colors; i++ )
+               {
+                       unsigned char r,g, b, dummy;
+
+                       rc = fread( &b, 1, 1, fp );
+                       m_bytesRead++;
+                       if ( rc != 1 ) {
+                               INVALID_FORMAT;
+                       }
+
+                       rc = fread( &g, 1, 1, fp );
+                       m_bytesRead++;
+                       if ( rc != 1 ) {
+                               INVALID_FORMAT;
+                       }
+
+                       rc = fread( &r, 1, 1, fp );
+                       m_bytesRead++;
+                       if ( rc != 1 ) {
+                               INVALID_FORMAT;
+                       }
+
+                       rc = fread( &dummy, 1, 1, fp );
+                       m_bytesRead++;
+                       if ( rc != 1 ) {
+                               INVALID_FORMAT;
+                       }
+               }
+       }
+
+       if ( (long)m_bytesRead > pixoff ) {
+               INVALID_FORMAT;
+       }
+
+       while ( (long)m_bytesRead < pixoff )
+       {
+               char dummy;
+               fread( &dummy,1,1,fp );
+               m_bytesRead++;
+       }
+
+       int w = bmWidth;
+       int h = bmHeight;
+
+       // set the output params
+       image = (unsigned char*)malloc( w * h );
+
+       if ( image != NULL ) {
+               unsigned char* outbuf = image;
+               long row = 0;
+               long rowOffset = 0;
+
+               if ( compression == 0 ) { // BI_RGB
+                       for ( row = 0; row < bmHeight; row++ )
+                       {
+                               // which row are we working on?
+                               rowOffset = (long unsigned)row * w;
+
+                               {
+                                       // pixels are packed as 1 , 4 or 8 bit vals. need to unpack them
+                                       int bit_count = 0;
+                                       unsigned long mask = ( 1 << bmBitsPixel ) - 1;
+                                       unsigned char inbyte = 0;
+
+                                       for ( int col = 0; col < w; col++ )
+                                       {
+                                               int pix = 0;
+
+                                               // if we need another byte
+                                               if ( bit_count <= 0 ) {
+                                                       bit_count = 8;
+                                                       if ( fread( &inbyte,1,1,fp ) != 1 ) {
+                                                               free( image );
+                                                               INVALID_FORMAT;
+                                                       }
+                                                       m_bytesRead++;
+                                               }
+
+                                               // keep track of where we are in the bytes
+                                               bit_count -= bmBitsPixel;
+                                               pix = ( inbyte >> bit_count ) & mask;
+
+                                               // lookup the color from the colormap - stuff it in our buffer
+                                               // swap red and blue
+                                               *( outbuf + rowOffset + col ) = pix;
+                                       }
+
+                                       // read DWORD padding
+                                       while ( ( m_bytesRead - pixoff ) & 3 )
+                                       {
+                                               char dummy;
+                                               if ( fread( &dummy,1,1,fp ) != 1 ) {
+                                                       free( image );
+                                                       INVALID_FORMAT;
+                                               }
+                                               m_bytesRead++;
+                                       }
+                               }
+                       }
+               }
+               else // compression != 0
+               {
+                       int i, x = 0;
+                       unsigned char c, c1 = 0, *pp;
+                       row = 0;
+                       pp = outbuf;
+
+                       if ( bmBitsPixel == 8 ) {
+                               while ( row < bmHeight )
+                               {
+                                       c = getc( fp );
+
+                                       if ( c ) {
+                                               // encoded mode
+                                               c1 = getc( fp );
+                                               for ( i = 0; i < c; x++, i++ )
+                                               {
+                                                       *pp = c1; pp++;
+                                               }
+                                       }
+                                       else
+                                       {
+                                               // c==0x00,  escape codes
+                                               c = getc( fp );
+
+                                               if ( c == 0x00 ) { // end of line
+                                                       row++;
+                                                       x = 0;
+                                                       pp = outbuf + row * bmWidth;
+                                               }
+                                               else if ( c == 0x01 ) {
+                                                       break; // end of pic
+                                               }
+                                               else if ( c == 0x02 ) { // delta
+                                                       c = getc( fp );
+                                                       x += c;
+                                                       c = getc( fp );
+                                                       row += c;
+                                                       pp = outbuf + x + row * bmWidth;
+                                               }
+                                               else // absolute mode
+                                               {
+                                                       for ( i = 0; i < c; x++, i++ )
+                                                       {
+                                                               c1 = getc( fp );
+                                                               *pp = c1; pp++;
+                                                       }
+
+                                                       if ( c & 1 ) {
+                                                               getc( fp ); // odd length run: read an extra pad byte
+                                                       }
+                                               }
+                                       }
+                               }
+                       }
+                       else if ( bmBitsPixel == 4 ) {
+                               while ( row < bmHeight )
+                               {
+                                       c = getc( fp );
+
+                                       if ( c ) {
+                                               // encoded mode
+                                               c1 = getc( fp );
+                                               for ( i = 0; i < c; x++, i++ )
+                                               {
+                                                       *pp = ( i & 1 ) ? ( c1 & 0x0f ) : ( ( c1 >> 4 ) & 0x0f ); pp++;
+                                               }
+                                       }
+                                       else
+                                       {
+                                               // c==0x00,  escape codes
+                                               c = getc( fp );
+
+                                               if ( c == 0x00 ) { // end of line
+                                                       row++;
+                                                       x = 0;
+                                                       pp = outbuf + bmHeight * bmWidth;
+                                               }
+                                               else if ( c == 0x01 ) {
+                                                       break; // end of pic
+                                               }
+                                               else if ( c == 0x02 ) { // delta
+                                                       c = getc( fp );
+                                                       x += c;
+                                                       c = getc( fp );
+                                                       row += c;
+                                                       pp = outbuf + x + row * bmWidth;
+                                               }
+                                               else // absolute mode
+                                               {
+                                                       for ( i = 0; i < c; x++, i++ )
+                                                       {
+                                                               if ( ( i & 1 ) == 0 ) {
+                                                                       c1 = getc( fp );
+                                                               }
+                                                               *pp = ( i & 1 ) ? ( c1 & 0x0f ) : ( ( c1 >> 4 ) & 0x0f ); pp++;
+                                                       }
+
+                                                       if ( ( ( c & 3 ) == 1 ) || ( ( c & 3 ) == 2 ) ) {
+                                                               getc( fp ); // odd length run: read an extra pad byte
+                                                       }
+                                               }
+                                       }
+                               }
+                       }
+               }
+       }
+       fclose( fp );
+
+       gbmp.width = w;
+       gbmp.height = h;
+       if ( gbmp.colors ) {
+               free( gbmp.colors );
+       }
+       gbmp.colors = image;
+       return image;
+
+
+}
+
+bool OpenBitmap(){
+
+       OpenBitmapFile();
+
+       if ( !gbmp.colors ) {
+               char Text[256];
+
+               sprintf( Text, "Error opening %s", gbmp.name );
+               g_FuncTable.m_pfnMessageBox( g_pWnd, Text, "Bitmap", eMB_OK, eMB_ICONWARNING );
+               strcpy( gbmp.name, "" );
+       }
+
+       if ( g_pWnd ) {
+               gtk_entry_set_text( GTK_ENTRY( g_object_get_data( G_OBJECT( g_pWnd ), "bmp_file" ) ), gbmp.name );
+               gtk_widget_set_sensitive( g_object_get_data( G_OBJECT( g_pWnd , "bmp_reload" ) ),
+                                                                 strlen( gbmp.name ) ? TRUE : FALSE );
+
+               UpdatePreview( true );
+       }
+
+       return ( gbmp.colors != NULL );
+}