SkImage::NewFromBitmap works with Index8
authorhalcanary <halcanary@google.com>
Mon, 28 Sep 2015 18:55:28 +0000 (11:55 -0700)
committerCommit bot <commit-bot@chromium.org>
Mon, 28 Sep 2015 18:55:28 +0000 (11:55 -0700)
BUG=skia:4390

Review URL: https://codereview.chromium.org/1377473002

src/image/SkImage_Raster.cpp
tests/ImageTest.cpp

index 30ea7cc1a45a7937d979d0e6170d2febfa5e33e4..512e788a39008b4450279a5024f9808ebeff7a6c 100644 (file)
@@ -22,7 +22,7 @@
 
 class SkImage_Raster : public SkImage_Base {
 public:
-    static bool ValidArgs(const Info& info, size_t rowBytes, SkColorTable* ctable,
+    static bool ValidArgs(const Info& info, size_t rowBytes, bool hasColorTable,
                           size_t* minSize) {
         const int maxDimension = SK_MaxS32 >> 2;
 
@@ -44,8 +44,7 @@ public:
         }
 
         const bool needsCT = kIndex_8_SkColorType == info.colorType();
-        const bool hasCT = nullptr != ctable;
-        if (needsCT != hasCT) {
+        if (needsCT != hasColorTable) {
             return false;
         }
 
@@ -227,7 +226,7 @@ SkImage* SkImage_Raster::onNewSubset(const SkIRect& subset) const {
 SkImage* SkImage::NewRasterCopy(const SkImageInfo& info, const void* pixels, size_t rowBytes,
                                 SkColorTable* ctable) {
     size_t size;
-    if (!SkImage_Raster::ValidArgs(info, rowBytes, ctable, &size) || !pixels) {
+    if (!SkImage_Raster::ValidArgs(info, rowBytes, ctable != nullptr, &size) || !pixels) {
         return nullptr;
     }
 
@@ -239,7 +238,7 @@ SkImage* SkImage::NewRasterCopy(const SkImageInfo& info, const void* pixels, siz
 
 SkImage* SkImage::NewRasterData(const SkImageInfo& info, SkData* data, size_t rowBytes) {
     size_t size;
-    if (!SkImage_Raster::ValidArgs(info, rowBytes, nullptr, &size) || !data) {
+    if (!SkImage_Raster::ValidArgs(info, rowBytes, false, &size) || !data) {
         return nullptr;
     }
 
@@ -255,7 +254,7 @@ SkImage* SkImage::NewRasterData(const SkImageInfo& info, SkData* data, size_t ro
 SkImage* SkImage::NewFromRaster(const SkImageInfo& info, const void* pixels, size_t rowBytes,
                                 RasterReleaseProc proc, ReleaseContext ctx) {
     size_t size;
-    if (!SkImage_Raster::ValidArgs(info, rowBytes, nullptr, &size) || !pixels) {
+    if (!SkImage_Raster::ValidArgs(info, rowBytes, false, &size) || !pixels) {
         return nullptr;
     }
 
@@ -267,7 +266,7 @@ SkImage* SkImage::NewFromRaster(const SkImageInfo& info, const void* pixels, siz
 SkImage* SkNewImageFromPixelRef(const SkImageInfo& info, SkPixelRef* pr,
                                 const SkIPoint& pixelRefOrigin, size_t rowBytes,
                                 const SkSurfaceProps* props) {
-    if (!SkImage_Raster::ValidArgs(info, rowBytes, nullptr, nullptr)) {
+    if (!SkImage_Raster::ValidArgs(info, rowBytes, false, nullptr)) {
         return nullptr;
     }
     return new SkImage_Raster(info, pr, pixelRefOrigin, rowBytes, props);
@@ -277,7 +276,13 @@ SkImage* SkNewImageFromRasterBitmap(const SkBitmap& bm, const SkSurfaceProps* pr
                                     ForceCopyMode forceCopy) {
     SkASSERT(nullptr == bm.getTexture());
 
-    if (!SkImage_Raster::ValidArgs(bm.info(), bm.rowBytes(), nullptr, nullptr)) {
+    bool hasColorTable = false;
+    if (kIndex_8_SkColorType == bm.colorType()) {
+        SkAutoLockPixels autoLockPixels(bm);
+        hasColorTable = bm.getColorTable() != nullptr;
+    }
+
+    if (!SkImage_Raster::ValidArgs(bm.info(), bm.rowBytes(), hasColorTable, nullptr)) {
         return nullptr;
     }
 
index 0b27af97807664a3a3beaa118c8ab9958af8ee6c..7cdf5b4c05d0f1ffdf0702a2388c51830e9b4f22 100644 (file)
@@ -5,6 +5,7 @@
  * found in the LICENSE file.
  */
 
+#include "SkBitmap.h"
 #include "SkCanvas.h"
 #include "SkData.h"
 #include "SkDevice.h"
@@ -345,3 +346,18 @@ DEF_GPUTEST(SkImage_Gpu2Cpu, reporter, factory) {
     }
 }
 #endif
+
+// http://skbug.com/4390
+DEF_TEST(ImageFromIndex8Bitmap, r) {
+    SkPMColor pmColors[1] = {SkPreMultiplyColor(SK_ColorWHITE)};
+    SkBitmap bm;
+    SkAutoTUnref<SkColorTable> ctable(
+            new SkColorTable(pmColors, SK_ARRAY_COUNT(pmColors)));
+    SkImageInfo info =
+            SkImageInfo::Make(1, 1, kIndex_8_SkColorType, kPremul_SkAlphaType);
+    bm.allocPixels(info, nullptr, ctable);
+    SkAutoLockPixels autoLockPixels(bm);
+    *bm.getAddr8(0, 0) = 0;
+    SkAutoTUnref<SkImage> img(SkImage::NewFromBitmap(bm));
+    REPORTER_ASSERT(r, img.get() != nullptr);
+}