Filter:   InfoImg
download fl_draw_pixmap.cxx
Language: C++
Copyright: Copyright 1998-2003 by Bill Spitzak and others.
LOC: 231
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
//
// "$Id: fl_draw_pixmap.cxx,v 1.24 2005/01/24 08:07:54 spitzak Exp $"
//
// Pixmap drawing code for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2003 by Bill Spitzak and others.
//
// 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".
//

// Implemented without using the xpm library (which I can't use because
// it interferes with the color cube used by drawimage).
// Current implementation is cheap and slow, and works best on a full-color
// display.  Transparency is not handled, and colors are dithered to
// the color cube.  Color index is achieved by adding the id
// characters together!  Also mallocs a lot of temporary memory!
// Notice that there is no pixmap file interface.  This is on purpose,
// as I want to discourage programs that require support files to work.
// All data needed by a program ui should be compiled in!!!

#include <fltk/Color.h>
#include <fltk/draw.h>
#include <config.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
using namespace fltk;

static int ncolors, chars_per_pixel;

int fltk::measure_xpm(const char*const* data, int &w, int &h) {
  int i = sscanf(data[0],"%d %d %d %d",&w,&h,&ncolors,&chars_per_pixel);
  if (i<4 || w<=0 || h<=0 ||
      chars_per_pixel!=1 && chars_per_pixel!=2) return w=0;
  return 1;
}

#ifdef U64

// The callback from drawimage to get a row of data passes this:
struct xpm_data {
  int w, h;
  const uchar*const* data;
  union {
    U64 colors[256];
    U64* byte1[256];
  };
};

// callback for 1 byte per pixel:
static const uchar* cb1(void*v, int x, int y, int w, uchar* buf) {
  xpm_data& d = *(xpm_data*)v;
  const uchar* p = d.data[y]+x;
  U64* q = (U64*)buf;
  for (int X=(w+1)/2; X--; p += 2) {
#if WORDS_BIGENDIAN
    *q++ = (d.colors[p[0]]<<32) | d.colors[p[1]];
#else
    *q++ = (d.colors[p[1]]<<32) | d.colors[p[0]];
#endif
  }
  return buf;
}

// callback for 2 bytes per pixel:
static const uchar* cb2(void*v, int x, int y, int w, uchar* buf) {
  xpm_data& d = *(xpm_data*)v;
  const uchar* p = d.data[y]+2*x;
  U64* q = (U64*)buf;
  for (int X=(w+1)/2; X--;) {
    U64* colors = d.byte1[*p++];
    int index = *p++;
    U64* colors1 = d.byte1[*p++];
    int index1 = *p++;
#if WORDS_BIGENDIAN
    *q++ = (colors[index]<<32) | colors1[index1];
#else
    *q++ = (colors1[index1]<<32) | colors[index];
#endif
  }
  return buf;
}

#else

// The callback from drawimage to get a row of data passes this:
struct xpm_data {
  int w, h;
  const uchar*const* data;
  union {
    U32 colors[256];
    U32* byte1[256];
  };
};

// callback for 1 byte per pixel:
static const uchar* cb1(void*v, int x, int y, int w, uchar* buf) {
  xpm_data& d = *(xpm_data*)v;
  const uchar* p = d.data[y]+x;
  U32* q = (U32*)buf;
  for (int X=w; X--;) *q++ = d.colors[*p++];
  return buf;
}

// callback for 2 bytes per pixel:
static const uchar* cb2(void*v, int x, int y, int w, uchar* buf) {
  xpm_data& d = *(xpm_data*)v;
  const uchar* p = d.data[y]+2*x;
  U32* q = (U32*)buf;
  for (int X=w; X--;) {
    U32* colors = d.byte1[*p++];
    *q++ = colors[*p++];
  }
  return buf;
}

#endif

static uchar **mask_bitmap;

void fltk::set_mask_bitmap(uchar **ppBitmap)
{
	mask_bitmap = ppBitmap;
}

Color fg_kludge;

int fltk::draw_xpm(const char*const* di, int x, int y, Color bg) {
  xpm_data d;
  if (!measure_xpm(di, d.w, d.h)) return 0;
  const uchar*const* data = (const uchar*const*)(di+1);
  int transparent_index = -1;

  if (ncolors < 0) {	// fltk (non standard) compressed colormap
    ncolors = -ncolors;
    const uchar *p = *data++;
    // if first color is ' ' it is transparent (put it later to make
    // it not be transparent):
    if (*p == ' ') {
      uchar* c = (uchar*)&d.colors[32];
#ifdef U64
      *(U64*)c = 0;
#if WORDS_BIGENDIAN
      c += 4;
#endif
#endif
      transparent_index = ' ';
      split_color(bg, c[0], c[1], c[2]); c[3] = 0;
      p += 4;
      ncolors--;
    }
    // read all the rest of the colors:
    for (int i=0; i < ncolors; i++) {
      uchar* c = (uchar*)&d.colors[*p++];
#ifdef U64
      *(U64*)c = 0;
#if WORDS_BIGENDIAN
      c += 4;
#endif
#endif
      *c++ = *p++;
      *c++ = *p++;
      *c++ = *p++;
      *c = 0xff;
    }
  } else {	// normal XPM colormap with names
    uchar f[3];
    uchar b[3];
    if (fg_kludge) {
      split_color(fg_kludge, f[0], f[1], f[2]);
      split_color(bg, b[0], b[1], b[2]);
    }
    if (chars_per_pixel>1) memset(d.byte1, 0, sizeof(d.byte1));
    for (int i=0; i<ncolors; i++) {
      const uchar* p = *data++;
      // the first 1 or 2 characters are the color index:
      int index = *p++;
      uchar* c;
      if (chars_per_pixel>1) {
#ifdef U64
	U64* colors = d.byte1[index];
	if (!colors) colors = d.byte1[index] = new U64[256];
#else
	U32* colors = d.byte1[index];
	if (!colors) colors = d.byte1[index] = new U32[256];
#endif
	c = (uchar*)&colors[*p];
	index = (index<<8)+*p++;
      } else {
	c = (uchar *)&d.colors[index];
      }
      // look for "c word", or last word if none:
      const uchar *previous_word = p;
      for (;;) {
	while (*p && isspace(*p)) p++; uchar what = *p++;
	while (*p && !isspace(*p)) p++;
	while (*p && isspace(*p)) p++;
	if (!*p) {p = previous_word; break;}
	if (what == 'c') break;
	previous_word = p;
	while (*p && !isspace(*p)) p++;
      }
#ifdef U64
      *(U64*)c = 0;
#if WORDS_BIGENDIAN
      c += 4;
#endif
#endif
      Color C = color((const char*)p);
      if (C) {
	c[0] = uchar(C>>24);
	c[1] = uchar(C>>16);
	c[2] = uchar(C>>8);
	if (fg_kludge) {
	  c[0] = (c[0]*b[0]+(255-c[0])*f[0])/255;
	  c[1] = (c[1]*b[1]+(255-c[1])*f[1])/255;
	  c[2] = (c[2]*b[2]+(255-c[2])*f[2])/255;
	}
	c[3] = 0xff;
      } else { // assume "None" or "#transparent" for any errors
	// this should be transparent...
	transparent_index = index;
	c[0] = c[1] = c[2] = c[3] = 0;
      }
    }
  }
  d.data = data;

  // build the mask bitmap used by xpmImage:
  if (mask_bitmap && transparent_index >= 0) {
    // search for usage of the transparent index, if none we act like
    // the image is opaque (which avoids some buggy code on X/Win32):
    int y;
    for (y = 0; y < d.h; y++) {
      const uchar* p = data[y];
      if (chars_per_pixel <= 1) {
	for (int x = 0; x < d.w; x++)
	  if (*p++ == transparent_index) goto GOTIT;
      } else {
	for (int x = 0; x < d.w; x++) {
	  int index = *p++;
	  index = (index<<8) | (*p++);
	  if (index == transparent_index) goto GOTIT;
	}
      }
    }
    goto NO_MASK;
  GOTIT:
    int W = (d.w+7)/8;
    uchar* bitmap = new uchar[W * d.h];
    *mask_bitmap = bitmap;
    for (y = 0; y < d.h; y++) {
      const uchar* p = data[y];
      if (chars_per_pixel <= 1) {
	for (int x = 0; x < W; x++) {
	  int b = (*p++ != transparent_index);
	  if (*p++ != transparent_index) b |= 2;
	  if (*p++ != transparent_index) b |= 4;
	  if (*p++ != transparent_index) b |= 8;
	  if (*p++ != transparent_index) b |= 16;
	  if (*p++ != transparent_index) b |= 32;
	  if (*p++ != transparent_index) b |= 64;
	  if (*p++ != transparent_index) b |= 128;
	  *bitmap++ = b;
	}
      } else {
	for (int x = 0; x < W; x++) {
	  int b = 0;
	  for (int i = 0; i < 8; i++) {
	    int index = *p++;
	    index = (index<<8) | (*p++);
	    if (index != transparent_index) b |= (1<<i);
	  }
	  *bitmap++ = b;
	}
      }
    }
  }
 NO_MASK:

  drawimage(chars_per_pixel==1 ? cb1 : cb2, &d, RGB, Rectangle(x, y, d.w, d.h), 4);
  if (chars_per_pixel > 1) for (int i = 0; i < 256; i++) delete[] d.byte1[i];
  return 1;
}

//
// End of "$Id: fl_draw_pixmap.cxx,v 1.24 2005/01/24 08:07:54 spitzak Exp $".
//