1 //
2 // "$Id$"
3 //
4 // implementation of class Fl_Gl_Device_Plugin for the Fast Light Tool Kit (FLTK).
5 //
6 // Copyright 2010-2014 by Bill Spitzak and others.
7 //
8 // This library is free software. Distribution and use rights are outlined in
9 // the file "COPYING" which should have been included with this file.  If this
10 // file is missing or damaged, see the license at:
11 //
12 //     http://www.fltk.org/COPYING.php
13 //
14 // Please report all bugs and problems to:
15 //
16 //     http://www.fltk.org/str.php
17 //
18 
19 #include <config.h>
20 #include <FL/Fl_Printer.H>
21 #include <FL/Fl_Gl_Window.H>
22 #include "Fl_Gl_Choice.H"
23 #include <FL/Fl_RGB_Image.H>
24 #include "FL/Fl.H"
25 
26 #if defined(__APPLE__)
convert_BGRA_to_RGB(uchar * baseAddress,int w,int h,int mByteWidth)27 uchar *convert_BGRA_to_RGB(uchar *baseAddress, int w, int h, int mByteWidth)
28 {
29   uchar *newimg = new uchar[3*w*h];
30   uchar *to = newimg;
31   for (int i = 0; i < h; i++) {
32     uchar *from = baseAddress + i * mByteWidth;
33     for (int j = 0; j < w; j++, from += 4) {
34 #if defined(__ppc__) && __ppc__
35       memcpy(to, from + 1, 3);
36       to += 3;
37 #else
38       *(to++) = *(from+2);
39       *(to++) = *(from+1);
40       *(to++) = *from;
41 #endif
42     }
43   }
44   delete[] baseAddress;
45   return newimg;
46 }
47 #endif
48 
capture_gl_rectangle(Fl_Gl_Window * glw,int x,int y,int w,int h)49 static Fl_RGB_Image* capture_gl_rectangle(Fl_Gl_Window *glw, int x, int y, int w, int h)
50 /* captures a rectangle of a Fl_Gl_Window window, and returns it as a RGB image
51  stored from bottom to top.
52  */
53 {
54 #if defined(__APPLE__)
55   const int bytesperpixel = 4;
56   float factor = glw->pixels_per_unit();
57   if (factor > 1) {
58     w *= factor; h *= factor; x *= factor; y *= factor;
59   }
60 #else
61   const int bytesperpixel = 3;
62 #endif
63   glw->flush(); // forces a GL redraw, necessary for the glpuzzle demo
64   // Read OpenGL context pixels directly.
65   // For extra safety, save & restore OpenGL states that are changed
66   glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
67   glPixelStorei(GL_PACK_ALIGNMENT, 4); /* Force 4-byte alignment */
68   glPixelStorei(GL_PACK_ROW_LENGTH, 0);
69   glPixelStorei(GL_PACK_SKIP_ROWS, 0);
70   glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
71   // Read a block of pixels from the frame buffer
72   int mByteWidth = w * bytesperpixel;
73   mByteWidth = (mByteWidth + 3) & ~3;    // Align to 4 bytes
74   uchar *baseAddress = new uchar[mByteWidth * h];
75   glReadPixels(x, glw->pixel_h() - (y+h), w, h,
76 #if defined(__APPLE__)
77                GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV,
78 #else
79                GL_RGB, GL_UNSIGNED_BYTE,
80 #endif
81                baseAddress);
82   glPopClientAttrib();
83 #if defined(__APPLE__)
84   baseAddress = convert_BGRA_to_RGB(baseAddress, w, h, mByteWidth);
85   mByteWidth = 3 * w;
86 #endif
87   Fl_RGB_Image *img = new Fl_RGB_Image(baseAddress, w, h, 3, mByteWidth);
88   img->alloc_array = 1;
89   return img;
90 }
91 
92 #ifdef __APPLE__
imgProviderReleaseData(void * info,const void * data,size_t size)93 static void imgProviderReleaseData (void *info, const void *data, size_t size)
94 {
95   delete (Fl_RGB_Image *)info;
96 }
97 #endif
98 
99 /**
100  This class will make sure that OpenGL printing/screen capture is available if fltk_gl
101  was linked to the program
102  */
103 class Fl_Gl_Device_Plugin : public Fl_Device_Plugin {
104 public:
Fl_Gl_Device_Plugin()105   Fl_Gl_Device_Plugin() : Fl_Device_Plugin(name()) { }
name()106   virtual const char *name() { return "opengl.device.fltk.org"; }
print(Fl_Widget * w,int x,int y,int height)107   virtual int print(Fl_Widget *w, int x, int y, int height /*useless*/) {
108     Fl_Gl_Window *glw = w->as_gl_window();
109     if (!glw) return 0;
110     Fl_RGB_Image *img = capture_gl_rectangle(glw, 0, 0, glw->w(), glw->h());
111 #ifdef __APPLE__
112     if (Fl_Surface_Device::surface()->class_name() == Fl_Printer::class_id) {
113       // convert the image to CGImage, and draw it at full res (useful on retina display)
114       CGColorSpaceRef cSpace = CGColorSpaceCreateDeviceRGB();
115       CGDataProviderRef provider = CGDataProviderCreateWithData(img, img->array, img->ld() * img->h(), imgProviderReleaseData);
116       CGImageRef cgimg = CGImageCreate(img->w(), img->h(), 8, 24, img->ld(), cSpace,
117                                      (CGBitmapInfo)(kCGImageAlphaNone),
118                                      provider, NULL, false, kCGRenderingIntentDefault);
119       CGColorSpaceRelease(cSpace);
120       CGDataProviderRelease(provider);
121       CGContextDrawImage(fl_gc, CGRectMake(0, 0, glw->w(), glw->h()), cgimg);
122       CFRelease(cgimg);
123       return 1;
124     } else if (img->w() > glw->w()) {
125       Fl_RGB_Image *img2 = (Fl_RGB_Image*)img->copy(glw->w(), glw->h());
126       delete img;
127       img = img2;
128     }
129 #endif
130     int ld = img->ld() ? img->ld() : img->w() * img->d();
131     fl_draw_image(img->array + (img->h() - 1) * ld, x, y , img->w(), img->h(), 3, - ld);
132     delete img;
133     return 1;
134   }
rectangle_capture(Fl_Widget * widget,int x,int y,int w,int h)135   virtual Fl_RGB_Image* rectangle_capture(Fl_Widget *widget, int x, int y, int w, int h) {
136     Fl_Gl_Window *glw = widget->as_gl_window();
137     if (!glw) return NULL;
138     return capture_gl_rectangle(glw, x, y, w, h);
139   }
140 };
141 
142 static Fl_Gl_Device_Plugin Gl_Device_Plugin;
143 
144 // The purpose of this variable, used in Fl_Gl_Window.cxx, is only to force this file to be loaded
145 // whenever Fl_Gl_Window.cxx is loaded, that is, whenever fltk_gl is.
146 FL_EXPORT int fl_gl_load_plugin = 0;
147 
148 //
149 // End of "$Id$".
150 //
151