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 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 ASSERT(originY >= 0); | 334 ASSERT(originY >= 0); |
335 ASSERT(originY < sourceRect.maxY()); | 335 ASSERT(originY < sourceRect.maxY()); |
336 | 336 |
337 const size_t srcBytesPerRow = 4 * sourceSize.width(); | 337 const size_t srcBytesPerRow = 4 * sourceSize.width(); |
338 const void* srcAddr = source + originY * srcBytesPerRow + originX * 4; | 338 const void* srcAddr = source + originY * srcBytesPerRow + originX * 4; |
339 SkAlphaType alphaType = (multiplied == Premultiplied) ? kPremul_SkAlphaType
: kUnpremul_SkAlphaType; | 339 SkAlphaType alphaType = (multiplied == Premultiplied) ? kPremul_SkAlphaType
: kUnpremul_SkAlphaType; |
340 SkImageInfo info = SkImageInfo::Make(sourceRect.width(), sourceRect.height()
, kRGBA_8888_SkColorType, alphaType); | 340 SkImageInfo info = SkImageInfo::Make(sourceRect.width(), sourceRect.height()
, kRGBA_8888_SkColorType, alphaType); |
341 m_surface->writePixels(info, srcAddr, srcBytesPerRow, destX, destY); | 341 m_surface->writePixels(info, srcAddr, srcBytesPerRow, destX, destY); |
342 } | 342 } |
343 | 343 |
344 bool ImageDataBuffer::encodeImage(const String& mimeType, const double* quality,
Vector<char>* output) const | 344 bool ImageDataBuffer::encodeImage(const String& mimeType, const double* quality,
Vector<char>* output, String* outputMimeType) const |
345 { | 345 { |
| 346 ASSERT(MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(mimeType)); |
| 347 |
346 Vector<unsigned char>* encodedImage = reinterpret_cast<Vector<unsigned char>
*>(output); | 348 Vector<unsigned char>* encodedImage = reinterpret_cast<Vector<unsigned char>
*>(output); |
347 | 349 |
348 if (mimeType == "image/jpeg") { | 350 if (mimeType == "image/jpeg") { |
349 int compressionQuality = JPEGImageEncoder::DefaultCompressionQuality; | 351 int compressionQuality = JPEGImageEncoder::DefaultCompressionQuality; |
350 if (quality && *quality >= 0.0 && *quality <= 1.0) | 352 if (quality && *quality >= 0.0 && *quality <= 1.0) |
351 compressionQuality = static_cast<int>(*quality * 100 + 0.5); | 353 compressionQuality = static_cast<int>(*quality * 100 + 0.5); |
352 if (!JPEGImageEncoder::encode(*this, compressionQuality, encodedImage)) | 354 if (!JPEGImageEncoder::encode(*this, compressionQuality, encodedImage)) |
353 return false; | 355 return false; |
| 356 } else if (mimeType == "image/webp.ll") { |
| 357 int compressionQuality = WEBPImageEncoder::DefaultCompressionQuality; |
| 358 if (quality && *quality >= 0.0 && *quality <= 1.0) |
| 359 compressionQuality = static_cast<int>(*quality * 100 + 0.5); |
| 360 if (!WEBPImageEncoder::encode(*this, compressionQuality, encodedImage, W
EBPImageEncoder::Lossless)) |
| 361 return false; |
| 362 // webp.ll is an encoder selector: map it to the encoded image mimeType
"image/webp". |
| 363 *outputMimeType = "image/webp"; |
| 364 return true; |
354 } else if (mimeType == "image/webp") { | 365 } else if (mimeType == "image/webp") { |
355 int compressionQuality = WEBPImageEncoder::DefaultCompressionQuality; | 366 int compressionQuality = WEBPImageEncoder::DefaultCompressionQuality; |
356 if (quality && *quality >= 0.0 && *quality <= 1.0) | 367 if (quality && *quality >= 0.0 && *quality <= 1.0) |
357 compressionQuality = static_cast<int>(*quality * 100 + 0.5); | 368 compressionQuality = static_cast<int>(*quality * 100 + 0.5); |
358 if (!WEBPImageEncoder::encode(*this, compressionQuality, encodedImage)) | 369 if (!WEBPImageEncoder::encode(*this, compressionQuality, encodedImage)) |
359 return false; | 370 return false; |
360 } else { | 371 } else { |
361 if (!PNGImageEncoder::encode(*this, encodedImage)) | 372 if (!PNGImageEncoder::encode(*this, encodedImage)) |
362 return false; | 373 return false; |
363 ASSERT(mimeType == "image/png"); | 374 ASSERT(mimeType == "image/png"); |
364 } | 375 } |
365 | 376 |
| 377 *outputMimeType = mimeType; |
366 return true; | 378 return true; |
367 } | 379 } |
368 | 380 |
369 String ImageDataBuffer::toDataURL(const String& mimeType, const double* quality)
const | 381 String ImageDataBuffer::toDataURL(const String& mimeType, const double* quality)
const |
370 { | 382 { |
371 ASSERT(MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(mimeType)); | |
372 | |
373 Vector<char> encodedImage; | 383 Vector<char> encodedImage; |
374 if (!encodeImage(mimeType, quality, &encodedImage)) | 384 String encodedMimeType; |
| 385 if (!encodeImage(mimeType, quality, &encodedImage, &encodedMimeType)) |
375 return "data:,"; | 386 return "data:,"; |
376 | 387 |
377 return "data:" + mimeType + ";base64," + base64Encode(encodedImage); | 388 return "data:" + encodedMimeType + ";base64," + base64Encode(encodedImage); |
378 } | 389 } |
379 | 390 |
380 } // namespace blink | 391 } // namespace blink |
OLD | NEW |