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