Home | History | Annotate | Download | only in tools
      1 /*
      2  * Copyright 2012 Google Inc.
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  */
      7 
      8 #include "picture_utils.h"
      9 #include "SkBitmap.h"
     10 #include "SkColorPriv.h"
     11 #include "SkHalf.h"
     12 #include "SkImageEncoder.h"
     13 #include "SkOSFile.h"
     14 #include "SkOSPath.h"
     15 #include "SkPM4fPriv.h"
     16 #include "SkPicture.h"
     17 #include "SkStream.h"
     18 #include "SkString.h"
     19 
     20 #include "sk_tool_utils.h"
     21 
     22 namespace sk_tools {
     23     void force_all_opaque(const SkBitmap& bitmap) {
     24         SkASSERT(kN32_SkColorType == bitmap.colorType());
     25         if (kN32_SkColorType == bitmap.colorType()) {
     26             return;
     27         }
     28 
     29         SkAutoLockPixels lock(bitmap);
     30         for (int y = 0; y < bitmap.height(); y++) {
     31             for (int x = 0; x < bitmap.width(); x++) {
     32                 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
     33             }
     34         }
     35     }
     36 
     37     void replace_char(SkString* str, const char oldChar, const char newChar) {
     38         if (nullptr == str) {
     39             return;
     40         }
     41         for (size_t i = 0; i < str->size(); ++i) {
     42             if (oldChar == str->operator[](i)) {
     43                 str->operator[](i) = newChar;
     44             }
     45         }
     46     }
     47 
     48     bool is_percentage(const char* const string) {
     49         SkString skString(string);
     50         return skString.endsWith("%");
     51     }
     52 
     53     void setup_bitmap(SkBitmap* bitmap, int width, int height) {
     54         bitmap->allocN32Pixels(width, height);
     55         bitmap->eraseColor(SK_ColorTRANSPARENT);
     56     }
     57 
     58     bool write_bitmap_to_disk(const SkBitmap& bm, const SkString& dirPath,
     59                               const char *subdirOrNull, const SkString& baseName) {
     60         SkString partialPath;
     61         if (subdirOrNull) {
     62             partialPath = SkOSPath::Join(dirPath.c_str(), subdirOrNull);
     63             sk_mkdir(partialPath.c_str());
     64         } else {
     65             partialPath.set(dirPath);
     66         }
     67         SkString fullPath = SkOSPath::Join(partialPath.c_str(), baseName.c_str());
     68         if (sk_tool_utils::EncodeImageToFile(fullPath.c_str(), bm, SkEncodedImageFormat::kPNG, 100)) {
     69             return true;
     70         } else {
     71             SkDebugf("Failed to write the bitmap to %s.\n", fullPath.c_str());
     72             return false;
     73         }
     74     }
     75 
     76     sk_sp<SkData> encode_bitmap_for_png(SkBitmap bitmap) {
     77         const int w = bitmap.width(),
     78                 h = bitmap.height();
     79         // PNG wants unpremultiplied 8-bit RGBA pixels (16-bit could work fine too).
     80         // We leave the gamma of these bytes unspecified, to continue the status quo,
     81         // which we think generally is to interpret them as sRGB.
     82 
     83         SkAutoTMalloc<uint32_t> rgba(w*h);
     84 
     85         auto srgbColorSpace = SkColorSpace::MakeSRGB();
     86         if (bitmap. colorType() ==  kN32_SkColorType &&
     87             bitmap.colorSpace() == srgbColorSpace.get()) {
     88             // These are premul sRGB 8-bit pixels in SkPMColor order.
     89             // We want unpremul sRGB 8-bit pixels in RGBA order.  We'll get there via floats.
     90             bitmap.lockPixels();
     91             auto px = (const uint32_t*)bitmap.getPixels();
     92             if (!px) {
     93                 return nullptr;
     94             }
     95             for (int i = 0; i < w*h; i++) {
     96                 Sk4f fs = Sk4f_fromS32(px[i]);         // Convert up to linear floats.
     97 #if defined(SK_PMCOLOR_IS_BGRA)
     98                 fs = SkNx_shuffle<2,1,0,3>(fs);        // Shuffle to RGBA, if not there already.
     99 #endif
    100                 float invA = 1.0f / fs[3];
    101                 fs = fs * Sk4f(invA, invA, invA, 1);   // Unpremultiply.
    102                 rgba[i] = Sk4f_toS32(fs);              // Pack down to sRGB bytes.
    103             }
    104 
    105         } else if (bitmap.colorType() == kRGBA_F16_SkColorType) {
    106             // These are premul linear half-float pixels in RGBA order.
    107             // We want unpremul sRGB 8-bit pixels in RGBA order.  We'll get there via floats.
    108             bitmap.lockPixels();
    109             auto px = (const uint64_t*)bitmap.getPixels();
    110             if (!px) {
    111                 return nullptr;
    112             }
    113             for (int i = 0; i < w*h; i++) {
    114                 // Convert up to linear floats.
    115                 Sk4f fs(SkHalfToFloat(static_cast<SkHalf>(px[i] >> (0 * 16))),
    116                         SkHalfToFloat(static_cast<SkHalf>(px[i] >> (1 * 16))),
    117                         SkHalfToFloat(static_cast<SkHalf>(px[i] >> (2 * 16))),
    118                         SkHalfToFloat(static_cast<SkHalf>(px[i] >> (3 * 16))));
    119                 fs = Sk4f::Max(0.0f, Sk4f::Min(fs, 1.0f));  // Clamp
    120                 float invA = 1.0f / fs[3];
    121                 fs = fs * Sk4f(invA, invA, invA, 1);  // Unpremultiply.
    122                 rgba[i] = Sk4f_toS32(fs);             // Pack down to sRGB bytes.
    123             }
    124 
    125         } else {
    126             // We "should" gamma correct in here but we don't.
    127             // We want Gold to show exactly what our clients are seeing, broken gamma.
    128 
    129             // Convert smaller formats up to premul linear 8-bit (in SkPMColor order).
    130             if (bitmap.colorType() != kN32_SkColorType) {
    131                 SkBitmap n32;
    132                 if (!bitmap.copyTo(&n32, kN32_SkColorType)) {
    133                     return nullptr;
    134                 }
    135                 bitmap = n32;
    136             }
    137 
    138             // Convert premul linear 8-bit to unpremul linear 8-bit RGBA.
    139             if (!bitmap.readPixels(SkImageInfo::Make(w,h, kRGBA_8888_SkColorType,
    140                                                      kUnpremul_SkAlphaType),
    141                                    rgba, 4*w, 0,0)) {
    142                 return nullptr;
    143             }
    144         }
    145 
    146         return SkData::MakeFromMalloc(rgba.release(), w*h*sizeof(uint32_t));
    147     }
    148 
    149 } // namespace sk_tools
    150