New member function Fl_Quartz_Graphics_Driver::draw_CGImage() used internally for all image drawing.

git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3-porting@11165 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
This commit is contained in:
Manolo Gouy 2016-02-13 14:48:13 +00:00
parent c95169ea49
commit 4af616a7a2
7 changed files with 50 additions and 68 deletions

View File

@ -165,8 +165,6 @@ public:
static void q_fill_context(); // fill a Quartz context with current FLTK state
static void q_clear_clipping(); // remove all clipping from a Quartz context
static void q_release_context(Fl_X *x=0); // free all resources associated with fl_gc
static void q_begin_image(CGRect&, int x, int y, int w, int h);
static void q_end_image();
// Cocoa additions
static NSOpenGLPixelFormat *mode_to_NSOpenGLPixelFormat(int mode, const int*); // computes NSOpenGLPixelFormat from Gl window's mode
static NSOpenGLContext* create_GLcontext_for_window(NSOpenGLPixelFormat *pixelformat, NSOpenGLContext *shared_ctx, Fl_Window *window);

View File

@ -330,10 +330,7 @@ void Fl_System_Printer::print_window_part(Fl_Window *win, int x, int y, int w, i
CGImageRef img = Fl_X::CGImage_from_window_rect(win, x, y, w, h);
if (save_front != win) save_front->show();
current->set_current();
CGRect rect = CGRectMake(delta_x, delta_y, w, h);
Fl_X::q_begin_image(rect, 0, 0, w, h);
CGContextDrawImage(fl_gc, rect, img);
Fl_X::q_end_image();
((Fl_Quartz_Graphics_Driver*)driver())->draw_CGImage(img,delta_x, delta_y, w, h, 0,0,w,h);
CFRelease(img);
}

View File

@ -83,6 +83,9 @@ static void clipboard_check(void);
static unsigned make_current_counts = 0; // if > 0, then Fl_Window::make_current() can be called only once
static NSBitmapImageRep* rect_to_NSBitmapImageRep(Fl_Window *win, int x, int y, int w, int h);
// make this available on all platforms to make code maintainability easier
class Fl_Widget *fl_selection_requestor;
int fl_mac_os_version = Fl_X::calc_mac_os_version(); // the version number of the running Mac OS X (e.g., 100604 for 10.6.4)
// public variables
@ -3344,46 +3347,6 @@ void Fl_X::q_release_context(Fl_X *x) {
#endif
}
void Fl_X::q_begin_image(CGRect &rect, int cx, int cy, int w, int h) {
CGContextSaveGState(fl_gc);
CGRect r2 = rect;
r2.origin.x -= 0.5f;
r2.origin.y -= 0.5f;
CGContextClipToRect(fl_gc, r2);
// move graphics context to origin of vertically reversed image
// The 0.5 here cancels the 0.5 offset present in Quartz graphics contexts.
// Thus, image and surface pixels are in phase if there's no scaling.
// Below, we handle x2 and /2 scalings that occur when drawing to
// a double-resolution bitmap, and when drawing a double-resolution bitmap to display.
CGContextTranslateCTM(fl_gc, rect.origin.x - cx - 0.5, rect.origin.y - cy + h - 0.5);
CGContextScaleCTM(fl_gc, 1, -1);
CGAffineTransform at = CGContextGetCTM(fl_gc);
if (at.a == at.d && at.b == 0 && at.c == 0) { // proportional scaling, no rotation
// phase image with display pixels
CGFloat deltax = 0, deltay = 0;
if (at.a == 2) { // make .tx and .ty have even values
deltax = (at.tx/2 - round(at.tx/2));
deltay = (at.ty/2 - round(at.ty/2));
} else if (at.a == 0.5) {
if (Fl_Display_Device::high_resolution()) { // make .tx and .ty have int or half-int values
deltax = (at.tx*2 - round(at.tx*2));
deltay = (at.ty*2 - round(at.ty*2));
} else { // make .tx and .ty have integral values
deltax = (at.tx - round(at.tx))*2;
deltay = (at.ty - round(at.ty))*2;
}
}
CGContextTranslateCTM(fl_gc, -deltax, -deltay);
}
rect.origin.x = rect.origin.y = 0;
rect.size.width = w;
rect.size.height = h;
}
void Fl_X::q_end_image() {
CGContextRestoreGState(fl_gc);
}
void Fl_X::set_high_resolution(bool new_val)
{
Fl_Display_Device::high_res_window_ = new_val;

View File

@ -65,11 +65,8 @@ void Fl_Quartz_Graphics_Driver::copy_offscreen(int x,int y,int w,int h,Fl_Offscr
CGDataProviderRef src_bytes = CGDataProviderCreateWithData( src, data, sw*sh*4, bmProviderRelease);
CGImageRef img = CGImageCreate( sw, sh, 8, 4*8, 4*sw, lut, alpha,
src_bytes, 0L, false, kCGRenderingIntentDefault);
// fl_push_clip();
CGRect rect = CGRectMake(x, y, w, h);
Fl_X::q_begin_image(rect, srcx, srcy, sw, sh);
CGContextDrawImage(fl_gc, rect, img);
Fl_X::q_end_image();
draw_CGImage(img, x, y, w, h, srcx, srcy, sw, sh);
CGImageRelease(img);
CGColorSpaceRelease(lut);
CGDataProviderRelease(src_bytes);
@ -153,7 +150,42 @@ void fl_end_offscreen() {
/** @} */
void Fl_Quartz_Graphics_Driver::draw_CGImage(CGImageRef cgimg, int x, int y, int w, int h, int srcx, int srcy, int sw, int sh)
{
CGRect rect = CGRectMake(x, y, w, h);
CGContextSaveGState(fl_gc);
CGContextClipToRect(fl_gc, CGRectOffset(rect, -0.5, -0.5 ));
// move graphics context to origin of vertically reversed image
// The 0.5 here cancels the 0.5 offset present in Quartz graphics contexts.
// Thus, image and surface pixels are in phase if there's no scaling.
CGContextTranslateCTM(fl_gc, rect.origin.x - srcx - 0.5, rect.origin.y - srcy + sh - 0.5);
CGContextScaleCTM(fl_gc, 1, -1);
CGAffineTransform at = CGContextGetCTM(fl_gc);
if (at.a == at.d && at.b == 0 && at.c == 0) { // proportional scaling, no rotation
// We handle x2 and /2 scalings that occur when drawing to
// a double-resolution bitmap, and when drawing a double-resolution bitmap to display.
bool doit = false;
// phase image with display pixels
CGFloat deltax = 0, deltay = 0;
if (at.a == 2) { // make .tx and .ty have even values
deltax = (at.tx/2 - round(at.tx/2));
deltay = (at.ty/2 - round(at.ty/2));
doit = true;
} else if (at.a == 0.5) {
doit = true;
if (Fl_Display_Device::high_resolution()) { // make .tx and .ty have int or half-int values
deltax = (at.tx*2 - round(at.tx*2));
deltay = (at.ty*2 - round(at.ty*2));
} else { // make .tx and .ty have integral values
deltax = (at.tx - round(at.tx))*2;
deltay = (at.ty - round(at.ty))*2;
}
}
if (doit) CGContextTranslateCTM(fl_gc, -deltax, -deltay);
}
CGContextDrawImage(fl_gc, CGRectMake(0, 0, sw, sh), cgimg);
CGContextRestoreGState(fl_gc);
}
//
// End of "$Id$".

View File

@ -64,6 +64,7 @@ public:
static Fl_Offscreen create_offscreen_with_alpha(int w, int h);
#endif
void copy_offscreen(int x, int y, int w, int h, Fl_Offscreen pixmap, int srcx, int srcy);
void draw_CGImage(CGImageRef cgimg, int x, int y, int w, int h, int srcx, int srcy, int sw, int sh);
protected:
// --- implementation is in src/fl_rect.cxx which includes src/cfg_gfx/quartz_rect.cxx
void point(int x, int y);

View File

@ -90,10 +90,7 @@ static void innards(const uchar *buf, int X, int Y, int W, int H,
src, 0L, false, kCGRenderingIntentDefault);
// draw the image into the destination context
if (img) {
CGRect rect = CGRectMake( X, Y, W, H);
Fl_X::q_begin_image(rect, 0, 0, W, H);
CGContextDrawImage(fl_gc, rect, img);
Fl_X::q_end_image();
((Fl_Quartz_Graphics_Driver*)fl_graphics_driver)->draw_CGImage(img, X,Y,W,H, 0,0,W,H);
// release all allocated resources
CGImageRelease(img);
}
@ -170,10 +167,7 @@ void Fl_Quartz_Graphics_Driver::draw(Fl_Bitmap *bm, int XP, int YP, int WP, int
return;
}
if (bm->id_ && fl_gc) {
CGRect rect = { { (CGFloat)X, (CGFloat)Y }, { (CGFloat)W, (CGFloat)H } };
Fl_X::q_begin_image(rect, cx, cy, bm->w(), bm->h());
CGContextDrawImage(fl_gc, rect, (CGImageRef)bm->id_);
Fl_X::q_end_image();
draw_CGImage((CGImageRef)bm->id_, X,Y,W,H, cx, cy, bm->w(), bm->h());
}
}
@ -249,10 +243,7 @@ void Fl_Quartz_Graphics_Driver::draw(Fl_RGB_Image *img, int XP, int YP, int WP,
CGColorSpaceRelease(lut);
CGDataProviderRelease(src);
}
CGRect rect = CGRectMake(X, Y, W, H);
Fl_X::q_begin_image(rect, cx, cy, img->w(), img->h());
CGContextDrawImage(fl_gc, rect, (CGImageRef)img->id_);
Fl_X::q_end_image();
draw_CGImage((CGImageRef)img->id_, X,Y,W,H, cx,cy, img->w(), img->h());
}
}

View File

@ -24,6 +24,9 @@
#include <FL/Fl.H>
#include <FL/x.H>
#include <FL/fl_draw.H>
#ifdef __APPLE__
#include "drivers/Quartz/Fl_Quartz_Graphics_Driver.h"
#endif
// scroll a rectangle and redraw the newly exposed portions:
/**
@ -144,10 +147,7 @@ void fl_scroll(int X, int Y, int W, int H, int dx, int dy,
#elif defined(__APPLE_QUARTZ__) // PORTME: Fl_Graphics_Driver - platform scrolling
CGImageRef img = Fl_X::CGImage_from_window_rect(Fl_Window::current(), src_x, src_y, src_w, src_h);
if (img) {
CGRect rect = CGRectMake(dest_x, dest_y, src_w, src_h);
Fl_X::q_begin_image(rect, 0, 0, src_w, src_h);
CGContextDrawImage(fl_gc, rect, img);
Fl_X::q_end_image();
((Fl_Quartz_Graphics_Driver*)fl_graphics_driver)->draw_CGImage(img,dest_x,dest_y,src_w,src_h,0,0,src_w,src_h);
CFRelease(img);
}
#elif defined(FL_PORTING)