You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

177 lines
5.6KB

  1. //
  2. // "$Id: fl_draw_image_mac.cxx 8581 2011-04-12 11:38:43Z manolo $"
  3. //
  4. // MacOS image drawing code for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-2010 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 on the following page:
  24. //
  25. // http://www.fltk.org/str.php
  26. //
  27. ////////////////////////////////////////////////////////////////
  28. #include <config.h>
  29. #include <FL/Fl.H>
  30. #include <FL/fl_draw.H>
  31. #include <FL/Fl_Printer.H>
  32. #include <FL/x.H>
  33. #define MAXBUFFER 0x40000 // 256k
  34. static void dataReleaseCB(void *info, const void *data, size_t size)
  35. {
  36. delete[] (uchar *)data;
  37. }
  38. /*
  39. * draw an image based on the input parameters
  40. *
  41. * buf: image source data
  42. * X, Y: position (in buffer?!)
  43. * W, H: size of picture (in pixel?)
  44. * delta: distance from pixel to pixel in buf in bytes
  45. * linedelta: distance from line to line in buf in bytes
  46. * mono: if set, pixel is one byte - if zero, pixel is 3 byte
  47. * cb: callback to copy image data into (RGB?) buffer
  48. * buf: pointer to first byte in image source
  49. * x, y: position in buffer
  50. * w: width (in bytes?)
  51. * dst: destination buffer
  52. * userdata: ?
  53. */
  54. static void innards(const uchar *buf, int X, int Y, int W, int H,
  55. int delta, int linedelta, int mono,
  56. Fl_Draw_Image_Cb cb, void* userdata)
  57. {
  58. if (!linedelta) linedelta = W*delta;
  59. const void *array = buf;
  60. uchar *tmpBuf = 0;
  61. if (cb || Fl_Surface_Device::surface()->class_name() == Fl_Printer::class_id) {
  62. tmpBuf = new uchar[ H*W*delta ];
  63. if (cb) {
  64. for (int i=0; i<H; i++) {
  65. cb(userdata, 0, i, W, tmpBuf+i*W*delta);
  66. }
  67. } else {
  68. uchar *p = tmpBuf;
  69. for (int i=0; i<H; i++) {
  70. memcpy(p, buf+i*linedelta, W*delta);
  71. p += W*delta;
  72. }
  73. }
  74. array = (void*)tmpBuf;
  75. linedelta = W*delta;
  76. }
  77. // create an image context
  78. CGColorSpaceRef lut = 0;
  79. if (delta<=2)
  80. lut = CGColorSpaceCreateDeviceGray();
  81. else
  82. lut = CGColorSpaceCreateDeviceRGB();
  83. // a release callback is necessary when the fl_gc is a print context because the image data
  84. // must be kept until the page is closed. Thus tmpBuf can't be deleted here. It's too early.
  85. CGDataProviderRef src = CGDataProviderCreateWithData( 0L, array, linedelta*H,
  86. tmpBuf ? dataReleaseCB : NULL
  87. );
  88. CGImageRef img = CGImageCreate( W, H, 8, 8*delta, linedelta,
  89. lut, delta&1?kCGImageAlphaNone:kCGImageAlphaNoneSkipLast,
  90. //lut, delta&1?kCGImageAlphaNone:kCGImageAlphaLast,
  91. src, 0L, false, kCGRenderingIntentDefault);
  92. // draw the image into the destination context
  93. if (img) {
  94. CGRect rect = { { X, Y }, { W, H } };
  95. Fl_X::q_begin_image(rect, 0, 0, W, H);
  96. CGContextDrawImage(fl_gc, rect, img);
  97. Fl_X::q_end_image();
  98. // release all allocated resources
  99. CGImageRelease(img);
  100. }
  101. CGColorSpaceRelease(lut);
  102. CGDataProviderRelease(src);
  103. if (img) return; // else fall through to slow mode
  104. // following the very save (and very slow) way to write the image into the give port
  105. CGContextSetShouldAntialias(fl_gc, false);
  106. if ( cb )
  107. {
  108. uchar *tmpBuf = new uchar[ W*4 ];
  109. for ( int i=0; i<H; i++ )
  110. {
  111. uchar *src = tmpBuf;
  112. cb( userdata, 0, i, W, tmpBuf );
  113. for ( int j=0; j<W; j++ )
  114. {
  115. if ( mono )
  116. { fl_color( src[0], src[0], src[0] ); }
  117. else
  118. { fl_color( src[0], src[1], src[2] ); }
  119. CGContextMoveToPoint(fl_gc, X+j, Y+i);
  120. CGContextAddLineToPoint(fl_gc, X+j, Y+i);
  121. CGContextStrokePath(fl_gc);
  122. src+=delta;
  123. }
  124. }
  125. delete[] tmpBuf;
  126. }
  127. else
  128. {
  129. for ( int i=0; i<H; i++ )
  130. {
  131. const uchar *src = buf+i*linedelta;
  132. for ( int j=0; j<W; j++ )
  133. {
  134. if ( mono )
  135. fl_color( src[0], src[0], src[0] );
  136. else
  137. fl_color( src[0], src[1], src[2] );
  138. CGContextMoveToPoint(fl_gc, X+j, Y+i);
  139. CGContextAddLineToPoint(fl_gc, X+j, Y+i);
  140. CGContextStrokePath(fl_gc);
  141. src += delta;
  142. }
  143. }
  144. }
  145. CGContextSetShouldAntialias(fl_gc, true);
  146. }
  147. void Fl_Quartz_Graphics_Driver::draw_image(const uchar* buf, int x, int y, int w, int h, int d, int l){
  148. innards(buf,x,y,w,h,d,l,(d<3&&d>-3),0,0);
  149. }
  150. void Fl_Quartz_Graphics_Driver::draw_image(Fl_Draw_Image_Cb cb, void* data,
  151. int x, int y, int w, int h,int d) {
  152. innards(0,x,y,w,h,d,0,(d<3&&d>-3),cb,data);
  153. }
  154. void Fl_Quartz_Graphics_Driver::draw_image_mono(const uchar* buf, int x, int y, int w, int h, int d, int l){
  155. innards(buf,x,y,w,h,d,l,1,0,0);
  156. }
  157. void Fl_Quartz_Graphics_Driver::draw_image_mono(Fl_Draw_Image_Cb cb, void* data,
  158. int x, int y, int w, int h,int d) {
  159. innards(0,x,y,w,h,d,0,1,cb,data);
  160. }
  161. void fl_rectf(int x, int y, int w, int h, uchar r, uchar g, uchar b) {
  162. fl_color(r,g,b);
  163. fl_rectf(x,y,w,h);
  164. }
  165. //
  166. // End of "$Id: fl_draw_image_mac.cxx 8581 2011-04-12 11:38:43Z manolo $".
  167. //