* Copyright 2015 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "include/core/SkSwizzle.h"
#include "include/private/SkImageInfoPriv.h"
#include "src/codec/SkSwizzler.h"
#include "src/core/SkOpts.h"
#include "tests/Test.h"
static void check_fill(skiatest::Reporter* r,
const SkImageInfo& imageInfo,
uint32_t startRow,
uint32_t endRow,
size_t rowBytes,
uint32_t offset) {
const size_t totalBytes = imageInfo.computeByteSize(rowBytes) + offset;
std::unique_ptr<uint8_t[]> storage(new uint8_t[totalBytes]);
memset(storage.get(), 0, totalBytes);
uint8_t* imageData = storage.get() + offset;
uint8_t* imageStart = imageData + rowBytes * startRow;
const SkImageInfo fillInfo = imageInfo.makeWH(imageInfo.width(), endRow - startRow + 1);
SkSampler::Fill(fillInfo, imageStart, rowBytes, SkCodec::kNo_ZeroInitialized);
uint8_t* indexPtr = imageData + startRow * rowBytes;
uint8_t* grayPtr = indexPtr;
uint32_t* colorPtr = (uint32_t*) indexPtr;
uint16_t* color565Ptr = (uint16_t*) indexPtr;
for (uint32_t y = startRow; y <= endRow; y++) {
for (int32_t x = 0; x < imageInfo.width(); x++) {
switch (imageInfo.colorType()) {
case kN32_SkColorType:
REPORTER_ASSERT(r, 0 == colorPtr[x]);
break;
case kGray_8_SkColorType:
REPORTER_ASSERT(r, 0 == grayPtr[x]);
break;
case kRGB_565_SkColorType:
REPORTER_ASSERT(r, 0 == color565Ptr[x]);
break;
default:
REPORTER_ASSERT(r, false);
break;
}
}
indexPtr += rowBytes;
colorPtr = (uint32_t*) indexPtr;
}
}
DEF_TEST(SwizzlerFill, r) {
const uint32_t widths[] = { 0, 10, 50 };
const uint32_t heights[] = { 1, 5, 10 };
const uint32_t paddings[] = { 0, 4 };
for (uint32_t width : widths) {
for (uint32_t height : heights) {
const SkImageInfo colorInfo = SkImageInfo::MakeN32(width, height, kUnknown_SkAlphaType);
const SkImageInfo grayInfo = colorInfo.makeColorType(kGray_8_SkColorType);
const SkImageInfo color565Info = colorInfo.makeColorType(kRGB_565_SkColorType);
for (uint32_t padding : paddings) {
const size_t colorRowBytes = SkColorTypeBytesPerPixel(kN32_SkColorType) * width
+ padding;
const size_t indexRowBytes = width + padding;
const size_t grayRowBytes = indexRowBytes;
const size_t color565RowBytes =
SkColorTypeBytesPerPixel(kRGB_565_SkColorType) * width + padding;
for (uint32_t offset = 0; offset <= padding; offset += 4) {
for (uint32_t startRow = 0; startRow < height; startRow++) {
for (uint32_t endRow = startRow; endRow < height; endRow++) {
check_fill(r, colorInfo, startRow, endRow, colorRowBytes, offset);
check_fill(r, grayInfo, startRow, endRow, grayRowBytes, offset);
check_fill(r, color565Info, startRow, endRow, color565RowBytes, offset);
}
}
}
}
}
}
}
DEF_TEST(SwizzleOpts, r) {
uint32_t dst, src;
for (int c = 0; c <= 255; c++) {
src = (255<<24) | c;
SkOpts::RGBA_to_rgbA(&dst, &src, 1);
REPORTER_ASSERT(r, dst == src);
SkOpts::RGBA_to_bgrA(&dst, &src, 1);
REPORTER_ASSERT(r, dst == (uint32_t)((255<<24) | (c<<16)));
src = (0<<24) | c;
SkOpts::RGBA_to_rgbA(&dst, &src, 1);
REPORTER_ASSERT(r, dst == 0);
SkOpts::RGBA_to_bgrA(&dst, &src, 1);
REPORTER_ASSERT(r, dst == 0);
}
src = 0xFACEB004;
SkOpts::RGBA_to_rgbA(&dst, &src, 1);
REPORTER_ASSERT(r, dst == 0xFACAAD04);
SkOpts::RGBA_to_BGRA(&dst, &src, 1);
REPORTER_ASSERT(r, dst == 0xFA04B0CE);
SkOpts::RGBA_to_bgrA(&dst, &src, 1);
REPORTER_ASSERT(r, dst == 0xFA04ADCA);
}
DEF_TEST(PublicSwizzleOpts, r) {
uint32_t dst, src;
src = 0xFACEB004;
SkSwapRB(&dst, &src, 1);
REPORTER_ASSERT(r, dst == 0xFA04B0CE);
}