Filter:   InfoImg
download fl_draw_image_win32.cxx
Language: C++
Copyright: Copyright 1998-2003 by Bill Spitzak and others.
LOC: 236
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
//
// "$Id: fl_draw_image_win32.cxx,v 1.26 2005/01/25 09:49:12 spitzak Exp $"
//
// _WIN32 image 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".
//

// I hope a simple and portable method of drawing color and monochrome
// images.  To keep this simple, only a single storage type is
// supported: 8 bit unsigned data, byte order RGB, and pixels are
// stored packed into rows with the origin at the top-left.  It is
// possible to alter the size of pixels with the "delta" argument, to
// add alpha or other information per pixel.  It is also possible to
// change the origin and direction of the image data by messing with
// the "delta" and "linedelta", making them negative, though this may
// defeat some of the shortcuts in translating the image for X.

// Unbelievably (since it conflicts with how most PC software works)
// MicroSoft picked a bottom-up and BGR storage format for their
// DIB images.  I'm pretty certain there is a way around this, but
// I can't find any other than the brute-force method of drawing
// each line as a seperate image.  This may also need to be done
// if the delta is any amount other than 1, 3, or 4.

////////////////////////////////////////////////////////////////

#include <fltk/Color.h>
#include <stdio.h>
using namespace fltk;

#define MAXBUFFER 0x40000 // 256k

#undef USE_COLORMAP
#define USE_COLORMAP 0
#if USE_COLORMAP

// error-diffusion dither into the fltk colormap
static void dither(uchar* to, const uchar* from, int w, int delta) {
  static int ri, gi, bi, dir;
  int r=ri, g=gi, b=bi;
  int d, td;
  if (dir) {
    dir = 0;
    from = from+(w-1)*delta;
    to = to+(w-1);
    d = -delta;
    td = -1;
  } else {
    dir = 1;
    d = delta;
    td = 1;
  }
  for (;; from += d, to += td) {
    r += from[0]; if (r < 0) r = 0; else if (r>255) r = 255;
    int rr = r*5/256;
    r -= rr*255/4;
    g += from[1]; if (g < 0) g = 0; else if (g>255) g = 255;
    int gg = g*8/256;
    g -= gg*255/7;
    b += from[2]; if (b < 0) b = 0; else if (b>255) b = 255;
    int bb = b*5/256;
    b -= bb*255/4;
    *to = uchar(BLACK+(bb*5+rr)*8+gg);
    if (!--w) break;
  }
  ri = r; gi = g; bi = b;
}

// error-diffusion dither into the fltk colormap
static void monodither(uchar* to, const uchar* from, int w, int delta) {
  static int ri, gi, bi, dir;
  int r=ri, g=gi, b=bi;
  int d, td;
  if (dir) {
    dir = 0;
    from = from+(w-1)*delta;
    to = to+(w-1);
    d = -delta;
    td = -1;
  } else {
    dir = 1;
    d = delta;
    td = 1;
  }
  for (;; from += d, to += td) {
    r += from[0]; if (r < 0) r = 0; else if (r>255) r = 255;
    int rr = r*5/256;
    r -= rr*255/4;
    g += from[0]; if (g < 0) g = 0; else if (g>255) g = 255;
    int gg = g*8/256;
    g -= gg*255/7;
    b += from[0]; if (b < 0) b = 0; else if (b>255) b = 255;
    int bb = b*5/256;
    b -= bb*255/4;
    *to = uchar(BLACK+(bb*5+rr)*8+gg);
    if (!--w) break;
  }
  ri = r;
}

#endif // USE_COLORMAP

static void innards(const uchar *buf, PixelType type,
		    const fltk::Rectangle& r1,
		    int delta, int linedelta,
		    DrawImageCallback cb, void* userdata)
{
#if USE_COLORMAP
  char indexed = (xpalette != 0);
#endif

  fltk::Rectangle r(r1); transform(r);
  fltk::Rectangle cr(r); if (!intersect_with_clip(cr)) return;
  if (!linedelta) linedelta = r1.w()*delta;
  int x = cr.x();
  int y = cr.y();
  int w = cr.w();
  int h = cr.h();
  int X = r.x();
  int Y = r.y();
  int W = r1.w();
  if (buf) buf += (x-X)*delta + (y-Y)*linedelta;

  static U32 bmibuffer[256+12];
  BITMAPINFO &bmi = *((BITMAPINFO*)bmibuffer);
  if (!bmi.bmiHeader.biSize) {
    bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bmi.bmiHeader.biPlanes = 1;
    bmi.bmiHeader.biCompression = BI_RGB;
    bmi.bmiHeader.biXPelsPerMeter = 0;
    bmi.bmiHeader.biYPelsPerMeter = 0;
    bmi.bmiHeader.biClrUsed = 0;
    bmi.bmiHeader.biClrImportant = 0;
  }

  // -1 == none
  // 0  == indexed
  // 1  == mono
  static int current_cmap = -1;

#if USE_COLORMAP
  if (indexed) {
    if(current_cmap != 0) {
      current_cmap = 0;
      for (short i=0; i<256; i++) {
        *((short*)(bmi.bmiColors)+i) = i;
      }
    }
  } else
#endif
    bool mono = (type == LUMINANCE);
  if (mono) {
    if(current_cmap != 1) {
      current_cmap = 1;
      for (int i=0; i<256; i++) {
        bmi.bmiColors[i].rgbBlue = (uchar)i;
        bmi.bmiColors[i].rgbGreen = (uchar)i;
        bmi.bmiColors[i].rgbRed = (uchar)i;
        bmi.bmiColors[i].rgbReserved = (uchar)i;
      }
    }
  }
  
#if USE_COLORMAP
  int pixelsize = mono|indexed ? 1 : 4;
#else
  int pixelsize = mono ? 1 : 4;
#endif

  int linesize = pixelsize * w;
  if ((linesize % 4) != 0) {
    linesize += 4 - (linesize % 4);
  }

  bmi.bmiHeader.biWidth = w;
  bmi.bmiHeader.biBitCount = pixelsize*8;

  static U32* buffer;
  int blocking = h;
  {int size = linesize*h;
  if (size > MAXBUFFER) {
    size = MAXBUFFER;
    blocking = MAXBUFFER/linesize;
  }
  static long buffer_size;
  if (size > buffer_size) {
    delete[] buffer;
    buffer_size = size;
    buffer = new U32[(size+3)/4];
  }}

  static U32* line_buffer;
  if (!buf) {
    int size = W*delta;
    static int line_buf_size;
    if (size > line_buf_size) {
      delete[] line_buffer;
      line_buf_size = size;
      line_buffer = new U32[(size+3)/4];
    }
  }

  int ypos = y;
  for (int j=0; j<h; ) {
    int k;
    uchar *to = (uchar*)buffer;
    for (k = 0; j<h && k<blocking; k++, j++) {
      const uchar* from;
      if (!buf) { // run the converter:		  
        from = cb(userdata, x-X, y-Y+j, w, (uchar*)line_buffer);
      } else {
        from = buf;
        buf += linedelta;
      }
#if USE_COLORMAP
      if (indexed) {
        if (mono)
          monodither(to, from, w, delta);
        else 
          dither(to, from, w, delta);
        to += w;
      } else
#endif
        if (mono) {
          for (int i=w; i--; from += delta) *to++ = *from;
        } else {
          for (int i=w; i--; from += delta, to += pixelsize) {
            if(delta == 4) {
              // Premultiply for AlphaBlend
              int a = from[3];
              float afactor = (float)a / (float)0xff;
              to[0] = (uchar)(float(from[2]) * afactor);
	      to[1] = (uchar)(float(from[1]) * afactor);
	      to[2] = (uchar)(float(from[0]) * afactor);
	      to[3] = a;
            } else {
	      to[0] = from[2];
	      to[1] = from[1];
	      to[2] = from[0];
              to[3] = 0xFF; // alpha channel is totally opaque
            }
          }
        }
    }
    bmi.bmiHeader.biHeight = -k;
    bmi.bmiHeader.biSizeImage = k*linesize;
#if 1
    SetDIBitsToDevice(dc, x, ypos, w, k, 0, 0, 0, k,
		      (LPSTR)buffer,
		      &bmi,
# if USE_COLORMAP
		      indexed ? DIB_PAL_COLORS : DIB_RGB_COLORS
# else
		      DIB_RGB_COLORS
# endif
		      );
#else
    // We have to do all this conversion stuff because SetDIBitsToDevice does 
    // not do alpha blending. The easiest way to use the win32 alpha blending 
    // is to use the DrawIconEx function. It does all the things necessary to 
    // draw alpha blended images.
      // This code was contributed to fltk but I have not seen it do any
      // actual alpha compositing. Maybe somebody can figure out what is
      // wrong?
    HBITMAP b = CreateDIBitmap(dc,
			       (BITMAPINFOHEADER*)&head,
			       CBM_INIT,
			       (LPSTR)buffer,
			       (BITMAPINFO*)&head,
			       DIB_RGB_COLORS); 

    // we must create a temporary single bit bitmap to satisfy the icon 
    // creating constraints, even though it isn't used.
    HBITMAP hMonoBitmap = CreateBitmap(w,h,1,1,NULL);

    ICONINFO ii;
    ii.fIcon = TRUE;
    ii.xHotspot = 0;
    ii.yHotspot = 0;
    ii.hbmMask = b; //hMonoBitmap;
    ii.hbmColor = b;
    HICON imageIcon = ::CreateIconIndirect(&ii);
    DrawIconEx(dc, x, ypos, imageIcon, w, k, 0, NULL, DI_NORMAL);
    // Clean up all the mess
    ::DestroyIcon(imageIcon);
    ::DeleteObject(hMonoBitmap);
    ::DeleteObject(b);
#endif
    ypos += k;
  }
}

#if USE_COLORMAP
#define DITHER_FILLRECT (xpalette!=0)
#else
#define DITHER_FILLRECT false
#endif

//
// End of "$Id: fl_draw_image_win32.cxx,v 1.26 2005/01/25 09:49:12 spitzak Exp $".
//