|
| 1 | +/* Copyright (C) 2003-2013 Runtime Revolution Ltd. |
| 2 | + |
| 3 | + This file is part of LiveCode. |
| 4 | + |
| 5 | + LiveCode is free software; you can redistribute it and/or modify it under |
| 6 | + the terms of the GNU General Public License v3 as published by the Free |
| 7 | + Software Foundation. |
| 8 | + |
| 9 | + LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY |
| 10 | + WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + for more details. |
| 13 | + |
| 14 | + You should have received a copy of the GNU General Public License |
| 15 | + along with LiveCode. If not see <http://www.gnu.org/licenses/>. */ |
| 16 | + |
| 17 | +#include "graphics.h" |
| 18 | + |
| 19 | +#include "graphics_util.h" |
| 20 | + |
| 21 | +//////////////////////////////////////////////////////////////////////////////// |
| 22 | + |
| 23 | +// IM-2014-10-22: [[ Bug 13746 ]] Set the given rect of the raster to transparent black |
| 24 | +void MCGRasterClearRect(MCGRaster &x_raster, const MCGIntegerRectangle &p_rect) |
| 25 | +{ |
| 26 | + MCGIntegerRectangle t_coverage; |
| 27 | + t_coverage = MCGIntegerRectangleIntersection(p_rect, MCGIntegerRectangleMake(0, 0, x_raster.width, x_raster.height)); |
| 28 | + |
| 29 | + if (MCGIntegerRectangleIsEmpty(t_coverage)) |
| 30 | + return; |
| 31 | + |
| 32 | + uint8_t *t_pixel_ptr; |
| 33 | + t_pixel_ptr = (uint8_t*)x_raster.pixels; |
| 34 | + |
| 35 | + if (t_coverage.origin.y > 0) |
| 36 | + t_pixel_ptr += t_coverage.origin.y * x_raster.stride; |
| 37 | + if (t_coverage.origin.x > 0) |
| 38 | + t_pixel_ptr += t_coverage.origin.x * sizeof(uint32_t); |
| 39 | + |
| 40 | + uint32_t t_width; |
| 41 | + t_width = t_coverage.size.width * sizeof(uint32_t); |
| 42 | + |
| 43 | + for (uint32_t y = 0; y < t_coverage.size.height; y++) |
| 44 | + { |
| 45 | + MCMemoryClear(t_pixel_ptr, t_width); |
| 46 | + t_pixel_ptr += x_raster.stride; |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// IM-2014-10-03: [[ Bug 13432 ]] Set the alpha values of the opaque image from the alpha raster, premultiplying the pixels |
| 51 | +void MCGRasterApplyAlpha(MCGRaster &x_raster, const MCGRaster &p_alpha, const MCGIntegerPoint &p_offset) |
| 52 | +{ |
| 53 | + MCAssert(p_alpha.format == kMCGRasterFormat_A); |
| 54 | + MCAssert(x_raster.format == kMCGRasterFormat_xRGB); |
| 55 | + |
| 56 | + MCGIntegerRectangle t_coverage; |
| 57 | + t_coverage = MCGIntegerRectangleIntersection(MCGIntegerRectangleMake(0, 0, x_raster.width, x_raster.height), |
| 58 | + MCGIntegerRectangleMake(p_offset.x, p_offset.y, p_alpha.width, p_alpha.height)); |
| 59 | + |
| 60 | + if (MCGIntegerRectangleIsEmpty(t_coverage)) |
| 61 | + { |
| 62 | + // IM-2014-10-22: [[ Bug 13746 ]] If the mask is not within the region of the raster then blank out everything |
| 63 | + MCGRasterClearRect(x_raster, MCGIntegerRectangleMake(0, 0, x_raster.width, x_raster.height)); |
| 64 | + x_raster.format = kMCGRasterFormat_ARGB; |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + // IM-2014-10-22: [[ Bug 13746 ]] Areas outside the mask need to be blanked out |
| 69 | + int32_t t_left, t_top, t_right, t_bottom; |
| 70 | + t_left = t_coverage.origin.x; |
| 71 | + t_top = t_coverage.origin.y; |
| 72 | + t_right = t_coverage.origin.x + t_coverage.size.width; |
| 73 | + t_bottom = t_coverage.origin.y + t_coverage.size.height; |
| 74 | + |
| 75 | + MCGRasterClearRect(x_raster, MCGIntegerRectangleMakeLTRB(0, 0, x_raster.width, t_top)); |
| 76 | + MCGRasterClearRect(x_raster, MCGIntegerRectangleMakeLTRB(0, t_bottom, x_raster.width, x_raster.height)); |
| 77 | + MCGRasterClearRect(x_raster, MCGIntegerRectangleMakeLTRB(0, t_top, t_left, t_bottom)); |
| 78 | + MCGRasterClearRect(x_raster, MCGIntegerRectangleMakeLTRB(t_right, t_top, x_raster.width, t_bottom)); |
| 79 | + |
| 80 | + uint8_t *t_alpha_ptr; |
| 81 | + t_alpha_ptr = (uint8_t*)p_alpha.pixels; |
| 82 | + |
| 83 | + if (p_offset.y < 0) |
| 84 | + t_alpha_ptr += p_alpha.stride * (-p_offset.y); |
| 85 | + if (p_offset.x < 0) |
| 86 | + t_alpha_ptr += (-p_offset.x); |
| 87 | + |
| 88 | + uint8_t *t_pixel_ptr; |
| 89 | + t_pixel_ptr = (uint8_t*)x_raster.pixels; |
| 90 | + |
| 91 | + if (t_coverage.origin.y > 0) |
| 92 | + t_pixel_ptr += x_raster.stride * t_coverage.origin.y; |
| 93 | + if (t_coverage.origin.x > 0) |
| 94 | + t_pixel_ptr += t_coverage.origin.x * sizeof(uint32_t); |
| 95 | + |
| 96 | + for (uint32_t y = 0; y < t_coverage.size.height; y++) |
| 97 | + { |
| 98 | + uint8_t *t_alpha_row; |
| 99 | + t_alpha_row = t_alpha_ptr; |
| 100 | + |
| 101 | + uint32_t *t_pixel_row; |
| 102 | + t_pixel_row = (uint32_t*)t_pixel_ptr; |
| 103 | + |
| 104 | + for (uint32_t x = 0; x < t_coverage.size.width; x++) |
| 105 | + { |
| 106 | + uint32_t t_pixel; |
| 107 | + t_pixel = *t_pixel_row; |
| 108 | + |
| 109 | + uint8_t t_alpha; |
| 110 | + t_alpha = *t_alpha_row++; |
| 111 | + |
| 112 | + *t_pixel_row++ = MCGPixelPreMultiplyNative(MCGPixelSetNativeAlpha(t_pixel, t_alpha)); |
| 113 | + } |
| 114 | + // |
| 115 | + |
| 116 | + t_alpha_ptr += p_alpha.stride; |
| 117 | + t_pixel_ptr += x_raster.stride; |
| 118 | + } |
| 119 | + |
| 120 | + x_raster.format = kMCGRasterFormat_ARGB; |
| 121 | +} |
| 122 | + |
| 123 | +//////////////////////////////////////////////////////////////////////////////// |
0 commit comments