#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "skia/ext/image_operations.h"
#include <stddef.h>
#include <stdint.h>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <numbers>
#include <vector>
#include "base/compiler_specific.h"
#include "base/files/file_util.h"
#include "base/strings/string_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkColorSpace.h"
#include "third_party/skia/include/core/SkImageInfo.h"
#include "third_party/skia/include/core/SkRect.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/geometry/size.h"
namespace {
uint32_t AveragePixel(const SkBitmap& bmp,
int x_min, int x_max,
int y_min, int y_max) { … }
SkColor AveragePixel(const SkColor colors[], size_t color_count) { … }
void PrintPixel(const SkBitmap& bmp,
int x_min, int x_max,
int y_min, int y_max) { … }
float ColorsEuclidianDistance(const SkColor a, const SkColor b) { … }
bool ColorsClose(uint32_t a, uint32_t b) { … }
void FillDataToBitmap(int w, int h, SkBitmap* bmp) { … }
void DrawCheckerToBitmap(int w, int h,
SkColor color1, SkColor color2,
int rect_w, int rect_h,
SkBitmap* bmp) { … }
#define DEBUG_BITMAP_GENERATION …
#if DEBUG_BITMAP_GENERATION
void SaveBitmapToPNG(const SkBitmap& bmp, const char* path) {
std::vector<unsigned char> png;
gfx::PNGCodec::ColorFormat color_format = gfx::PNGCodec::FORMAT_RGBA;
if (!gfx::PNGCodec::Encode(
reinterpret_cast<const unsigned char*>(bmp.getPixels()),
color_format, gfx::Size(bmp.width(), bmp.height()),
static_cast<int>(bmp.rowBytes()),
false, std::vector<gfx::PNGCodec::Comment>(), &png)) {
FAIL() << "Failed to encode image";
}
const base::FilePath fpath(path);
if (!base::WriteFile(fpath, png)) {
FAIL() << "Failed to write dest \"" << path << '"';
}
}
#endif
void CheckResampleToSame(skia::ImageOperations::ResizeMethod method) { … }
struct TestedResizeMethod { … };
struct TestedPixel { … };
void CheckResizeMethodShouldAverageGrid(
const SkBitmap& src,
const TestedResizeMethod& tested_method,
int dest_w, int dest_h, SkColor average_color,
bool* method_passed) { … }
}
#if 0 && DEBUG_BITMAP_GENERATION
TEST(ImageOperations, GenerateGradientBitmap) {
const int src_w = 640, src_h = 480;
SkBitmap src;
FillDataToBitmap(src_w, src_h, &src);
SaveBitmapToPNG(src, "/tmp/gradient_640x480.png");
}
TEST(ImageOperations, GenerateGridBitmap) {
const int src_w = 640, src_h = 480, src_grid_pitch = 10, src_grid_width = 4;
const SkColor grid_color = SK_ColorRED, background_color = SK_ColorBLUE;
SkBitmap src;
DrawGridToBitmap(src_w, src_h,
background_color, grid_color,
src_grid_pitch, src_grid_width,
&src);
SaveBitmapToPNG(src, "/tmp/grid_640x408_10_4_red_blue.png");
}
TEST(ImageOperations, GenerateCheckerBitmap) {
const int src_w = 640, src_h = 480, rect_w = 10, rect_h = 4;
const SkColor color1 = SK_ColorRED, color2 = SK_ColorBLUE;
SkBitmap src;
DrawCheckerToBitmap(src_w, src_h, color1, color2, rect_w, rect_h, &src);
SaveBitmapToPNG(src, "/tmp/checker_640x408_10_4_red_blue.png");
}
#endif
TEST(ImageOperations, Halve) { … }
TEST(ImageOperations, HalveSubset) { … }
TEST(ImageOperations, InvalidParams) { … }
TEST(ImageOperations, ResampleToSameHamming1) { … }
TEST(ImageOperations, ResampleToSameLanczos3) { … }
TEST(ImageOperations, ResizeShouldAverageColors) { … }
static double sinc(double x) { … }
static double lanczos3(double offset) { … }
TEST(ImageOperations, ScaleUp) { … }