int main (int argc, char * const argv[]) { SkAutoGraphics ag; int i, outDirIndex = 0; SkString outDir; for (i = 1; i < argc; i++) { if (!strcmp(argv[i], "-help")) { show_help(); return 0; } if (!strcmp(argv[i], "-o")) { if (i == argc-1) { SkDebugf("ERROR: -o needs a following filename\n"); return -1; } outDirIndex = i; outDir.set(argv[i+1]); if (outDir.c_str()[outDir.size() - 1] != '/') { outDir.append("/"); } i += 1; // skip the out dir name } } for (i = 1; i < argc; i++) { if (i == outDirIndex) { i += 1; // skip this and the next entry continue; } SkBitmap bitmap; if (decodeFile(&bitmap, argv[i])) { if (outDirIndex) { SkString outPath; make_outname(&outPath, outDir.c_str(), argv[i]); SkDebugf(" writing %s\n", outPath.c_str()); SkImageEncoder::EncodeFile(outPath.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100); } else { SkDebugf(" decoded %s [%d %d]\n", argv[i], bitmap.width(), bitmap.height()); } } } return 0; }
/** * Encode the bitmap to a file, written one of two ways, depending on * FLAGS_writeChecksumBasedFilenames. If true, the final image will be * written to: * outDir/hashType/src/digestValue.png * If false, the final image will be written out to: * outDir/src.png * The function returns whether the file was successfully written. */ static bool write_bitmap(const char outDir[], const char src[], const skiagm::BitmapAndDigest& bitmapAndDigest) { SkString filename; if (FLAGS_writeChecksumBasedFilenames) { // First create the directory for the hashtype. const SkString hashType = bitmapAndDigest.fDigest.getHashType(); const SkString hashDir = SkOSPath::SkPathJoin(outDir, hashType.c_str()); if (!sk_mkdir(hashDir.c_str())) { return false; } // Now create the name of the folder specific to this image. SkString basename = SkOSPath::SkBasename(src); const SkString imageDir = SkOSPath::SkPathJoin(hashDir.c_str(), basename.c_str()); if (!sk_mkdir(imageDir.c_str())) { return false; } // Name the file <digest>.png SkString checksumBasedName = bitmapAndDigest.fDigest.getDigestValue(); checksumBasedName.append(".png"); filename = SkOSPath::SkPathJoin(imageDir.c_str(), checksumBasedName.c_str()); } else { make_outname(&filename, outDir, src, ".png"); } const SkBitmap& bm = bitmapAndDigest.fBitmap; if (SkImageEncoder::EncodeFile(filename.c_str(), bm, SkImageEncoder::kPNG_Type, 100)) { return true; } if (bm.colorType() == kN32_SkColorType) { // First attempt at encoding failed, and the bitmap was already 8888. Making // a copy is not going to help. return false; } // Encoding failed. Copy to 8888 and try again. SkBitmap bm8888; if (!bm.copyTo(&bm8888, kN32_SkColorType)) { return false; } return SkImageEncoder::EncodeFile(filename.c_str(), bm8888, SkImageEncoder::kPNG_Type, 100); }