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#include "skdiff.h"
8#include "skdiff_utils.h"
9#include "SkBitmap.h"
10#include "SkData.h"
11#include "SkImageDecoder.h"
12#include "SkImageEncoder.h"
13#include "SkStream.h"
14#include "SkTemplates.h"
15#include "SkTypes.h"
16
17bool are_buffers_equal(SkData* skdata1, SkData* skdata2) {
18    if ((NULL == skdata1) || (NULL == skdata2)) {
19        return false;
20    }
21    if (skdata1->size() != skdata2->size()) {
22        return false;
23    }
24    return (0 == memcmp(skdata1->data(), skdata2->data(), skdata1->size()));
25}
26
27SkData* read_file(const char* file_path) {
28    SkFILEStream fileStream(file_path);
29    if (!fileStream.isValid()) {
30        SkDebugf("WARNING: could not open file <%s> for reading\n", file_path);
31        return NULL;
32    }
33    size_t bytesInFile = fileStream.getLength();
34    size_t bytesLeftToRead = bytesInFile;
35
36    void* bufferStart = sk_malloc_throw(bytesInFile);
37    char* bufferPointer = (char*)bufferStart;
38    while (bytesLeftToRead > 0) {
39        size_t bytesReadThisTime = fileStream.read(bufferPointer, bytesLeftToRead);
40        if (0 == bytesReadThisTime) {
41            SkDebugf("WARNING: error reading from <%s>\n", file_path);
42            sk_free(bufferStart);
43            return NULL;
44        }
45        bytesLeftToRead -= bytesReadThisTime;
46        bufferPointer += bytesReadThisTime;
47    }
48    return SkData::NewFromMalloc(bufferStart, bytesInFile);
49}
50
51bool get_bitmap(SkData* fileBits, DiffResource& resource, SkImageDecoder::Mode mode) {
52    SkMemoryStream stream(fileBits->data(), fileBits->size());
53
54    SkImageDecoder* codec = SkImageDecoder::Factory(&stream);
55    if (NULL == codec) {
56        SkDebugf("ERROR: no codec found for <%s>\n", resource.fFullPath.c_str());
57        resource.fStatus = DiffResource::kCouldNotDecode_Status;
58        return false;
59    }
60
61    // In debug, the DLL will automatically be unloaded when this is deleted,
62    // but that shouldn't be a problem in release mode.
63    SkAutoTDelete<SkImageDecoder> ad(codec);
64
65    stream.rewind();
66    if (!codec->decode(&stream, &resource.fBitmap, kN32_SkColorType, mode)) {
67        SkDebugf("ERROR: codec failed for basePath <%s>\n", resource.fFullPath.c_str());
68        resource.fStatus = DiffResource::kCouldNotDecode_Status;
69        return false;
70    }
71
72    resource.fStatus = DiffResource::kDecoded_Status;
73    return true;
74}
75
76/** Thanks to PNG, we need to force all pixels 100% opaque. */
77static void force_all_opaque(const SkBitmap& bitmap) {
78   SkAutoLockPixels lock(bitmap);
79   for (int y = 0; y < bitmap.height(); y++) {
80       for (int x = 0; x < bitmap.width(); x++) {
81           *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
82       }
83   }
84}
85
86bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
87    SkBitmap copy;
88    bitmap.copyTo(&copy, kN32_SkColorType);
89    force_all_opaque(copy);
90    return SkImageEncoder::EncodeFile(path.c_str(), copy,
91                                      SkImageEncoder::kPNG_Type, 100);
92}
93
94/// Return a copy of the "input" string, within which we have replaced all instances
95/// of oldSubstring with newSubstring.
96///
97/// TODO: If we like this, we should move it into the core SkString implementation,
98/// adding more checks and ample test cases, and paying more attention to efficiency.
99static SkString replace_all(const SkString &input,
100                            const char oldSubstring[], const char newSubstring[]) {
101    SkString output;
102    const char *input_cstr = input.c_str();
103    const char *first_char = input_cstr;
104    const char *match_char;
105    size_t oldSubstringLen = strlen(oldSubstring);
106    while (NULL != (match_char = strstr(first_char, oldSubstring))) {
107        output.append(first_char, (match_char - first_char));
108        output.append(newSubstring);
109        first_char = match_char + oldSubstringLen;
110    }
111    output.append(first_char);
112    return output;
113}
114
115static SkString filename_to_derived_filename(const SkString& filename, const char *suffix) {
116    SkString diffName (filename);
117    const char* cstring = diffName.c_str();
118    size_t dotOffset = strrchr(cstring, '.') - cstring;
119    diffName.remove(dotOffset, diffName.size() - dotOffset);
120    diffName.append(suffix);
121
122    // In case we recursed into subdirectories, replace slashes with something else
123    // so the diffs will all be written into a single flat directory.
124    diffName = replace_all(diffName, PATH_DIV_STR, "_");
125    return diffName;
126}
127
128SkString filename_to_diff_filename(const SkString& filename) {
129    return filename_to_derived_filename(filename, "-diff.png");
130}
131
132SkString filename_to_white_filename(const SkString& filename) {
133    return filename_to_derived_filename(filename, "-white.png");
134}
135
136void create_and_write_diff_image(DiffRecord* drp,
137                                 DiffMetricProc dmp,
138                                 const int colorThreshold,
139                                 const SkString& outputDir,
140                                 const SkString& filename) {
141    const int w = drp->fBase.fBitmap.width();
142    const int h = drp->fBase.fBitmap.height();
143
144    if (w != drp->fComparison.fBitmap.width() || h != drp->fComparison.fBitmap.height()) {
145        drp->fResult = DiffRecord::kDifferentSizes_Result;
146    } else {
147        drp->fDifference.fBitmap.allocN32Pixels(w, h);
148
149        drp->fWhite.fBitmap.allocN32Pixels(w, h);
150
151        SkASSERT(DiffRecord::kUnknown_Result == drp->fResult);
152        compute_diff(drp, dmp, colorThreshold);
153        SkASSERT(DiffRecord::kUnknown_Result != drp->fResult);
154    }
155
156    if (outputDir.isEmpty()) {
157        drp->fDifference.fStatus = DiffResource::kUnspecified_Status;
158        drp->fWhite.fStatus = DiffResource::kUnspecified_Status;
159
160    } else {
161        drp->fDifference.fFilename = filename_to_diff_filename(filename);
162        drp->fDifference.fFullPath = outputDir;
163        drp->fDifference.fFullPath.append(drp->fDifference.fFilename);
164        drp->fDifference.fStatus = DiffResource::kSpecified_Status;
165
166        drp->fWhite.fFilename = filename_to_white_filename(filename);
167        drp->fWhite.fFullPath = outputDir;
168        drp->fWhite.fFullPath.append(drp->fWhite.fFilename);
169        drp->fWhite.fStatus = DiffResource::kSpecified_Status;
170
171        if (DiffRecord::kDifferentPixels_Result == drp->fResult) {
172            if (write_bitmap(drp->fDifference.fFullPath, drp->fDifference.fBitmap)) {
173                drp->fDifference.fStatus = DiffResource::kExists_Status;
174            } else {
175                drp->fDifference.fStatus = DiffResource::kDoesNotExist_Status;
176            }
177            if (write_bitmap(drp->fWhite.fFullPath, drp->fWhite.fBitmap)) {
178                drp->fWhite.fStatus = DiffResource::kExists_Status;
179            } else {
180                drp->fWhite.fStatus = DiffResource::kDoesNotExist_Status;
181            }
182        }
183    }
184}
185