Pierre Ossman | 403ac27 | 2017-01-02 17:00:41 +0100 | [diff] [blame] | 1 | /* Copyright 2016 Pierre Ossman for Cendio AB |
| 2 | * |
| 3 | * This is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This software is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this software; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 16 | * USA. |
| 17 | */ |
| 18 | |
| 19 | #include <assert.h> |
| 20 | |
| 21 | #include <ApplicationServices/ApplicationServices.h> |
| 22 | |
| 23 | #include <FL/Fl_RGB_Image.H> |
| 24 | #include <FL/Fl_Window.H> |
| 25 | #include <FL/x.H> |
| 26 | |
| 27 | #include <rdr/Exception.h> |
| 28 | |
| 29 | #include "Surface.h" |
| 30 | |
Pierre Ossman | 455566e | 2017-02-10 16:37:52 +0100 | [diff] [blame] | 31 | static void render(CGContextRef gc, CGImageRef image, |
| 32 | CGBlendMode mode, CGFloat alpha, |
Pierre Ossman | 2d0dc3a | 2017-04-27 13:28:49 +0200 | [diff] [blame^] | 33 | int src_x, int src_y, |
Pierre Ossman | 3d74d88 | 2017-01-02 19:49:52 +0100 | [diff] [blame] | 34 | int x, int y, int w, int h) |
| 35 | { |
| 36 | CGRect rect; |
Pierre Ossman | 2d0dc3a | 2017-04-27 13:28:49 +0200 | [diff] [blame^] | 37 | CGImageRef subimage; |
| 38 | |
| 39 | rect.origin.x = src_x; |
| 40 | rect.origin.y = src_y; |
| 41 | rect.size.width = w; |
| 42 | rect.size.height = h; |
| 43 | |
| 44 | subimage = CGImageCreateWithImageInRect(image, rect); |
| 45 | if (!subimage) |
| 46 | throw rdr::Exception("CGImageCreateImageWithImageInRect"); |
Pierre Ossman | 3d74d88 | 2017-01-02 19:49:52 +0100 | [diff] [blame] | 47 | |
| 48 | CGContextSaveGState(gc); |
| 49 | |
Pierre Ossman | de6a580 | 2017-01-02 20:07:10 +0100 | [diff] [blame] | 50 | CGContextSetBlendMode(gc, mode); |
Pierre Ossman | 455566e | 2017-02-10 16:37:52 +0100 | [diff] [blame] | 51 | CGContextSetAlpha(gc, alpha); |
Pierre Ossman | de6a580 | 2017-01-02 20:07:10 +0100 | [diff] [blame] | 52 | |
Pierre Ossman | 3d74d88 | 2017-01-02 19:49:52 +0100 | [diff] [blame] | 53 | rect.origin.x = x; |
| 54 | rect.origin.y = y; |
| 55 | rect.size.width = w; |
| 56 | rect.size.height = h; |
| 57 | |
Pierre Ossman | 2d0dc3a | 2017-04-27 13:28:49 +0200 | [diff] [blame^] | 58 | CGContextDrawImage(gc, rect, subimage); |
Pierre Ossman | 3d74d88 | 2017-01-02 19:49:52 +0100 | [diff] [blame] | 59 | |
| 60 | CGContextRestoreGState(gc); |
Pierre Ossman | 2d0dc3a | 2017-04-27 13:28:49 +0200 | [diff] [blame^] | 61 | |
| 62 | CGImageRelease(subimage); |
Pierre Ossman | 3d74d88 | 2017-01-02 19:49:52 +0100 | [diff] [blame] | 63 | } |
| 64 | |
Pierre Ossman | de6a580 | 2017-01-02 20:07:10 +0100 | [diff] [blame] | 65 | static CGContextRef make_bitmap(int width, int height, unsigned char* data) |
| 66 | { |
| 67 | CGColorSpaceRef lut; |
| 68 | CGContextRef bitmap; |
| 69 | |
| 70 | lut = CGDisplayCopyColorSpace(kCGDirectMainDisplay); |
| 71 | if (!lut) { |
| 72 | lut = CGColorSpaceCreateDeviceRGB(); |
| 73 | if (!lut) |
| 74 | throw rdr::Exception("CGColorSpaceCreateDeviceRGB"); |
| 75 | } |
| 76 | |
| 77 | bitmap = CGBitmapContextCreate(data, width, height, 8, width*4, lut, |
| 78 | kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little); |
| 79 | CGColorSpaceRelease(lut); |
| 80 | if (!bitmap) |
| 81 | throw rdr::Exception("CGBitmapContextCreate"); |
| 82 | |
| 83 | return bitmap; |
| 84 | } |
| 85 | |
Pierre Ossman | 403ac27 | 2017-01-02 17:00:41 +0100 | [diff] [blame] | 86 | void Surface::clear(unsigned char r, unsigned char g, unsigned char b, unsigned char a) |
| 87 | { |
| 88 | unsigned char* out; |
| 89 | int x, y; |
| 90 | |
| 91 | r = (unsigned)r * a / 255; |
| 92 | g = (unsigned)g * a / 255; |
| 93 | b = (unsigned)b * a / 255; |
| 94 | |
| 95 | out = data; |
| 96 | for (y = 0;y < width();y++) { |
| 97 | for (x = 0;x < height();x++) { |
| 98 | *out++ = b; |
| 99 | *out++ = g; |
| 100 | *out++ = r; |
| 101 | *out++ = a; |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | void Surface::draw(int src_x, int src_y, int x, int y, int w, int h) |
| 107 | { |
Pierre Ossman | 403ac27 | 2017-01-02 17:00:41 +0100 | [diff] [blame] | 108 | CGContextSaveGState(fl_gc); |
| 109 | |
| 110 | // Reset the transformation matrix back to the default identity |
| 111 | // matrix as otherwise we get a massive performance hit |
| 112 | CGContextConcatCTM(fl_gc, CGAffineTransformInvert(CGContextGetCTM(fl_gc))); |
| 113 | |
| 114 | // macOS Coordinates are from bottom left, not top left |
Pierre Ossman | 403ac27 | 2017-01-02 17:00:41 +0100 | [diff] [blame] | 115 | y = Fl_Window::current()->h() - (y + h); |
| 116 | |
Pierre Ossman | 455566e | 2017-02-10 16:37:52 +0100 | [diff] [blame] | 117 | render(fl_gc, image, kCGBlendModeCopy, 1.0, |
Pierre Ossman | 2d0dc3a | 2017-04-27 13:28:49 +0200 | [diff] [blame^] | 118 | src_x, src_y, x, y, w, h); |
Pierre Ossman | 403ac27 | 2017-01-02 17:00:41 +0100 | [diff] [blame] | 119 | |
| 120 | CGContextRestoreGState(fl_gc); |
| 121 | } |
| 122 | |
Pierre Ossman | 3d74d88 | 2017-01-02 19:49:52 +0100 | [diff] [blame] | 123 | void Surface::draw(Surface* dst, int src_x, int src_y, int x, int y, int w, int h) |
| 124 | { |
Pierre Ossman | 3d74d88 | 2017-01-02 19:49:52 +0100 | [diff] [blame] | 125 | CGContextRef bitmap; |
| 126 | |
Pierre Ossman | de6a580 | 2017-01-02 20:07:10 +0100 | [diff] [blame] | 127 | bitmap = make_bitmap(dst->width(), dst->height(), dst->data); |
Pierre Ossman | 3d74d88 | 2017-01-02 19:49:52 +0100 | [diff] [blame] | 128 | |
| 129 | // macOS Coordinates are from bottom left, not top left |
Pierre Ossman | 3d74d88 | 2017-01-02 19:49:52 +0100 | [diff] [blame] | 130 | y = dst->height() - (y + h); |
| 131 | |
Pierre Ossman | 455566e | 2017-02-10 16:37:52 +0100 | [diff] [blame] | 132 | render(bitmap, image, kCGBlendModeCopy, 1.0, |
Pierre Ossman | 2d0dc3a | 2017-04-27 13:28:49 +0200 | [diff] [blame^] | 133 | src_x, src_y, x, y, w, h); |
Pierre Ossman | de6a580 | 2017-01-02 20:07:10 +0100 | [diff] [blame] | 134 | |
| 135 | CGContextRelease(bitmap); |
| 136 | } |
| 137 | |
Pierre Ossman | 455566e | 2017-02-10 16:37:52 +0100 | [diff] [blame] | 138 | void Surface::blend(int src_x, int src_y, int x, int y, int w, int h, int a) |
Pierre Ossman | de6a580 | 2017-01-02 20:07:10 +0100 | [diff] [blame] | 139 | { |
| 140 | CGContextSaveGState(fl_gc); |
| 141 | |
| 142 | // Reset the transformation matrix back to the default identity |
| 143 | // matrix as otherwise we get a massive performance hit |
| 144 | CGContextConcatCTM(fl_gc, CGAffineTransformInvert(CGContextGetCTM(fl_gc))); |
| 145 | |
| 146 | // macOS Coordinates are from bottom left, not top left |
Pierre Ossman | de6a580 | 2017-01-02 20:07:10 +0100 | [diff] [blame] | 147 | y = Fl_Window::current()->h() - (y + h); |
| 148 | |
Pierre Ossman | 455566e | 2017-02-10 16:37:52 +0100 | [diff] [blame] | 149 | render(fl_gc, image, kCGBlendModeNormal, (CGFloat)a/255.0, |
Pierre Ossman | 2d0dc3a | 2017-04-27 13:28:49 +0200 | [diff] [blame^] | 150 | src_x, src_y, x, y, w, h); |
Pierre Ossman | de6a580 | 2017-01-02 20:07:10 +0100 | [diff] [blame] | 151 | |
| 152 | CGContextRestoreGState(fl_gc); |
| 153 | } |
| 154 | |
Pierre Ossman | 455566e | 2017-02-10 16:37:52 +0100 | [diff] [blame] | 155 | void Surface::blend(Surface* dst, int src_x, int src_y, int x, int y, int w, int h, int a) |
Pierre Ossman | de6a580 | 2017-01-02 20:07:10 +0100 | [diff] [blame] | 156 | { |
| 157 | CGContextRef bitmap; |
| 158 | |
| 159 | bitmap = make_bitmap(dst->width(), dst->height(), dst->data); |
| 160 | |
| 161 | // macOS Coordinates are from bottom left, not top left |
Pierre Ossman | de6a580 | 2017-01-02 20:07:10 +0100 | [diff] [blame] | 162 | y = dst->height() - (y + h); |
| 163 | |
Pierre Ossman | 455566e | 2017-02-10 16:37:52 +0100 | [diff] [blame] | 164 | render(bitmap, image, kCGBlendModeNormal, (CGFloat)a/255.0, |
Pierre Ossman | 2d0dc3a | 2017-04-27 13:28:49 +0200 | [diff] [blame^] | 165 | src_x, src_y, x, y, w, h); |
Pierre Ossman | 3d74d88 | 2017-01-02 19:49:52 +0100 | [diff] [blame] | 166 | |
| 167 | CGContextRelease(bitmap); |
| 168 | } |
| 169 | |
Pierre Ossman | 403ac27 | 2017-01-02 17:00:41 +0100 | [diff] [blame] | 170 | void Surface::alloc() |
| 171 | { |
| 172 | CGColorSpaceRef lut; |
| 173 | CGDataProviderRef provider; |
| 174 | |
| 175 | data = new unsigned char[width() * height() * 4]; |
| 176 | |
| 177 | lut = CGDisplayCopyColorSpace(kCGDirectMainDisplay); |
| 178 | if (!lut) { |
| 179 | lut = CGColorSpaceCreateDeviceRGB(); |
| 180 | if (!lut) |
| 181 | throw rdr::Exception("CGColorSpaceCreateDeviceRGB"); |
| 182 | } |
| 183 | |
| 184 | provider = CGDataProviderCreateWithData(NULL, data, |
| 185 | width() * height() * 4, NULL); |
| 186 | if (!provider) |
| 187 | throw rdr::Exception("CGDataProviderCreateWithData"); |
| 188 | |
| 189 | image = CGImageCreate(width(), height(), 8, 32, width() * 4, lut, |
Pierre Ossman | de6a580 | 2017-01-02 20:07:10 +0100 | [diff] [blame] | 190 | kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little, |
Pierre Ossman | 403ac27 | 2017-01-02 17:00:41 +0100 | [diff] [blame] | 191 | provider, NULL, false, kCGRenderingIntentDefault); |
| 192 | CGColorSpaceRelease(lut); |
| 193 | CGDataProviderRelease(provider); |
| 194 | if (!image) |
| 195 | throw rdr::Exception("CGImageCreate"); |
| 196 | } |
| 197 | |
| 198 | void Surface::dealloc() |
| 199 | { |
| 200 | CGImageRelease(image); |
| 201 | delete [] data; |
| 202 | } |
| 203 | |
| 204 | void Surface::update(const Fl_RGB_Image* image) |
| 205 | { |
| 206 | int x, y; |
| 207 | const unsigned char* in; |
| 208 | unsigned char* out; |
| 209 | |
| 210 | assert(image->w() == width()); |
| 211 | assert(image->h() == height()); |
| 212 | |
| 213 | // Convert data and pre-multiply alpha |
| 214 | in = (const unsigned char*)image->data()[0]; |
| 215 | out = data; |
| 216 | for (y = 0;y < image->h();y++) { |
| 217 | for (x = 0;x < image->w();x++) { |
| 218 | switch (image->d()) { |
| 219 | case 1: |
| 220 | *out++ = in[0]; |
| 221 | *out++ = in[0]; |
| 222 | *out++ = in[0]; |
| 223 | *out++ = 0xff; |
| 224 | break; |
| 225 | case 2: |
| 226 | *out++ = (unsigned)in[0] * in[1] / 255; |
| 227 | *out++ = (unsigned)in[0] * in[1] / 255; |
| 228 | *out++ = (unsigned)in[0] * in[1] / 255; |
| 229 | *out++ = in[1]; |
| 230 | break; |
| 231 | case 3: |
| 232 | *out++ = in[2]; |
| 233 | *out++ = in[1]; |
| 234 | *out++ = in[0]; |
| 235 | *out++ = 0xff; |
| 236 | break; |
| 237 | case 4: |
| 238 | *out++ = (unsigned)in[2] * in[3] / 255; |
| 239 | *out++ = (unsigned)in[1] * in[3] / 255; |
| 240 | *out++ = (unsigned)in[0] * in[3] / 255; |
| 241 | *out++ = in[3]; |
| 242 | break; |
| 243 | } |
| 244 | in += image->d(); |
| 245 | } |
| 246 | if (image->ld() != 0) |
| 247 | in += image->ld() - image->w() * image->d(); |
| 248 | } |
| 249 | } |