1 /* 2 * Copyright (C) Research In Motion Limited 2009. All rights reserved. 3 * 4 * This library is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Library General Public 6 * License as published by the Free Software Foundation; either 7 * version 2 of the License, or (at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * Library General Public License for more details. 13 * 14 * You should have received a copy of the GNU Library General Public License 15 * along with this library; see the file COPYING.LIB. If not, write to 16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 17 * Boston, MA 02110-1301, USA. 18 */ 19 20 #include "config.h" 21 #include "VGUtils.h" 22 23 #include "AffineTransform.h" 24 #include "FloatRect.h" 25 #include "TransformationMatrix.h" 26 27 namespace WebCore { 28 29 VGMatrix::VGMatrix(const VGfloat data[9]) 30 { 31 m_data[0] = data[0]; 32 m_data[1] = data[1]; 33 m_data[2] = data[2]; 34 m_data[3] = data[3]; 35 m_data[4] = data[4]; 36 m_data[5] = data[5]; 37 m_data[6] = data[6]; 38 m_data[7] = data[7]; 39 m_data[8] = data[8]; 40 } 41 42 VGMatrix::VGMatrix(const AffineTransform& transformation) 43 { 44 m_data[0] = transformation.a(); 45 m_data[1] = transformation.b(); 46 m_data[2] = 0; 47 m_data[3] = transformation.c(); 48 m_data[4] = transformation.d(); 49 m_data[5] = 0; 50 m_data[6] = transformation.e(); 51 m_data[7] = transformation.f(); 52 m_data[8] = 1; 53 } 54 55 VGMatrix::VGMatrix(const TransformationMatrix& matrix) 56 { 57 m_data[0] = matrix.m11(); 58 m_data[1] = matrix.m12(); 59 m_data[2] = matrix.m14(); 60 m_data[3] = matrix.m21(); 61 m_data[4] = matrix.m22(); 62 m_data[5] = matrix.m24(); 63 m_data[6] = matrix.m41(); 64 m_data[7] = matrix.m42(); 65 m_data[8] = matrix.m44(); 66 } 67 68 VGMatrix::operator AffineTransform() const 69 { 70 AffineTransform transformation( 71 m_data[0], m_data[1], 72 m_data[3], m_data[4], 73 m_data[6], m_data[7]); 74 return transformation; 75 } 76 77 VGMatrix::operator TransformationMatrix() const 78 { 79 TransformationMatrix matrix( 80 m_data[0], m_data[1], 0, m_data[2], 81 m_data[3], m_data[4], 0, m_data[5], 82 0, 0, 1, 0, 83 m_data[6], m_data[7], 0, m_data[8]); 84 return matrix; 85 } 86 87 AffineTransform::operator VGMatrix() const 88 { 89 return VGMatrix(*this); 90 } 91 92 TransformationMatrix::operator VGMatrix() const 93 { 94 return VGMatrix(*this); 95 } 96 97 VGRect::VGRect(const VGfloat data[4]) 98 { 99 m_data[0] = data[0]; 100 m_data[1] = data[1]; 101 m_data[2] = data[2]; 102 m_data[3] = data[3]; 103 } 104 105 VGRect::VGRect(const FloatRect& rect) 106 { 107 m_data[0] = rect.x(); 108 m_data[1] = rect.y(); 109 m_data[2] = rect.width(); 110 m_data[3] = rect.height(); 111 } 112 113 VGRect::operator FloatRect() const 114 { 115 return FloatRect(m_data[0], m_data[1], m_data[2], m_data[3]); 116 } 117 118 FloatRect::operator VGRect() const 119 { 120 return VGRect(*this); 121 } 122 123 int VGUtils::bytesForImage(VGImageFormat format, VGint width, VGint height) 124 { 125 return width * height * imageFormatBitsPerPixel(format) / 8; 126 } 127 128 int VGUtils::bytesForImageScanline(VGImageFormat format, VGint width) 129 { 130 int bits = width * imageFormatBitsPerPixel(format); 131 if (bits % 8 > 1) // If unaligned, round up to the next byte. 132 bits += 8 - (bits % 8); 133 134 return bits / 8; 135 } 136 137 int VGUtils::imageFormatBitsPerPixel(VGImageFormat format) 138 { 139 switch (format) { 140 case VG_sRGBX_8888: 141 case VG_sRGBA_8888: 142 case VG_sRGBA_8888_PRE: 143 case VG_lRGBX_8888: 144 case VG_lRGBA_8888: 145 case VG_lRGBA_8888_PRE: 146 case VG_sXRGB_8888: 147 case VG_sARGB_8888: 148 case VG_sARGB_8888_PRE: 149 case VG_lXRGB_8888: 150 case VG_lARGB_8888: 151 case VG_lARGB_8888_PRE: 152 case VG_sBGRX_8888: 153 case VG_sBGRA_8888: 154 case VG_sBGRA_8888_PRE: 155 case VG_lBGRX_8888: 156 case VG_lBGRA_8888: 157 case VG_lBGRA_8888_PRE: 158 case VG_sXBGR_8888: 159 case VG_sABGR_8888: 160 case VG_sABGR_8888_PRE: 161 case VG_lXBGR_8888: 162 case VG_lABGR_8888: 163 case VG_lABGR_8888_PRE: 164 return 32; 165 166 case VG_sRGB_565: 167 case VG_sRGBA_5551: 168 case VG_sRGBA_4444: 169 case VG_sARGB_1555: 170 case VG_sARGB_4444: 171 case VG_sBGR_565: 172 case VG_sBGRA_5551: 173 case VG_sBGRA_4444: 174 case VG_sABGR_1555: 175 case VG_sABGR_4444: 176 return 16; 177 178 case VG_sL_8: 179 case VG_lL_8: 180 case VG_A_8: 181 return 8; 182 183 case VG_A_4: 184 return 4; 185 186 case VG_BW_1: 187 case VG_A_1: 188 return 1; 189 190 default: // Will only happen when OpenVG extends the enum and we don't. 191 ASSERT(false); 192 return 0; 193 } 194 } 195 196 #ifndef WTF_PLATFORM_BIG_ENDIAN 197 VGImageFormat VGUtils::endianAwareImageFormat(VGImageFormat bigEndianFormat) 198 { 199 switch (bigEndianFormat) { 200 case VG_sRGBX_8888: return VG_sXBGR_8888; 201 case VG_sRGBA_8888: return VG_sABGR_8888; 202 case VG_sRGBA_8888_PRE: return VG_sABGR_8888_PRE; 203 case VG_lRGBX_8888: return VG_lXBGR_8888; 204 case VG_lRGBA_8888: return VG_lABGR_8888; 205 case VG_lRGBA_8888_PRE: return VG_lABGR_8888_PRE; 206 case VG_sXRGB_8888: return VG_sBGRX_8888; 207 case VG_sARGB_8888: return VG_sBGRA_8888; 208 case VG_sARGB_8888_PRE: return VG_sBGRA_8888_PRE; 209 case VG_lXRGB_8888: return VG_lBGRX_8888; 210 case VG_lARGB_8888: return VG_lBGRA_8888; 211 case VG_lARGB_8888_PRE: return VG_lBGRA_8888_PRE; 212 case VG_sBGRX_8888: return VG_sXRGB_8888; 213 case VG_sBGRA_8888: return VG_sARGB_8888; 214 case VG_sBGRA_8888_PRE: return VG_sARGB_8888_PRE; 215 case VG_lBGRX_8888: return VG_lXRGB_8888; 216 case VG_lBGRA_8888: return VG_lARGB_8888; 217 case VG_lBGRA_8888_PRE: return VG_lARGB_8888_PRE; 218 case VG_sXBGR_8888: return VG_sRGBX_8888; 219 case VG_sABGR_8888: return VG_sRGBA_8888; 220 case VG_sABGR_8888_PRE: return VG_sRGBA_8888_PRE; 221 case VG_lXBGR_8888: return VG_lRGBX_8888; 222 case VG_lABGR_8888: return VG_lRGBA_8888; 223 case VG_lABGR_8888_PRE: return VG_lRGBA_8888_PRE; 224 default: ASSERT(false); 225 return (VGImageFormat) 0; 226 } 227 } 228 #else 229 VGImageFormat VGUtils::endianAwareImageFormat(VGImageFormat bigEndianFormat) 230 { 231 return bigEndianFormat; 232 } 233 #endif 234 235 } 236