OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2008, Google Inc. All rights reserved. | 2 * Copyright (c) 2008, Google Inc. All rights reserved. |
3 * Copyright (C) 2009 Dirk Schulze <krit@webkit.org> | 3 * Copyright (C) 2009 Dirk Schulze <krit@webkit.org> |
4 * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved. | 4 * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved. |
5 * | 5 * |
6 * Redistribution and use in source and binary forms, with or without | 6 * Redistribution and use in source and binary forms, with or without |
7 * modification, are permitted provided that the following conditions are | 7 * modification, are permitted provided that the following conditions are |
8 * met: | 8 * met: |
9 * | 9 * |
10 * * Redistributions of source code must retain the above copyright | 10 * * Redistributions of source code must retain the above copyright |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 RefPtr<ByteArray> result = ByteArray::create(rect.width() * rect.height() *
4); | 219 RefPtr<ByteArray> result = ByteArray::create(rect.width() * rect.height() *
4); |
220 | 220 |
221 unsigned char* data = result->data(); | 221 unsigned char* data = result->data(); |
222 | 222 |
223 if (rect.x() < 0 | 223 if (rect.x() < 0 |
224 || rect.y() < 0 | 224 || rect.y() < 0 |
225 || rect.maxX() > size.width() | 225 || rect.maxX() > size.width() |
226 || rect.maxY() > size.height()) | 226 || rect.maxY() > size.height()) |
227 memset(data, 0, result->length()); | 227 memset(data, 0, result->length()); |
228 | 228 |
229 int originX = rect.x(); | 229 unsigned destBytesPerRow = 4 * rect.width(); |
230 int destX = 0; | 230 SkBitmap destBitmap; |
231 if (originX < 0) { | 231 destBitmap.setConfig(SkBitmap::kARGB_8888_Config, rect.width(), rect.height(
), destBytesPerRow); |
232 destX = -originX; | 232 destBitmap.setPixels(data); |
233 originX = 0; | |
234 } | |
235 int endX = rect.maxX(); | |
236 if (endX > size.width()) | |
237 endX = size.width(); | |
238 int numColumns = endX - originX; | |
239 | 233 |
240 if (numColumns <= 0) | 234 SkCanvas::Config8888 config8888; |
241 return result.release(); | 235 if (multiplied == Premultiplied) |
| 236 config8888 = SkCanvas::kRGBA_Premul_Config8888; |
| 237 else |
| 238 config8888 = SkCanvas::kRGBA_Unpremul_Config8888; |
242 | 239 |
243 int originY = rect.y(); | 240 canvas->readPixels(&destBitmap, rect.x(), rect.y(), config8888); |
244 int destY = 0; | |
245 if (originY < 0) { | |
246 destY = -originY; | |
247 originY = 0; | |
248 } | |
249 int endY = rect.maxY(); | |
250 if (endY > size.height()) | |
251 endY = size.height(); | |
252 int numRows = endY - originY; | |
253 | |
254 if (numRows <= 0) | |
255 return result.release(); | |
256 | |
257 SkBitmap srcBitmap; | |
258 if (!canvas->readPixels(SkIRect::MakeXYWH(originX, originY, numColumns, numR
ows), &srcBitmap)) | |
259 return result.release(); | |
260 | |
261 unsigned destBytesPerRow = 4 * rect.width(); | |
262 unsigned char* destRow = data + destY * destBytesPerRow + destX * 4; | |
263 | |
264 // Do conversion of byte order and alpha divide (if necessary) | |
265 for (int y = 0; y < numRows; ++y) { | |
266 SkPMColor* srcBitmapRow = srcBitmap.getAddr32(0, y); | |
267 for (int x = 0; x < numColumns; ++x) { | |
268 SkPMColor srcPMColor = srcBitmapRow[x]; | |
269 unsigned char* destPixel = &destRow[x * 4]; | |
270 if (multiplied == Unmultiplied) { | |
271 unsigned char a = SkGetPackedA32(srcPMColor); | |
272 destPixel[0] = a ? SkGetPackedR32(srcPMColor) * 255 / a : 0; | |
273 destPixel[1] = a ? SkGetPackedG32(srcPMColor) * 255 / a : 0; | |
274 destPixel[2] = a ? SkGetPackedB32(srcPMColor) * 255 / a : 0; | |
275 destPixel[3] = a; | |
276 } else { | |
277 // Input and output are both pre-multiplied, we just need to re-
arrange the | |
278 // bytes from the bitmap format to RGBA. | |
279 destPixel[0] = SkGetPackedR32(srcPMColor); | |
280 destPixel[1] = SkGetPackedG32(srcPMColor); | |
281 destPixel[2] = SkGetPackedB32(srcPMColor); | |
282 destPixel[3] = SkGetPackedA32(srcPMColor); | |
283 } | |
284 } | |
285 destRow += destBytesPerRow; | |
286 } | |
287 | |
288 return result.release(); | 241 return result.release(); |
289 } | 242 } |
290 | 243 |
291 PassRefPtr<ByteArray> ImageBuffer::getUnmultipliedImageData(const IntRect& rect)
const | 244 PassRefPtr<ByteArray> ImageBuffer::getUnmultipliedImageData(const IntRect& rect)
const |
292 { | 245 { |
293 return getImageData<Unmultiplied>(rect, context()->platformContext()->canvas
(), m_size); | 246 return getImageData<Unmultiplied>(rect, context()->platformContext()->canvas
(), m_size); |
294 } | 247 } |
295 | 248 |
296 PassRefPtr<ByteArray> ImageBuffer::getPremultipliedImageData(const IntRect& rect
) const | 249 PassRefPtr<ByteArray> ImageBuffer::getPremultipliedImageData(const IntRect& rect
) const |
297 { | 250 { |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
399 | 352 |
400 Vector<char> encodedImage, base64Data; | 353 Vector<char> encodedImage, base64Data; |
401 if (!encodeImage(imageData, mimeType, quality, &encodedImage)) | 354 if (!encodeImage(imageData, mimeType, quality, &encodedImage)) |
402 return "data:,"; | 355 return "data:,"; |
403 | 356 |
404 base64Encode(encodedImage, base64Data); | 357 base64Encode(encodedImage, base64Data); |
405 return "data:" + mimeType + ";base64," + base64Data; | 358 return "data:" + mimeType + ";base64," + base64Data; |
406 } | 359 } |
407 | 360 |
408 } // namespace WebCore | 361 } // namespace WebCore |
OLD | NEW |