diff --git a/src/Fl_Gl_Device_Plugin.cxx b/src/Fl_Gl_Device_Plugin.cxx index 1ab104cae..269a9ef02 100644 --- a/src/Fl_Gl_Device_Plugin.cxx +++ b/src/Fl_Gl_Device_Plugin.cxx @@ -57,7 +57,6 @@ uchar *convert_BGRA_to_RGB(uchar *baseAddress, int w, int h, int mByteWidth) static Fl_RGB_Image* capture_gl_rectangle(Fl_Gl_Window *glw, int x, int y, int w, int h) /* captures a rectangle of a Fl_Gl_Window window, and returns it as a RGB image - stored from bottom to top. */ { #if defined(__APPLE__) // PORTME: Fl_Surface_Driver - platform OpenGL management @@ -93,6 +92,18 @@ static Fl_RGB_Image* capture_gl_rectangle(Fl_Gl_Window *glw, int x, int y, int w baseAddress = convert_BGRA_to_RGB(baseAddress, w, h, mByteWidth); mByteWidth = 3 * w; #endif + + // GL gives a bottom-to-top image, convert it to top-to-bottom + uchar *tmp = new uchar[mByteWidth]; + uchar *p = baseAddress ; + uchar *q = baseAddress + (h-1)*mByteWidth; + for (int i = 0; i < h/2; i++, p += mByteWidth, q -= mByteWidth) { + memcpy(tmp, p, mByteWidth); + memcpy(p, q, mByteWidth); + memcpy(q, tmp, mByteWidth); + } + delete[] tmp; + Fl_RGB_Image *img = new Fl_RGB_Image(baseAddress, w, h, 3, mByteWidth); img->alloc_array = 1; return img; @@ -110,17 +121,6 @@ public: Fl_Gl_Window *glw = w->as_gl_window(); if (!glw) return 0; Fl_RGB_Image *img = capture_gl_rectangle(glw, 0, 0, glw->w(), glw->h()); - // turn img upside-down - int ld = img->ld() ? img->ld() : img->w() * img->d(); - uchar *tmp = new uchar[ld]; - uchar *p = (uchar*)img->array ; - uchar *q = (uchar*)img->array + (img->h()-1)*ld; - for (int i = 0; i < img->h()/2; i++, p += ld, q -= ld) { - memcpy(tmp, p, ld); - memcpy(p, q, ld); - memcpy(q, tmp, ld); - } - delete[] tmp; Fl_Shared_Image *shared = Fl_Shared_Image::get(img); shared->scale(glw->w(), glw->h()); shared->draw(x, y); diff --git a/src/Fl_cocoa.mm b/src/Fl_cocoa.mm index 42e85fd69..bbd26c389 100644 --- a/src/Fl_cocoa.mm +++ b/src/Fl_cocoa.mm @@ -4091,7 +4091,7 @@ static NSBitmapImageRep* GL_rect_to_nsbitmap(Fl_Window *win, int x, int y, int w NSBitmapImageRep* bitmap = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL pixelsWide:img->w() pixelsHigh:img->h() bitsPerSample:8 samplesPerPixel:4 hasAlpha:YES isPlanar:NO colorSpaceName:NSDeviceRGBColorSpace bytesPerRow:4*img->w() bitsPerPixel:32]; memset([bitmap bitmapData], 0xFF, [bitmap bytesPerPlane]); const uchar *from = img->array; - for (int r = img->h() - 1; r >= 0; r--) { + for (int r = 0; r < img->h(); r++) { uchar *to = [bitmap bitmapData] + r * [bitmap bytesPerRow]; for (int c = 0; c < img->w(); c++) { memcpy(to, from, 3); diff --git a/src/fl_read_image.cxx b/src/fl_read_image.cxx index 3a4d8ede3..ebf43c324 100644 --- a/src/fl_read_image.cxx +++ b/src/fl_read_image.cxx @@ -45,14 +45,14 @@ static uchar *read_win_rectangle(uchar *p, int X, int Y, int w, int h, int alpha static void write_image_inside(Fl_RGB_Image *to, Fl_RGB_Image *from, int to_x, int to_y) /* Copy the image "from" inside image "to" with its top-left angle at coordinates to_x, to_y. - Also, exchange top and bottom of "from". Image depth can differ between "to" and "from". + Image depth can differ between "to" and "from". */ { int to_ld = (to->ld() == 0? to->w() * to->d() : to->ld()); int from_ld = (from->ld() == 0? from->w() * from->d() : from->ld()); uchar *tobytes = (uchar*)to->array + to_y * to_ld + to_x * to->d(); - const uchar *frombytes = from->array + (from->h() - 1) * from_ld; - for (int i = from->h() - 1; i >= 0; i--) { + const uchar *frombytes = from->array; + for (int i = 0; i < from->h(); i++) { if (from->d() == to->d()) memcpy(tobytes, frombytes, from->w() * from->d()); else { for (int j = 0; j < from->w(); j++) { @@ -60,7 +60,7 @@ static void write_image_inside(Fl_RGB_Image *to, Fl_RGB_Image *from, int to_x, i } } tobytes += to_ld; - frombytes -= from_ld; + frombytes += from_ld; } } @@ -92,9 +92,9 @@ static Fl_RGB_Image *traverse_to_gl_subwindows(Fl_Group *g, uchar *p, int x, int Fl_Plugin_Manager pm("fltk:device"); Fl_Device_Plugin *pi = (Fl_Device_Plugin*)pm.plugin("opengl.device.fltk.org"); if (!pi) return full_img; - Fl_RGB_Image *img = pi->rectangle_capture(g, x, y, w, h); // bottom to top image - if (full_img) full_img = img; // top and bottom will be exchanged later - else { // exchange top and bottom to get a proper FLTK image + Fl_RGB_Image *img = pi->rectangle_capture(g, x, y, w, h); + if (full_img) full_img = img; + else { uchar *data = ( p ? p : new uchar[img->w() * img->h() * (alpha?4:3)] ); full_img = new Fl_RGB_Image(data, img->w(), img->h(), alpha?4:3); if (!p) full_img->alloc_array = 1;