| 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 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 310 const size_t srcBytesPerRow = 4 * sourceSize.width(); | 310 const size_t srcBytesPerRow = 4 * sourceSize.width(); |
| 311 const void* srcAddr = source + originY * srcBytesPerRow + originX * 4; | 311 const void* srcAddr = source + originY * srcBytesPerRow + originX * 4; |
| 312 SkAlphaType alphaType = (multiplied == Premultiplied) ? kPremul_SkAlphaType
: kUnpremul_SkAlphaType; | 312 SkAlphaType alphaType = (multiplied == Premultiplied) ? kPremul_SkAlphaType
: kUnpremul_SkAlphaType; |
| 313 SkImageInfo info = SkImageInfo::Make(sourceRect.width(), sourceRect.height()
, kRGBA_8888_SkColorType, alphaType); | 313 SkImageInfo info = SkImageInfo::Make(sourceRect.width(), sourceRect.height()
, kRGBA_8888_SkColorType, alphaType); |
| 314 | 314 |
| 315 m_surface->willAccessPixels(); | 315 m_surface->willAccessPixels(); |
| 316 | 316 |
| 317 canvas()->writePixels(info, srcAddr, srcBytesPerRow, destX, destY); | 317 canvas()->writePixels(info, srcAddr, srcBytesPerRow, destX, destY); |
| 318 } | 318 } |
| 319 | 319 |
| 320 static bool encodeImage(const ImageDataBuffer& source, const String& mimeType, c
onst double* quality, Vector<char>* output) | 320 template <typename T> |
| 321 static bool encodeImage(T& source, const String& mimeType, const double* quality
, Vector<char>* output) |
| 321 { | 322 { |
| 322 Vector<unsigned char>* encodedImage = reinterpret_cast<Vector<unsigned char>
*>(output); | 323 Vector<unsigned char>* encodedImage = reinterpret_cast<Vector<unsigned char>
*>(output); |
| 323 | 324 |
| 324 if (mimeType == "image/jpeg") { | 325 if (mimeType == "image/jpeg") { |
| 325 int compressionQuality = JPEGImageEncoder::DefaultCompressionQuality; | 326 int compressionQuality = JPEGImageEncoder::DefaultCompressionQuality; |
| 326 if (quality && *quality >= 0.0 && *quality <= 1.0) | 327 if (quality && *quality >= 0.0 && *quality <= 1.0) |
| 327 compressionQuality = static_cast<int>(*quality * 100 + 0.5); | 328 compressionQuality = static_cast<int>(*quality * 100 + 0.5); |
| 328 if (!JPEGImageEncoder::encode(source, compressionQuality, encodedImage)) | 329 if (!JPEGImageEncoder::encode(source, compressionQuality, encodedImage)) |
| 329 return false; | 330 return false; |
| 330 } else if (mimeType == "image/webp") { | 331 } else if (mimeType == "image/webp") { |
| 331 int compressionQuality = WEBPImageEncoder::DefaultCompressionQuality; | 332 int compressionQuality = WEBPImageEncoder::DefaultCompressionQuality; |
| 332 if (quality && *quality >= 0.0 && *quality <= 1.0) | 333 if (quality && *quality >= 0.0 && *quality <= 1.0) |
| 333 compressionQuality = static_cast<int>(*quality * 100 + 0.5); | 334 compressionQuality = static_cast<int>(*quality * 100 + 0.5); |
| 334 if (!WEBPImageEncoder::encode(source, compressionQuality, encodedImage)) | 335 if (!WEBPImageEncoder::encode(source, compressionQuality, encodedImage)) |
| 335 return false; | 336 return false; |
| 336 } else { | 337 } else { |
| 337 if (!PNGImageEncoder::encode(source, encodedImage)) | 338 if (!PNGImageEncoder::encode(source, encodedImage)) |
| 338 return false; | 339 return false; |
| 339 ASSERT(mimeType == "image/png"); | 340 ASSERT(mimeType == "image/png"); |
| 340 } | 341 } |
| 341 | 342 |
| 342 return true; | 343 return true; |
| 343 } | 344 } |
| 344 | 345 |
| 346 String ImageBuffer::toDataURL(const String& mimeType, const double* quality) con
st |
| 347 { |
| 348 ASSERT(MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(mimeType)); |
| 349 |
| 350 Vector<char> encodedImage; |
| 351 if (!isSurfaceValid() || !encodeImage(m_surface->bitmap(), mimeType, quality
, &encodedImage)) |
| 352 return "data:,"; |
| 353 |
| 354 return "data:" + mimeType + ";base64," + base64Encode(encodedImage); |
| 355 } |
| 356 |
| 345 String ImageDataBuffer::toDataURL(const String& mimeType, const double* quality)
const | 357 String ImageDataBuffer::toDataURL(const String& mimeType, const double* quality)
const |
| 346 { | 358 { |
| 347 ASSERT(MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(mimeType)); | 359 ASSERT(MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(mimeType)); |
| 348 | 360 |
| 349 Vector<char> encodedImage; | 361 Vector<char> encodedImage; |
| 350 if (!encodeImage(*this, mimeType, quality, &encodedImage)) | 362 if (!encodeImage(*this, mimeType, quality, &encodedImage)) |
| 351 return "data:,"; | 363 return "data:,"; |
| 352 | 364 |
| 353 return "data:" + mimeType + ";base64," + base64Encode(encodedImage); | 365 return "data:" + mimeType + ";base64," + base64Encode(encodedImage); |
| 354 } | 366 } |
| 355 | 367 |
| 356 } // namespace blink | 368 } // namespace blink |
| OLD | NEW |