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 typedef __int32 int32_t;
26 typedef unsigned __int16 uint16_t;
27 typedef unsigned __int8 uint8_t;
28 typedef unsigned __int32 uint32_t;
29 typedef __int16 int16_t;
35 void GenerateBitmapMapping(){
43 unsigned char *colors;
51 for ( j = 0; j <= NV; j++ )
53 y = (double)( j * ( gbmp.height - 1 ) ) / (double)NV;
56 for ( i = 0; i <= NH; i++ )
58 x = (double)( i * ( gbmp.width - 1 ) ) / (double)NH;
61 O00 = r0 * gbmp.width + c0;
62 O01 = r0 * gbmp.width + c1;
63 O10 = r1 * gbmp.width + c0;
64 O11 = r1 * gbmp.width + c1;
65 C0 = (double)colors[O00] + (double)( colors[O01] - colors[O00] ) * ( x - (double)c0 );
66 C1 = (double)colors[O10] + (double)( colors[O11] - colors[O10] ) * ( x - (double)c0 );
67 color = (int)( C0 + ( C1 - C0 ) * ( y - r0 ) );
69 value = CalculateSnapValue( gbmp.black_value + color * ( ( gbmp.white_value - gbmp.black_value ) / 255. ) );
75 xyz[i][j].p[1] = value;
79 xyz[i][j].p[0] = value;
82 xyz[i][j].p[2] = value;
88 static unsigned char* OpenBitmapFile(){
89 #define INVALID_FORMAT do { \
90 fprintf( stderr,"%s:%d: Error file '%s' is malformed.\n",__FILE__,__LINE__,gbmp.name ); \
102 int32_t filesize, pixoff;
103 int32_t bmisize, compression;
104 int32_t xscale, yscale;
105 int32_t colors, impcol;
106 uint32_t m_bytesRead = 0;
107 unsigned char *image;
110 fp = fopen( gbmp.name, "rb" );
112 fprintf( stderr,"Error: Invalid filename '%s'\n",gbmp.name );
117 rc = fread( &m1, 1, 1, fp );
124 rc = fread( &m2, 1, 1, fp );
126 if ( ( m1 != 'B' ) || ( m2 != 'M' ) ) {
130 rc = fread( (uint32_t*)&( filesize ),4,1,fp ); m_bytesRead += 4;
135 rc = fread( (uint16_t*)&( res1 ),2,1,fp ); m_bytesRead += 2;
140 rc = fread( (uint16_t*)&( res2 ),2,1,fp ); m_bytesRead += 2;
145 rc = fread( (uint32_t*)&( pixoff ),4,1,fp ); m_bytesRead += 4;
150 rc = fread( (uint32_t*)&( bmisize ),4,1,fp ); m_bytesRead += 4;
155 rc = fread( (uint32_t *)&( bmWidth ),4,1,fp ); m_bytesRead += 4;
160 rc = fread( (uint32_t*)&( bmHeight ),4,1,fp ); m_bytesRead += 4;
165 rc = fread( (uint16_t*)&( bmPlanes ),2,1,fp ); m_bytesRead += 2;
170 rc = fread( (uint16_t*)&( bmBitsPixel ),2,1,fp ); m_bytesRead += 2;
175 rc = fread( (uint32_t*)&( compression ),4,1,fp ); m_bytesRead += 4;
180 rc = fread( (uint32_t*)&( sizeimage ),4,1,fp ); m_bytesRead += 4;
185 rc = fread( (uint32_t*)&( xscale ),4,1,fp ); m_bytesRead += 4;
190 rc = fread( (uint32_t*)&( yscale ),4,1,fp ); m_bytesRead += 4;
195 rc = fread( (uint32_t*)&( colors ),4,1,fp ); m_bytesRead += 4;
200 rc = fread( (uint32_t*)&( impcol ),4,1,fp ); m_bytesRead += 4;
205 if ( bmBitsPixel != 8 ) {
206 g_FuncTable.m_pfnMessageBox( g_pWnd, "This is not an 8-bit image. GenSurf can't use it.",
207 "Bitmap", MB_ICONEXCLAMATION, NULL );
212 colors = 1 << bmBitsPixel;
215 if ( bmBitsPixel != 24 ) {
217 for ( i = 0; i < colors; i++ )
219 unsigned char r,g, b, dummy;
221 rc = fread( &b, 1, 1, fp );
227 rc = fread( &g, 1, 1, fp );
233 rc = fread( &r, 1, 1, fp );
239 rc = fread( &dummy, 1, 1, fp );
247 if ( (long)m_bytesRead > pixoff ) {
251 while ( (long)m_bytesRead < pixoff )
254 fread( &dummy,1,1,fp );
261 // set the output params
262 image = (unsigned char*)malloc( w * h );
264 if ( image != NULL ) {
265 unsigned char* outbuf = image;
269 if ( compression == 0 ) { // BI_RGB
270 for ( row = 0; row < bmHeight; row++ )
272 // which row are we working on?
273 rowOffset = (long unsigned)row * w;
276 // pixels are packed as 1 , 4 or 8 bit vals. need to unpack them
278 unsigned long mask = ( 1 << bmBitsPixel ) - 1;
279 unsigned char inbyte = 0;
281 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 )
308 if ( fread( &dummy,1,1,fp ) != 1 ) {
317 else // compression != 0
320 unsigned char c, c1 = 0, *pp;
324 if ( bmBitsPixel == 8 ) {
325 while ( row < bmHeight )
332 for ( i = 0; i < c; x++, i++ )
339 // c==0x00, escape codes
342 if ( c == 0x00 ) { // end of line
345 pp = outbuf + row * bmWidth;
347 else if ( c == 0x01 ) {
350 else if ( c == 0x02 ) { // delta
355 pp = outbuf + x + row * bmWidth;
357 else // absolute mode
359 for ( i = 0; i < c; x++, i++ )
366 getc( fp ); // odd length run: read an extra pad byte
372 else if ( bmBitsPixel == 4 ) {
373 while ( row < bmHeight )
380 for ( i = 0; i < c; x++, i++ )
382 *pp = ( i & 1 ) ? ( c1 & 0x0f ) : ( ( c1 >> 4 ) & 0x0f ); pp++;
387 // c==0x00, escape codes
390 if ( c == 0x00 ) { // end of line
393 pp = outbuf + bmHeight * bmWidth;
395 else if ( c == 0x01 ) {
398 else if ( c == 0x02 ) { // delta
403 pp = outbuf + x + row * bmWidth;
405 else // absolute mode
407 for ( i = 0; i < c; x++, i++ )
409 if ( ( i & 1 ) == 0 ) {
412 *pp = ( i & 1 ) ? ( c1 & 0x0f ) : ( ( c1 >> 4 ) & 0x0f ); pp++;
415 if ( ( ( c & 3 ) == 1 ) || ( ( c & 3 ) == 2 ) ) {
416 getc( fp ); // odd length run: read an extra pad byte
441 if ( !gbmp.colors ) {
442 g_print( "failed to load file gbmp %s\n",gbmp.name );
445 sprintf( Text, "Error opening %s", gbmp.name );
446 g_FuncTable.m_pfnMessageBox( g_pWnd, Text, "Bitmap", MB_ICONEXCLAMATION, NULL );
447 strcpy( gbmp.name, "" );
451 gtk_entry_set_text( GTK_ENTRY( g_object_get_data( G_OBJECT( g_pWnd ), "bmp_file" ) ), gbmp.name );
452 gtk_widget_set_sensitive( GTK_WIDGET( g_object_get_data( G_OBJECT( g_pWnd ), "bmp_reload" ) ),
453 strlen( gbmp.name ) ? TRUE : FALSE );
455 UpdatePreview( true );
458 return ( gbmp.colors != NULL );