Filter:   InfoImg
download fl_bmp.cxx
Language: C++
Copyright: Copyright 1998-2004 by Bill Spitzak and others. Copyright 2000. Copyright 1997-2003 by Easy Software Products.
LOC: 372
Project Info
Fast Light Tool Kit(fltk)
Server: SourceForge
Type: cvs
...Forge\f\fltk\fltk\fltk\src\
   allfiles.xbm
   ARRAY.h
   browser_minus.xbm
   browser_plus.xbm
   cmap.cxx
   CycleButton.cxx
   d1.xbm
   d1_mask.xbm
   dlload_osx.cxx
   dump_compose.c
   ew.xbm
   ew_mask.xbm
   fastarrow.h
   filename_absolute.cxx
   filename_expand.cxx
   filename_ext.cxx
   filename_isdir.cxx
   filename_list.cxx
   filename_match.cxx
   filename_setext.cxx
   Fl.cxx
   Fl_abort.cxx
   Fl_add_idle.cxx
   Fl_Adjuster.cxx
   Fl_Align_Group.cxx
   fl_arc.cxx
   fl_arci.cxx
   Fl_arg.cxx
   fl_ask.cxx
   Fl_Bar.cxx
   Fl_Bitmap.cxx
   fl_bmp.cxx
   Fl_Box.cxx
   Fl_Boxtype.cxx
   Fl_Browser.cxx
   Fl_Browser_load.cxx
   Fl_Button.cxx
   fl_call_main.c
   Fl_Check_Button.cxx
   Fl_Choice.cxx
   fl_clip.cxx
   Fl_Clock.cxx
   fl_cmap.h
   fl_color.cxx
   Fl_Color_Chooser.cxx
   fl_color_mac.cxx
   fl_color_win32.cxx
   fl_color_x.cxx
   Fl_compose.cxx
   fl_cursor.cxx
   fl_curve.cxx
   Fl_Dial.cxx
   fl_diamond_box.cxx
   Fl_display.cxx
   fl_dnd.cxx
   fl_dnd_mac.cxx
   fl_dnd_win32.cxx
   fl_dnd_x.cxx
   fl_draw.cxx
   fl_draw_image.cxx
   fl_draw_image_mac.cxx
   fl_draw_image_win32.cxx
   fl_draw_image_x.cxx
   fl_draw_pixmap.cxx
   fl_engraved_label.cxx
   fl_file_chooser.cxx
   Fl_FileBrowser.cxx
   Fl_FileChooser.cxx
   Fl_FileChooser.fl
   Fl_FileChooser2.cxx
   Fl_FileIcon.cxx
   Fl_FileIcon2.cxx
   Fl_FileInput.cxx
   Fl_Float_Input.cxx
   fl_font.cxx
   fl_font_mac.cxx
   fl_font_win32.cxx
   fl_font_x.cxx
   fl_font_xft.cxx
   Fl_get_key.cxx
   Fl_get_key_mac.cxx
   Fl_get_key_win32.cxx
   fl_gif.cxx
   fl_glyph.cxx
   Fl_Group.cxx
   Fl_Help_Dialog.cxx
   Fl_Help_View.cxx
   Fl_Highlight_Button.cxx
   Fl_Image.cxx
   Fl_Input.cxx
   Fl_Input_Browser.cxx
   Fl_Item.cxx
   Fl_key_name.cxx
   fl_labeltype.cxx
   Fl_Light_Button.cxx
   fl_list_fonts.cxx
   fl_list_fonts_mac.cxx
   fl_list_fonts_win32.cxx
   fl_list_fonts_x.cxx
   fl_load_plugin.cxx
   Fl_lock.cxx
   Fl_mac.cxx
   Fl_Menu.cxx
   Fl_Menu_.cxx
   Fl_Menu_add.cxx
   Fl_Menu_Bar.cxx
   Fl_Menu_Button.cxx
   Fl_Menu_global.cxx
   Fl_Menu_Item.cxx
   Fl_Menu_Window.cxx
   Fl_Multi_Image.cxx
   Fl_Numeric_Input.cxx
   Fl_Output.cxx
   fl_oval_box.cxx
   fl_overlay.cxx
   fl_overlay_visual.cxx
   Fl_own_colormap.cxx
   Fl_Pack.cxx
   Fl_Pixmap.cxx
   fl_plastic_box.cxx
   Fl_ProgressBar.cxx
   Fl_Radio_Button.cxx
   fl_rect.cxx
   Fl_Repeat_Button.cxx
   Fl_Return_Button.cxx
   fl_rgb.cxx
   Fl_RGB_Image.cxx
   Fl_Roller.cxx
   fl_round_box.cxx
   fl_rounded_box.cxx
   Fl_Scroll.cxx
   fl_scroll_area.cxx
   Fl_Scrollbar.cxx
   fl_set_fonts_mac.cxx
   fl_shadow_box.cxx
   Fl_Shaped_Window.cxx
   Fl_Shared_Image.cxx
   fl_show_colormap.cxx
   Fl_Single_Window.cxx
   Fl_Slider.cxx
   Fl_String_List.cxx
   Fl_Style.cxx
   Fl_Style_Set.cxx
   fl_symbols.cxx
   Fl_Tabs.cxx
   Fl_Text_Buffer.cxx
   Fl_Text_Display.cxx
   Fl_Text_Editor.cxx
   Fl_Tile.cxx
   Fl_Tiled_Image.cxx
   Fl_Tooltip.cxx
   Fl_Valuator.cxx
   Fl_Value_Input.cxx
   Fl_Value_Slider.cxx
   fl_vertex.cxx
   Fl_visual.cxx
   Fl_Widget.cxx
   Fl_win32.cxx
   Fl_Window.cxx
   Fl_Window_fullscreen.cxx
   Fl_Window_hotspot.cxx
   Fl_Window_iconize.cxx
   Fl_x.cxx
   Fl_XColor.h
   fl_xpm.cxx
   fltk_theme.cxx
   fltk_theme_win32.cxx
   fltk_theme_x.cxx
   IFont.h
   mediumarrow.h
   new.xbm
   ns.xbm
   ns_mask.xbm
   pktdef.h
   scandir.c
   scandir_win32.c
   shortcuts.cxx
   slowarrow.h
   string.c
   up.xbm
   utf.c
   ValueOutput.cxx
   vsnprintf.c
   wintab.h
   wintabx.h
   XColorMap.h
   xutf8.cxx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
//
// "$Id: fl_bmp.cxx,v 1.27 2005/01/24 08:34:34 spitzak Exp $"
//
// bmpImage routines.
//
// Copyright 1997-2003 by Easy Software Products.
// Copyright 1998-2004 by Bill Spitzak and others.
// Image support donated by Matthias Melcher, Copyright 2000.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 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
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library 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.
//
// Please report all bugs and problems to "fltk-bugs@fltk.org".
//

#include <config.h>
#include <fltk/SharedImage.h> // defines bmpImage.h
#include <fltk/events.h>
#include <fltk/error.h>
#include <fltk/draw.h>
#include <fltk/x.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace fltk;

static FILE *bmpFile;
static uchar* bmpDatasStart;
static uchar* bmpDatas;

static uchar GETC()
{
  if (bmpDatas) return *bmpDatas++;
  else return fgetc(bmpFile);
}


static int FREAD(void *buf, int size)
{
  if (bmpDatas) {
    memcpy(buf, bmpDatas, size);
    bmpDatas += size;
    return size;
  }
  return fread(buf, 1, size, bmpFile);
}

static int FSEEK(int offset)
{
  if (bmpDatas) {
    bmpDatas = bmpDatasStart + offset;
    return offset;
  }
  return fseek(bmpFile, offset, SEEK_SET);
}

//
// BMP definitions...
//

#ifndef BI_RGB
#  define BI_RGB       0             // No compression - straight BGR data
#  define BI_RLE8      1             // 8-bit run-length compression
#  define BI_RLE4      2             // 4-bit run-length compression
#  define BI_BITFIELDS 3             // RGB bitmap with RGB masks
#endif // !BI_RGB


//
// Local functions...
//

static int		read_long();
static unsigned short	read_word();
static unsigned int	read_dword();

bool bmpImage::test(const uchar* buffer, unsigned size)
{
  return !strncmp((char*)buffer, "BM", size<2? size:2);
}

void bmpImage::_measure(int &W, int &H) const
{
  if (w() >= 0) {
    W = w();
    H = h();
    return;
  }

  bmpDatas = (uchar*)datas;
  bmpDatasStart = bmpDatas;

  if (!datas) {
    if ((bmpFile = fopen(get_filename(), "rb")) == NULL) {
      fltk::warning("Cannot open BMP file '%s'", get_filename());
      const_cast<bmpImage*>(this)->setsize(0,0);
      return;
    }
  }

  // Get the header...
  uchar byte = (uchar)GETC();	// Check "BM" sync chars
  uchar bit  = (uchar)GETC();
  if (byte != 'B' || bit != 'M') {
    if (!datas) fclose(bmpFile);
    return;
  }

  read_dword();	// Skip size
  read_word();	// Skip reserved stuff
  read_word();
  read_dword(); // Read offset to image data

  // Then the bitmap information...
  int info_size = read_dword();

  if (info_size < 40) {
    // Old Windows/OS2 BMP header...
    W = read_word();
    H = read_word();
    const_cast<bmpImage*>(this)->setsize((int)W,(int)H);
  } else {
    // New BMP header...
    W = read_long();
    H = read_long();
    const_cast<bmpImage*>(this)->setsize((int)W,(int)H);
  }

  if (!datas) fclose(bmpFile);

  return;
}

//
// Load a BMP image file.
//
void bmpImage::read()
{
  bmpDatas = (uchar*)datas;
  bmpDatasStart = bmpDatas;

  int		info_size,	// Size of info header
		depth,		// Depth of image (bits)
		bDepth = 3,	// Depth of image (bytes)
		compression,	// Type of compression
		colors_used,	// Number of colors used
		x, y,		// Looping vars
		color,		// Color of RLE pixel
		repcount,	// Number of times to repeat
		temp,		// Temporary color
		align,		// Alignment bytes
		dataSize;	// number of bytes in image data set
  long		offbits;	// Offset to image data
  uchar		bit,		// Bit in image
		byte;		// Byte in image
  uchar		*ptr;		// Pointer into pixels
  uchar		colormap[256][3];// Colormap
  uchar		mask = 0;	// single bit mask follows image data

  if (!datas) {
    if ((bmpFile = fopen(get_filename(), "rb")) == NULL) {
      fltk::warning("Cannot open BMP file '%s'", get_filename());
      return;
    }
  }

  // Get the header...
  byte = (uchar)GETC();	// Check "BM" sync chars
  bit  = (uchar)GETC();
  if (byte != 'B' || bit != 'M') {
    if (!datas) fclose(bmpFile);
    return;
  }

  read_dword();		// Skip size
  read_word();		// Skip reserved stuff
  read_word();
  offbits = (long)read_dword();// Read offset to image data

  // Then the bitmap information...
  info_size = read_dword();

//  printf("offbits = %ld, info_size = %d\n", offbits, info_size);

  if (info_size < 40) {
    // Old Windows/OS2 BMP header...
    int W = read_word();
    int H = read_word();
    this->setsize(W,H);
    read_word();
    depth = read_word();
    compression = BI_RGB;
    colors_used = 0;

    repcount = info_size - 12;
  } else {
    // New BMP header...
    int W = read_long();
    int H = read_long();
    this->setsize(W,H);
    read_word();
    depth = read_word();
    compression = read_dword();
    dataSize = read_dword();
    read_long();
    read_long();
    colors_used = read_dword();
    read_dword();

    repcount = info_size - 40;

    if (!compression && depth>=8 && w()>32/depth) {
      int Bpp = depth/8;
      int maskSize = (((w()*Bpp+3)&~3)*h()) + (((((w()+7)/8)+3)&~3)*h());
      if (maskSize==2*dataSize) {
	mask = 1;
	h_ = (h_/2);
	bDepth = 4;
      }
    }
  }

//  printf("w() = %d, h() = %d, depth = %d, compression = %d, colors_used = %d, repcount = %d\n",
//         w(), h(), depth, compression, colors_used, repcount);

  // Skip remaining header bytes...
  while (repcount > 0) {
    GETC();
    repcount --;
  }

  // Check header data...
  if (!w() || !h() || !depth) {
    if (!datas) fclose(bmpFile);
    return;
  }

  // Get colormap...
  if (colors_used == 0 && depth <= 8)
    colors_used = 1 << depth;

  for (repcount = 0; repcount < colors_used; repcount ++) {
    // Read BGR color...
    FREAD(colormap[repcount], 3);
    //fread(colormap[repcount], 1, 3, );

    // Skip pad byte for new BMP files...
    if (info_size > 12) GETC();
  }

  // Setup image and buffers...
  if (offbits) FSEEK(offbits);

  uchar *array = new uchar[w() * h() * bDepth];
  //alloc_array = 1;

  // Read the image data...
  color = 0;
  repcount = 0;
  align = 0;
  byte  = 0;
  temp  = 0;

  for (y = h() - 1; y >= 0; y --) {
    ptr = (uchar *)array + y * w() * bDepth;

    switch (depth)
    {
      case 1 : // Bitmap
	  for (x = w(), bit = 128; x > 0; x --) {
	    if (bit == 128) byte = (uchar)GETC();

	    if (byte & bit) {
	      *ptr++ = colormap[1][2];
	      *ptr++ = colormap[1][1];
	      *ptr++ = colormap[1][0];
	    } else {
	      *ptr++ = colormap[0][2];
	      *ptr++ = colormap[0][1];
	      *ptr++ = colormap[0][0];
	    }

	    if (bit > 1)
	      bit >>= 1;
	    else
	      bit = 128;
	  }

	  // Read remaining bytes to align to 32 bits...
	  for (temp = (w() + 7) / 8; temp & 3; temp ++) {
	    GETC();
	  }
	  break;

      case 4 : // 16-color
	  for (x = w(), bit = 0xf0; x > 0; x --) {
	    // Get a new repcount as needed...
	    if (repcount == 0) {
	      if (compression != BI_RLE4) {
		repcount = 2;
		color = -1;
	      } else {
		while (align > 0) {
		  align --;
		  GETC();
		}

		if ((repcount = GETC()) == 0) {
		  if ((repcount = GETC()) == 0) {
		    // End of line...
		    x ++;
		    continue;
		  } else if (repcount == 1) {
		    // End of image...
		    break;
		  } else if (repcount == 2) {
		    // Delta...
		    repcount = GETC() * GETC() * w();
		    color = 0;
		  } else {
		    // Absolute...
		    color = -1;
		    align = ((4 - (repcount & 3)) / 2) & 1;
		  }
		} else {
		  color = GETC();
		}
	      }
	    }

	    // Get a new color as needed...
	    repcount --;

	    // Extract the next pixel...
	    if (bit == 0xf0) {
	      // Get the next color byte as needed...
	      if (color < 0) temp = GETC();
	      else temp = color;

	      // Copy the color value...
	      *ptr++ = colormap[(temp >> 4) & 15][2];
	      *ptr++ = colormap[(temp >> 4) & 15][1];
	      *ptr++ = colormap[(temp >> 4) & 15][0];

	      bit  = 0x0f;
	    } else {
	      bit  = 0xf0;

	      // Copy the color value...
	      *ptr++ = colormap[temp & 15][2];
	      *ptr++ = colormap[temp & 15][1];
	      *ptr++ = colormap[temp & 15][0];
	    }

	  }

	  if (!compression) {
	    // Read remaining bytes to align to 32 bits...
	    for (temp = (w() + 1) / 2; temp & 3; temp ++) {
	      GETC();
	    }
	  }
	  break;

      case 8 : // 256-color
	  for (x = w(); x > 0; x --) {
	    // Get a new repcount as needed...
	    if (compression != BI_RLE8) {
	      repcount = 1;
	      color = -1;
	    }

	    if (repcount == 0) {
	      while (align > 0) {
		align --;
		GETC();
	      }

	      if ((repcount = GETC()) == 0) {
		if ((repcount = GETC()) == 0) {
		  // End of line...
		  x ++;
		  continue;
		} else if (repcount == 1) {
		  // End of image...
		  break;
		} else if (repcount == 2) {
		  // Delta...
		  repcount = GETC() * GETC() * w();
		  color = 0;
		} else {
		  // Absolute...
		  color = -1;
		  align = (2 - (repcount & 1)) & 1;
		}
	      } else {
		color = GETC();
	      }
	    }

	    // Get a new color as needed...
	    if (color < 0) temp = GETC();
	    else temp = color;

	    repcount --;

	    // Copy the color value...
	    *ptr++ = colormap[temp][2];
	    *ptr++ = colormap[temp][1];
	    *ptr++ = colormap[temp][0];
	    if (mask) ptr++;
	  }

	  if (!compression) {
	    // Read remaining bytes to align to 32 bits...
	    for (temp = w(); temp & 3; temp ++) {
	      GETC();
	    }
	  }
	  break;

      case 16 : // 16-bit 5:5:5 RGB
	  for (x = w(); x > 0; x --, ptr += bDepth) {
	    uchar b = GETC(), a = GETC() ;
#ifdef USE_5_6_5 // Green as the brightest color should have one bit more 5:6:5
	    ptr[0] = (uchar)(( b << 3 ) & 0xf8);
	    ptr[1] = (uchar)(((a << 5) & 0xe0) | ((b >> 3) & 0x1c));
	    ptr[2] = (uchar)(a & 0xf8);
#else // this is the default wasting one bit: 5:5:5
	    ptr[2] = (uchar)((b << 3) & 0xf8);
	    ptr[1] = (uchar)(((a << 6) & 0xc0) | ((b >> 2) & 0x38));
	    ptr[0] = (uchar)((a<<1) & 0xf8);
#endif
	  }

	  // Read remaining bytes to align to 32 bits...
	  for (temp = w() * 3; temp & 3; temp ++) {
	    GETC();
	  }
	  break;

      case 24 : // 24-bit RGB
	  for (x = w(); x > 0; x --, ptr += bDepth) {
	    ptr[2] = (uchar)GETC();
	    ptr[1] = (uchar)GETC();
	    ptr[0] = (uchar)GETC();
	  }

	  // Read remaining bytes to align to 32 bits...
	  for (temp = w() * 3; temp & 3; temp ++) {
	    GETC();
	  }
	  break;
    }
  }

  if (mask) {
    for (y = h() - 1; y >= 0; y --) {
      ptr = (uchar *)array + y * w() * bDepth + 3;
      for (x = w(), bit = 128; x > 0; x --, ptr+=bDepth) {
	if (bit == 128) byte = (uchar)GETC();
	if (byte & bit)
	  *ptr = 0;
	else
	  *ptr = 255;
	if (bit > 1)
	  bit >>= 1;
	else
	  bit = 128;
      }
      // Read remaining bytes to align to 32 bits...
      for (temp = (w() + 7) / 8; temp & 3; temp ++)
	GETC();
    }
  }

  ImageDraw idraw(this);
  drawimage(array, PixelType(bDepth), Rectangle(w_, h_), bDepth);

  // Close the file and return...
  if (!datas) fclose(bmpFile);

  delete []array;
}


//
// 'read_word()' - Read a 16-bit unsigned integer.
//

static unsigned short	// O - 16-bit unsigned integer
read_word() {
  unsigned char b0, b1;	// Bytes from file

  b0 = (uchar)GETC();
  b1 = (uchar)GETC();

  return ((b1 << 8) | b0);
}


//
// 'read_dword()' - Read a 32-bit unsigned integer.
//

static unsigned int		// O - 32-bit unsigned integer
read_dword() {
  unsigned char b0, b1, b2, b3;	// Bytes from file

  b0 = (uchar)GETC();
  b1 = (uchar)GETC();
  b2 = (uchar)GETC();
  b3 = (uchar)GETC();

  return ((((((b3 << 8) | b2) << 8) | b1) << 8) | b0);
}


//
// 'read_long()' - Read a 32-bit signed integer.
//

static int			// O - 32-bit signed integer
read_long() {
  unsigned char b0, b1, b2, b3;	// Bytes from file

  b0 = (uchar)GETC();
  b1 = (uchar)GETC();
  b2 = (uchar)GETC();
  b3 = (uchar)GETC();

  return ((int)(((((b3 << 8) | b2) << 8) | b1) << 8) | b0);
}

//
// End of "$Id: fl_bmp.cxx,v 1.27 2005/01/24 08:34:34 spitzak Exp $"
//