Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 #define _USE_MATH_DEFINES | 5 #define _USE_MATH_DEFINES |
| 6 #include <cmath> | 6 #include <cmath> |
| 7 #include <limits> | 7 #include <limits> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "skia/ext/image_operations.h" | 10 #include "skia/ext/image_operations.h" |
| (...skipping 606 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 617 while (x_pix < 0) | 617 while (x_pix < 0) |
| 618 x_pix += source.width(); | 618 x_pix += source.width(); |
| 619 | 619 |
| 620 dst_row[x] = source_row[x_pix]; | 620 dst_row[x] = source_row[x_pix]; |
| 621 } | 621 } |
| 622 } | 622 } |
| 623 | 623 |
| 624 return cropped; | 624 return cropped; |
| 625 } | 625 } |
| 626 | 626 |
| 627 // static | |
| 628 SkBitmap ImageOperations::DownsampleByTwo(const SkBitmap& bitmap) { | |
| 629 // Handle the nop case. | |
| 630 if (bitmap.width() <= 1 || bitmap.height() <= 1) | |
| 631 return bitmap; | |
| 632 | |
|
cpu_(ooo_6.6-7.5)
2009/06/09 17:32:42
do we care if the source bitmap is not KARG_888 ?
| |
| 633 SkBitmap result; | |
| 634 result.setConfig(SkBitmap::kARGB_8888_Config, | |
| 635 (bitmap.width() + 1) / 2, (bitmap.height() + 1) / 2); | |
|
cpu_(ooo_6.6-7.5)
2009/06/09 17:32:42
vertical aligment
| |
| 636 result.allocPixels(); | |
| 637 | |
| 638 SkAutoLockPixels lock(bitmap); | |
| 639 for (int dest_y = 0; dest_y < result.height(); dest_y++) { | |
| 640 for (int dest_x = 0; dest_x < result.width(); dest_x++ ) { | |
| 641 // This code is based on downsampleby2_proc32 in SkBitmap.cpp. It is very | |
| 642 // clever in that it does two channels at once: alpha and green ("ag") | |
| 643 // and red and blue ("rb"). Each channel gets averaged across 4 pixels | |
| 644 // to get the result. | |
| 645 int src_x = dest_x << 1; | |
| 646 int src_y = dest_y << 1; | |
| 647 const SkPMColor* cur_src = bitmap.getAddr32(src_x, src_y); | |
| 648 SkPMColor tmp, ag, rb; | |
| 649 | |
| 650 // Top left pixel of the 2x2 block. | |
| 651 tmp = *cur_src; | |
| 652 ag = (tmp >> 8) & 0xFF00FF; | |
| 653 rb = tmp & 0xFF00FF; | |
| 654 if (src_x < bitmap.width() - 1) | |
| 655 cur_src += 1; | |
| 656 | |
| 657 // Top right pixel of the 2x2 block. | |
| 658 tmp = *cur_src; | |
| 659 ag += (tmp >> 8) & 0xFF00FF; | |
| 660 rb += tmp & 0xFF00FF; | |
| 661 if (src_y < bitmap.height() - 1) | |
| 662 cur_src = bitmap.getAddr32(src_x, src_y + 1); | |
| 663 else | |
| 664 cur_src = bitmap.getAddr32(src_x, src_y); // Move back to the first. | |
| 665 | |
| 666 // Bottom left pixel of the 2x2 block. | |
| 667 tmp = *cur_src; | |
| 668 ag += (tmp >> 8) & 0xFF00FF; | |
| 669 rb += tmp & 0xFF00FF; | |
| 670 if (src_x < bitmap.width() - 1) | |
| 671 cur_src += 1; | |
| 672 | |
| 673 // Bottom right pixel of the 2x2 block. | |
| 674 tmp = *cur_src; | |
| 675 ag += (tmp >> 8) & 0xFF00FF; | |
| 676 rb += tmp & 0xFF00FF; | |
| 677 | |
|
cpu_(ooo_6.6-7.5)
2009/06/09 17:32:42
note that there must be some implicit division by
| |
| 678 *result.getAddr32(dest_x, dest_y) = | |
| 679 ((rb >> 2) & 0xFF00FF) | ((ag << 6) & 0xFF00FF00); | |
| 680 } | |
| 681 } | |
| 682 | |
| 683 return result; | |
| 684 } | |
| 685 | |
| 686 // static | |
| 687 SkBitmap ImageOperations::DownsampleByTwoUntilSize(const SkBitmap& bitmap, | |
| 688 int min_w, int min_h) { | |
| 689 if (bitmap.width() <= min_w || bitmap.height() <= min_h || | |
| 690 min_w < 0 || min_h < 0) | |
| 691 return bitmap; | |
| 692 | |
| 693 // Since bitmaps are refcounted, this copy will be fast. | |
| 694 SkBitmap current = bitmap; | |
| 695 while (current.width() >= min_w * 2 && current.height() >= min_h * 2 && | |
| 696 current.width() > 1 && current.height() > 1) | |
| 697 current = DownsampleByTwo(current); | |
| 698 return current; | |
| 699 } | |
| 700 | |
| 627 } // namespace skia | 701 } // namespace skia |
| 628 | 702 |
| OLD | NEW |