home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / FLTK-1.0.6 / src / fl_draw_image_win32.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-07  |  7.3 KB  |  263 lines

  1. //
  2. // "$Id: fl_draw_image_win32.cxx,v 1.4 1999/01/07 19:17:38 mike Exp $"
  3. //
  4. // WIN32 image drawing code for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-1999 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems to "fltk-bugs@easysw.com".
  24. //
  25.  
  26. // I hope a simple and portable method of drawing color and monochrome
  27. // images.  To keep this simple, only a single storage type is
  28. // supported: 8 bit unsigned data, byte order RGB, and pixels are
  29. // stored packed into rows with the origin at the top-left.  It is
  30. // possible to alter the size of pixels with the "delta" argument, to
  31. // add alpha or other information per pixel.  It is also possible to
  32. // change the origin and direction of the image data by messing with
  33. // the "delta" and "linedelta", making them negative, though this may
  34. // defeat some of the shortcuts in translating the image for X.
  35.  
  36. // Unbelievably (since it conflicts with how most PC software works)
  37. // Micro$oft picked a bottom-up and BGR storage format for their
  38. // DIB images.  I'm pretty certain there is a way around this, but
  39. // I can't find any other than the brute-force method of drawing
  40. // each line as a seperate image.  This may also need to be done
  41. // if the delta is any amount other than 1, 3, or 4.
  42.  
  43. ////////////////////////////////////////////////////////////////
  44.  
  45. #include <config.h>
  46. #include <FL/Fl.H>
  47. #include <FL/fl_draw.H>
  48. #include <FL/x.H>
  49.  
  50. #define MAXBUFFER 0x40000 // 256k
  51.  
  52. #if USE_COLORMAP
  53.  
  54. // error-diffusion dither into the fltk colormap
  55. static void dither(uchar* to, const uchar* from, int w, int delta) {
  56.   static int ri, gi, bi, dir;
  57.   int r=ri, g=gi, b=bi;
  58.   int d, td;
  59.   if (dir) {
  60.     dir = 0;
  61.     from = from+(w-1)*delta;
  62.     to = to+(w-1);
  63.     d = -delta;
  64.     td = -1;
  65.   } else {
  66.     dir = 1;
  67.     d = delta;
  68.     td = 1;
  69.   }
  70.   for (; w--; from += d, to += td) {
  71.     r += from[0]; if (r < 0) r = 0; else if (r>255) r = 255;
  72.     int rr = r*FL_NUM_RED/256;
  73.     r -= rr*255/(FL_NUM_RED-1);
  74.     g += from[1]; if (g < 0) g = 0; else if (g>255) g = 255;
  75.     int gg = g*FL_NUM_GREEN/256;
  76.     g -= gg*255/(FL_NUM_GREEN-1);
  77.     b += from[2]; if (b < 0) b = 0; else if (b>255) b = 255;
  78.     int bb = b*FL_NUM_BLUE/256;
  79.     b -= bb*255/(FL_NUM_BLUE-1);
  80.     *to = uchar(FL_COLOR_CUBE+(bb*FL_NUM_RED+rr)*FL_NUM_GREEN+gg);
  81.   }
  82.   ri = r; gi = g; bi = b;
  83. }
  84.  
  85. // error-diffusion dither into the fltk colormap
  86. static void monodither(uchar* to, const uchar* from, int w, int delta) {
  87.   static int ri,dir;
  88.   int r=ri;
  89.   int d, td;
  90.   if (dir) {
  91.     dir = 0;
  92.     from = from+(w-1)*delta;
  93.     to = to+(w-1);
  94.     d = -delta;
  95.     td = -1;
  96.   } else {
  97.     dir = 1;
  98.     d = delta;
  99.     td = 1;
  100.   }
  101.   for (; w--; from += d, to += td) {
  102.     r += *from; if (r < 0) r = 0; else if (r>255) r = 255;
  103.     int rr = r*FL_NUM_GRAY/256;
  104.     r -= rr*255/(FL_NUM_GRAY-1);
  105.     *to = uchar(FL_GRAY_RAMP+rr);
  106.   }
  107.   ri = r;
  108. }
  109.  
  110. #endif // USE_COLORMAP
  111.  
  112. static void innards(const uchar *buf, int X, int Y, int W, int H,
  113.             int delta, int linedelta, int mono,
  114.             Fl_Draw_Image_Cb cb, void* userdata)
  115. {
  116. #if USE_COLORMAP
  117.   char indexed = (fl_palette != 0);
  118. #endif
  119.  
  120.   if (!linedelta) linedelta = W*delta;
  121.  
  122.   int x, y, w, h;
  123.   fl_clip_box(X,Y,W,H,x,y,w,h);
  124.   if (w<=0 || h<=0) return;
  125.   if (buf) buf += (x-X)*delta + (y-Y)*linedelta;
  126.  
  127.   static U32 bmibuffer[256+12];
  128.   BITMAPINFO &bmi = *((BITMAPINFO*)bmibuffer);
  129.   if (!bmi.bmiHeader.biSize) {
  130.     bmi.bmiHeader.biSize = sizeof(bmi)-4; // does it use this to determine type?
  131.     bmi.bmiHeader.biPlanes = 1;
  132.     bmi.bmiHeader.biCompression = BI_RGB;
  133.     bmi.bmiHeader.biXPelsPerMeter = 0;
  134.     bmi.bmiHeader.biYPelsPerMeter = 0;
  135.     bmi.bmiHeader.biClrUsed = 0;
  136.     bmi.bmiHeader.biClrImportant = 0;
  137.   }
  138. #if USE_COLORMAP
  139.   if (indexed) {
  140.     for (int i=0; i<256; i++) {
  141.       *((short*)(bmi.bmiColors)+i) = i;
  142.     }
  143.   } else
  144. #endif
  145.   if (mono) {
  146.     for (int i=0; i<256; i++) {
  147.       bmi.bmiColors[i].rgbBlue = i;
  148.       bmi.bmiColors[i].rgbGreen = i;
  149.       bmi.bmiColors[i].rgbRed = i;
  150.       bmi.bmiColors[i].rgbReserved = i;
  151.     }
  152.   }
  153.   bmi.bmiHeader.biWidth = w;
  154. #if USE_COLORMAP
  155.   bmi.bmiHeader.biBitCount = mono|indexed ? 8 : 24;
  156.   int pixelsize = mono|indexed ? 1 : 3;
  157. #else
  158.   bmi.bmiHeader.biBitCount = mono ? 8 : 24;
  159.   int pixelsize = mono ? 1 : 3;
  160. #endif
  161.   int linesize = (pixelsize*w+3)&~3;
  162.   
  163.   static U32* buffer;
  164.   int blocking = h;
  165.   {int size = linesize*h;
  166.   if (size > MAXBUFFER) {
  167.     size = MAXBUFFER;
  168.     blocking = MAXBUFFER/linesize;
  169.   }
  170.   static long buffer_size;
  171.   if (size > buffer_size) {
  172.     delete[] buffer;
  173.     buffer_size = size;
  174.     buffer = new U32[(size+3)/4];
  175.   }}
  176.   bmi.bmiHeader.biHeight = blocking;
  177.   static U32* line_buffer;
  178.   if (!buf) {
  179.     int size = W*delta;
  180.     static int line_buf_size;
  181.     if (size > line_buf_size) {
  182.       delete[] line_buffer;
  183.       line_buf_size = size;
  184.       line_buffer = new U32[(size+3)/4];
  185.     }
  186.   }
  187.   for (int j=0; j<h; ) {
  188.     int k;
  189.     for (k = 0; j<h && k<blocking; k++, j++) {
  190.       const uchar* from;
  191.       if (!buf) { // run the converter:
  192.     cb(userdata, x-X, y-Y+j, w, (uchar*)line_buffer);
  193.     from = (uchar*)line_buffer;
  194.       } else {
  195.     from = buf;
  196.     buf += linedelta;
  197.       }
  198.       uchar *to = (uchar*)buffer+(blocking-k-1)*linesize;
  199. #if USE_COLORMAP
  200.       if (indexed) {
  201.     if (mono)
  202.       monodither(to, from, w, delta);
  203.     else 
  204.       dither(to, from, w, delta);
  205.     to += w;
  206.       } else
  207. #endif
  208.       if (mono) {
  209.     for (int i=w; i--; from += delta) *to++ = *from;
  210.       } else {
  211.     for (int i=w; i--; from += delta, to += 3) {
  212.       uchar r = from[0];
  213.       to[0] = from[2];
  214.       to[1] = from[1];
  215.       to[2] = r;
  216.         }
  217.       }
  218.     }
  219.     SetDIBitsToDevice(fl_gc, x, y+j-k, w, k, 0, 0, 0, k,
  220.               (LPSTR)((uchar*)buffer+(blocking-k)*linesize),
  221.               &bmi,
  222. #if USE_COLORMAP
  223.               indexed ? DIB_PAL_COLORS : DIB_RGB_COLORS
  224. #else
  225.               DIB_RGB_COLORS
  226. #endif
  227.               );
  228.   }
  229. }
  230.  
  231. void fl_draw_image(const uchar* buf, int x, int y, int w, int h, int d, int l){
  232.   innards(buf,x,y,w,h,d,l,(d<3&&d>-3),0,0);
  233. }
  234. void fl_draw_image(Fl_Draw_Image_Cb cb, void* data,
  235.            int x, int y, int w, int h,int d) {
  236.   innards(0,x,y,w,h,d,0,(d<3&&d>-3),cb,data);
  237. }
  238. void fl_draw_image_mono(const uchar* buf, int x, int y, int w, int h, int d, int l){
  239.   innards(buf,x,y,w,h,d,l,1,0,0);
  240. }
  241. void fl_draw_image_mono(Fl_Draw_Image_Cb cb, void* data,
  242.            int x, int y, int w, int h,int d) {
  243.   innards(0,x,y,w,h,d,0,1,cb,data);
  244. }
  245.  
  246. void fl_rectf(int x, int y, int w, int h, uchar r, uchar g, uchar b) {
  247. #if USE_COLORMAP
  248.   // use the error diffusion dithering code to produce a much nicer block:
  249.   if (fl_palette) {
  250.     uchar c[3];
  251.     c[0] = r; c[1] = g; c[2] = b;
  252.     innards(c,x,y,w,h,0,0,0,0,0);
  253.     return;
  254.   }
  255. #endif
  256.   fl_color(r,g,b);
  257.   fl_rectf(x,y,w,h);
  258. }
  259.  
  260. //
  261. // End of "$Id: fl_draw_image_win32.cxx,v 1.4 1999/01/07 19:17:38 mike Exp $".
  262. //
  263.