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 template <typename T> | 320 static bool encodeImage(const ImageDataBuffer& source, const String& mimeType, c
onst double* quality, Vector<char>* output) |
321 static bool encodeImage(T& source, const String& mimeType, const double* quality
, Vector<char>* output) | |
322 { | 321 { |
323 Vector<unsigned char>* encodedImage = reinterpret_cast<Vector<unsigned char>
*>(output); | 322 Vector<unsigned char>* encodedImage = reinterpret_cast<Vector<unsigned char>
*>(output); |
324 | 323 |
325 if (mimeType == "image/jpeg") { | 324 if (mimeType == "image/jpeg") { |
326 int compressionQuality = JPEGImageEncoder::DefaultCompressionQuality; | 325 int compressionQuality = JPEGImageEncoder::DefaultCompressionQuality; |
327 if (quality && *quality >= 0.0 && *quality <= 1.0) | 326 if (quality && *quality >= 0.0 && *quality <= 1.0) |
328 compressionQuality = static_cast<int>(*quality * 100 + 0.5); | 327 compressionQuality = static_cast<int>(*quality * 100 + 0.5); |
329 if (!JPEGImageEncoder::encode(source, compressionQuality, encodedImage)) | 328 if (!JPEGImageEncoder::encode(source, compressionQuality, encodedImage)) |
330 return false; | 329 return false; |
331 } else if (mimeType == "image/webp") { | 330 } else if (mimeType == "image/webp") { |
332 int compressionQuality = WEBPImageEncoder::DefaultCompressionQuality; | 331 int compressionQuality = WEBPImageEncoder::DefaultCompressionQuality; |
333 if (quality && *quality >= 0.0 && *quality <= 1.0) | 332 if (quality && *quality >= 0.0 && *quality <= 1.0) |
334 compressionQuality = static_cast<int>(*quality * 100 + 0.5); | 333 compressionQuality = static_cast<int>(*quality * 100 + 0.5); |
335 if (!WEBPImageEncoder::encode(source, compressionQuality, encodedImage)) | 334 if (!WEBPImageEncoder::encode(source, compressionQuality, encodedImage)) |
336 return false; | 335 return false; |
337 } else { | 336 } else { |
338 if (!PNGImageEncoder::encode(source, encodedImage)) | 337 if (!PNGImageEncoder::encode(source, encodedImage)) |
339 return false; | 338 return false; |
340 ASSERT(mimeType == "image/png"); | 339 ASSERT(mimeType == "image/png"); |
341 } | 340 } |
342 | 341 |
343 return true; | 342 return true; |
344 } | 343 } |
345 | 344 |
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 | |
357 String ImageDataBuffer::toDataURL(const String& mimeType, const double* quality)
const | 345 String ImageDataBuffer::toDataURL(const String& mimeType, const double* quality)
const |
358 { | 346 { |
359 ASSERT(MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(mimeType)); | 347 ASSERT(MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(mimeType)); |
360 | 348 |
361 Vector<char> encodedImage; | 349 Vector<char> encodedImage; |
362 if (!encodeImage(*this, mimeType, quality, &encodedImage)) | 350 if (!encodeImage(*this, mimeType, quality, &encodedImage)) |
363 return "data:,"; | 351 return "data:,"; |
364 | 352 |
365 return "data:" + mimeType + ";base64," + base64Encode(encodedImage); | 353 return "data:" + mimeType + ";base64," + base64Encode(encodedImage); |
366 } | 354 } |
367 | 355 |
368 } // namespace blink | 356 } // namespace blink |
OLD | NEW |