Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(77)

Side by Side Diff: third_party/WebKit/Source/platform/graphics/ImageBuffer.cpp

Issue 1355333005: Implement Asynchronous image encoding for Canvas.toBlob (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Change async func to static so no need worry if canvas instance does not live long enough Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 328 matching lines...) Expand 10 before | Expand all | Expand 10 after
339 ASSERT(originY >= 0); 339 ASSERT(originY >= 0);
340 ASSERT(originY < sourceRect.maxY()); 340 ASSERT(originY < sourceRect.maxY());
341 341
342 const size_t srcBytesPerRow = 4 * sourceSize.width(); 342 const size_t srcBytesPerRow = 4 * sourceSize.width();
343 const void* srcAddr = source + originY * srcBytesPerRow + originX * 4; 343 const void* srcAddr = source + originY * srcBytesPerRow + originX * 4;
344 SkAlphaType alphaType = (multiplied == Premultiplied) ? kPremul_SkAlphaType : kUnpremul_SkAlphaType; 344 SkAlphaType alphaType = (multiplied == Premultiplied) ? kPremul_SkAlphaType : kUnpremul_SkAlphaType;
345 SkImageInfo info = SkImageInfo::Make(sourceRect.width(), sourceRect.height() , kRGBA_8888_SkColorType, alphaType); 345 SkImageInfo info = SkImageInfo::Make(sourceRect.width(), sourceRect.height() , kRGBA_8888_SkColorType, alphaType);
346 m_surface->writePixels(info, srcAddr, srcBytesPerRow, destX, destY); 346 m_surface->writePixels(info, srcAddr, srcBytesPerRow, destX, destY);
347 } 347 }
348 348
349 bool ImageDataBuffer::encodeImage(const String& mimeType, const double* quality, Vector<char>* output) const 349 bool ImageDataBuffer::encodeImage(const String& mimeType, const double& quality, Vector<char>* output) const
350 { 350 {
351 Vector<unsigned char>* encodedImage = reinterpret_cast<Vector<unsigned char> *>(output); 351 Vector<unsigned char>* encodedImage = reinterpret_cast<Vector<unsigned char> *>(output);
352 352
353 if (mimeType == "image/jpeg") { 353 if (mimeType == "image/jpeg") {
354 int compressionQuality = JPEGImageEncoder::DefaultCompressionQuality; 354 int compressionQuality = JPEGImageEncoder::DefaultCompressionQuality;
355 if (quality && *quality >= 0.0 && *quality <= 1.0) 355 if (quality >= 0.0 && quality <= 1.0)
356 compressionQuality = static_cast<int>(*quality * 100 + 0.5); 356 compressionQuality = static_cast<int>(quality * 100 + 0.5);
357 if (!JPEGImageEncoder::encode(*this, compressionQuality, encodedImage)) 357 if (!JPEGImageEncoder::encode(*this, compressionQuality, encodedImage))
358 return false; 358 return false;
359 } else if (mimeType == "image/webp") { 359 } else if (mimeType == "image/webp") {
360 int compressionQuality = WEBPImageEncoder::DefaultCompressionQuality; 360 int compressionQuality = WEBPImageEncoder::DefaultCompressionQuality;
361 if (quality && *quality >= 0.0 && *quality <= 1.0) 361 if (quality >= 0.0 && quality <= 1.0)
362 compressionQuality = static_cast<int>(*quality * 100 + 0.5); 362 compressionQuality = static_cast<int>(quality * 100 + 0.5);
363 if (!WEBPImageEncoder::encode(*this, compressionQuality, encodedImage)) 363 if (!WEBPImageEncoder::encode(*this, compressionQuality, encodedImage))
364 return false; 364 return false;
365 } else { 365 } else {
366 if (!PNGImageEncoder::encode(*this, encodedImage)) 366 if (!PNGImageEncoder::encode(*this, encodedImage))
367 return false; 367 return false;
368 ASSERT(mimeType == "image/png"); 368 ASSERT(mimeType == "image/png");
369 } 369 }
370 370
371 return true; 371 return true;
372 } 372 }
373 373
374 String ImageDataBuffer::toDataURL(const String& mimeType, const double* quality) const 374 String ImageDataBuffer::toDataURL(const String& mimeType, const double& quality) const
375 { 375 {
376 ASSERT(MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(mimeType)); 376 ASSERT(MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(mimeType));
377 377
378 Vector<char> encodedImage; 378 Vector<char> encodedImage;
379 if (!encodeImage(mimeType, quality, &encodedImage)) 379 if (!encodeImage(mimeType, quality, &encodedImage))
380 return "data:,"; 380 return "data:,";
381 381
382 return "data:" + mimeType + ";base64," + base64Encode(encodedImage); 382 return "data:" + mimeType + ";base64," + base64Encode(encodedImage);
383 } 383 }
384 384
385 } // namespace blink 385 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698