• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 Google LLC.
2 // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3 
4 #include "include/codec/SkCodec.h"
5 #include "include/core/SkBitmap.h"
6 #include "include/encode/SkPngEncoder.h"
7 #include "src/core/SkOSFile.h"
8 
update(SkBitmap * maxBitmap,SkBitmap * minBitmap,const SkBitmap & bm)9 static void update(SkBitmap* maxBitmap, SkBitmap* minBitmap, const SkBitmap& bm) {
10     SkASSERT(!bm.drawsNothing());
11     SkASSERT(4 == bm.bytesPerPixel());
12     if (maxBitmap->drawsNothing()) {
13         maxBitmap->allocPixels(bm.info());
14         maxBitmap->eraseColor(0x00000000);
15         minBitmap->allocPixels(bm.info());
16         minBitmap->eraseColor(0xFFFFFFFF);
17     }
18     SkASSERT_RELEASE(maxBitmap->info() == bm.info());
19     const SkPixmap& pmin = minBitmap->pixmap();
20     const SkPixmap& pmax = maxBitmap->pixmap();
21     const SkPixmap& pm = bm.pixmap();
22     for (int y = 0; y < pm.height(); ++y) {
23         for (int x = 0; x < pm.width(); ++x) {
24             uint32_t* minPtr = pmin.writable_addr32(x, y);
25             uint32_t* maxPtr = pmax.writable_addr32(x, y);
26             uint8_t minColor[4], maxColor[4], color[4];
27             memcpy(minColor, minPtr, 4);
28             memcpy(maxColor, maxPtr, 4);
29             memcpy(color, pm.addr32(x, y), 4);
30             for (unsigned i = 0; i < 4; ++i) {
31                 minColor[i] = std::min(minColor[i], color[i]);
32                 maxColor[i] = std::max(maxColor[i], color[i]);
33             }
34             memcpy(minPtr, minColor, 4);
35             memcpy(maxPtr, maxColor, 4);
36         }
37     }
38 }
39 
decode_to_srgb_8888_unpremul(const char * path)40 static SkBitmap decode_to_srgb_8888_unpremul(const char* path) {
41     SkBitmap dst;
42     if (auto codec = SkCodec::MakeFromData(SkData::MakeFromFileName(path))) {
43         SkISize size = codec->getInfo().dimensions();
44         SkASSERT(!size.isEmpty());
45         dst.allocPixels(SkImageInfo::Make(
46                     size.width(), size.height(), kRGBA_8888_SkColorType,
47                     kUnpremul_SkAlphaType, SkColorSpace::MakeSRGB()));
48         if (SkCodec::kSuccess != codec->getPixels(dst.pixmap())) {
49             dst.reset();
50         }
51     }
52     return dst;
53 }
54 
encode_png(const char * path,const SkPixmap & pixmap)55 bool encode_png(const char* path, const SkPixmap& pixmap) {
56     if (!pixmap.addr()) {
57         return false;
58     }
59     SkPngEncoder::Options encOpts;
60     encOpts.fZLibLevel = 9;  // slow encode;
61     SkFILEWStream o(path);
62     return o.isValid() && SkPngEncoder::Encode(&o, pixmap, encOpts);
63 }
64 
main(int argc,char ** argv)65 int main(int argc, char** argv) {
66     SkASSERT_RELEASE(argc > 2);
67     const char* src_dir = argv[1];
68     const char* dst_dir = argv[2];
69     SkBitmap maxBitmap, minBitmap;
70     SkOSFile::Iter iter(src_dir);
71     SkString name;
72     while (iter.next(&name)) {
73         name.prependf("%s/", src_dir);
74         SkBitmap bm = decode_to_srgb_8888_unpremul(name.c_str());
75         if (!bm.drawsNothing()) {
76             update(&maxBitmap, &minBitmap, bm);
77         }
78     }
79     SkASSERT_RELEASE(sk_mkdir(dst_dir));
80     encode_png(SkStringPrintf("%s/min.png", dst_dir).c_str(), minBitmap.pixmap());
81     encode_png(SkStringPrintf("%s/max.png", dst_dir).c_str(), maxBitmap.pixmap());
82 }
83