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

Side by Side Diff: ui/base/clipboard/clipboard_win.cc

Issue 7967007: Add heuristic for fixing the alpha channel when reading clipboard images on Windows (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 9 years, 3 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Many of these functions are based on those found in 5 // Many of these functions are based on those found in
6 // webkit/port/platform/PasteboardWin.cpp 6 // webkit/port/platform/PasteboardWin.cpp
7 7
8 #include "ui/base/clipboard/clipboard.h" 8 #include "ui/base/clipboard/clipboard.h"
9 9
10 #include <shlobj.h> 10 #include <shlobj.h>
(...skipping 412 matching lines...) Expand 10 before | Expand all | Expand 10 after
423 std::string html_fragment(static_cast<const char*>(::GlobalLock(data))); 423 std::string html_fragment(static_cast<const char*>(::GlobalLock(data)));
424 ::GlobalUnlock(data); 424 ::GlobalUnlock(data);
425 425
426 std::string markup_utf8; 426 std::string markup_utf8;
427 ClipboardUtil::CFHtmlToHtml(html_fragment, markup ? &markup_utf8 : NULL, 427 ClipboardUtil::CFHtmlToHtml(html_fragment, markup ? &markup_utf8 : NULL,
428 src_url); 428 src_url);
429 if (markup) 429 if (markup)
430 markup->assign(UTF8ToWide(markup_utf8)); 430 markup->assign(UTF8ToWide(markup_utf8));
431 } 431 }
432 432
433 static bool BitmapHasInvalidPremultipliedColors(const SkBitmap& bitmap) {
tony 2011/09/22 01:26:58 Nit: Chromium style normally puts static functions
434 for (int x = 0; x < bitmap.width(); ++x) {
435 for (int y = 0; y < bitmap.height(); ++y) {
436 uint32_t pixel = *bitmap.getAddr32(x, y);
437 if (SkColorGetR(pixel) > SkColorGetA(pixel) ||
438 SkColorGetG(pixel) > SkColorGetA(pixel) ||
439 SkColorGetB(pixel) > SkColorGetA(pixel))
440 return true;
441 }
442 }
443 return false;
444 }
445
446 static void MakeBitmapOpaque(const SkBitmap& bitmap) {
447 for (int x = 0; x < bitmap.width(); ++x) {
448 for (int y = 0; y < bitmap.height(); ++y) {
449 *bitmap.getAddr32(x, y) = SkColorSetA(*bitmap.getAddr32(x, y), 0xFF);
450 }
451 }
452 }
453
433 SkBitmap Clipboard::ReadImage(Buffer buffer) const { 454 SkBitmap Clipboard::ReadImage(Buffer buffer) const {
434 DCHECK_EQ(buffer, BUFFER_STANDARD); 455 DCHECK_EQ(buffer, BUFFER_STANDARD);
435 456
436 // Acquire the clipboard. 457 // Acquire the clipboard.
437 ScopedClipboard clipboard; 458 ScopedClipboard clipboard;
438 if (!clipboard.Acquire(GetClipboardWindow())) 459 if (!clipboard.Acquire(GetClipboardWindow()))
439 return SkBitmap(); 460 return SkBitmap();
440 461
441 // We use a DIB rather than a DDB here since ::GetObject() with the 462 // We use a DIB rather than a DDB here since ::GetObject() with the
442 // HBITMAP returned from ::GetClipboardData(CF_BITMAP) always reports a color 463 // HBITMAP returned from ::GetClipboardData(CF_BITMAP) always reports a color
(...skipping 26 matching lines...) Expand all
469 gfx::CanvasSkia canvas(bitmap->bmiHeader.biWidth, bitmap->bmiHeader.biHeight, 490 gfx::CanvasSkia canvas(bitmap->bmiHeader.biWidth, bitmap->bmiHeader.biHeight,
470 false); 491 false);
471 { 492 {
472 skia::ScopedPlatformPaint scoped_platform_paint(&canvas); 493 skia::ScopedPlatformPaint scoped_platform_paint(&canvas);
473 HDC dc = scoped_platform_paint.GetPlatformSurface(); 494 HDC dc = scoped_platform_paint.GetPlatformSurface();
474 ::SetDIBitsToDevice(dc, 0, 0, bitmap->bmiHeader.biWidth, 495 ::SetDIBitsToDevice(dc, 0, 0, bitmap->bmiHeader.biWidth,
475 bitmap->bmiHeader.biHeight, 0, 0, 0, 496 bitmap->bmiHeader.biHeight, 0, 0, 0,
476 bitmap->bmiHeader.biHeight, bitmap_bits, bitmap, 497 bitmap->bmiHeader.biHeight, bitmap_bits, bitmap,
477 DIB_RGB_COLORS); 498 DIB_RGB_COLORS);
478 } 499 }
479 // SetDIBitsToDevice doesn't properly set alpha values for bitmaps with 500 // Windows doesn't really handle alpha channels well in many situations. When
Wez 2011/09/22 10:55:41 nit: Technically it's applications which handle DI
480 // depth < 32bpp so manually fix it up. 501 // the source image is < 32 bpp, we force the bitmap to be opaque. When the
481 if (bitmap->bmiHeader.biBitCount < 32) { 502 // source image is 32 bpp, the alpha channel might still contain garbage data.
Wez 2011/09/22 10:55:41 Isn't it rather that some applications put images
482 const SkBitmap& device_bitmap = canvas.getDevice()->accessBitmap(true); 503 // Since Windows uses premultiplied alpha, we scan for instances where
504 // (R, G, B) > A. If there are any invalid premultiplied colors in the image,
505 // we assume the alpha channel contains garbage and force the bitmap to be
506 // opaque as well. Note that this heuristic will fail on a transparent bitmap
507 // containing only black pixels...
508 const SkBitmap& device_bitmap = canvas.getDevice()->accessBitmap(true);
509 {
483 SkAutoLockPixels lock(device_bitmap); 510 SkAutoLockPixels lock(device_bitmap);
484 for (int x = 0; x < device_bitmap.width(); ++x) { 511 bool has_invalid_alpha_channel = bitmap->bmiHeader.biBitCount < 32 ||
485 for (int y = 0; y < device_bitmap.height(); ++y) { 512 BitmapHasInvalidPremultipliedColors(device_bitmap);
486 *device_bitmap.getAddr32(x, y) = 513 if (has_invalid_alpha_channel) {
487 SkColorSetA(*device_bitmap.getAddr32(x, y), 0xFF); 514 MakeBitmapOpaque(device_bitmap);
488 }
489 } 515 }
490 } 516 }
517
491 return canvas.ExtractBitmap(); 518 return canvas.ExtractBitmap();
492 } 519 }
493 520
494 void Clipboard::ReadBookmark(string16* title, std::string* url) const { 521 void Clipboard::ReadBookmark(string16* title, std::string* url) const {
495 if (title) 522 if (title)
496 title->clear(); 523 title->clear();
497 524
498 if (url) 525 if (url)
499 url->clear(); 526 url->clear();
500 527
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
688 clipboard_owner_ = ::CreateWindow(L"ClipboardOwnerWindowClass", 715 clipboard_owner_ = ::CreateWindow(L"ClipboardOwnerWindowClass",
689 L"ClipboardOwnerWindow", 716 L"ClipboardOwnerWindow",
690 0, 0, 0, 0, 0, 717 0, 0, 0, 0, 0,
691 HWND_MESSAGE, 718 HWND_MESSAGE,
692 0, 0, 0); 719 0, 0, 0);
693 } 720 }
694 return clipboard_owner_; 721 return clipboard_owner_;
695 } 722 }
696 723
697 } // namespace ui 724 } // namespace ui
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698