encode kAlpha_8 as grayalpha with sigbits for gray==1

Bug: skia:
Change-Id: Ib61e8e0f62af92d8746f5e73469002e7804a8447
Reviewed-on: https://skia-review.googlesource.com/78481
Reviewed-by: Leon Scroggins <scroggo@google.com>
Commit-Queue: Mike Reed <reed@google.com>
This commit is contained in:
Mike Reed 2017-11-30 15:33:04 -05:00 committed by Skia Commit-Bot
parent 74663e722e
commit d6cb11ee9d
6 changed files with 66 additions and 1 deletions

View File

@ -175,6 +175,10 @@ bool SkCodec::conversionSupported(const SkImageInfo& dst, SkEncodedInfo::Color s
case kGray_8_SkColorType:
return SkEncodedInfo::kGray_Color == srcColor && srcIsOpaque &&
!needs_color_xform(dst, srcCS, false);
case kAlpha_8_SkColorType:
// conceptually we can convert anything into alpha_8, but we haven't actually coded
// all of those other conversions yet, so only return true for the case we have codec.
return fSrcInfo.colorType() == kAlpha_8_SkColorType;;
default:
return false;
}

View File

@ -14,6 +14,7 @@
#include "SkMath.h"
#include "SkOpts.h"
#include "SkPngCodec.h"
#include "SkPngPriv.h"
#include "SkPoint3.h"
#include "SkSize.h"
#include "SkStream.h"
@ -935,7 +936,14 @@ void AutoCleanPng::infoCallback(size_t idatLength) {
SkEncodedInfo encodedInfo = SkEncodedInfo::Make(color, alpha, bitDepth);
SkImageInfo imageInfo = encodedInfo.makeImageInfo(origWidth, origHeight, colorSpace);
if (SkEncodedInfo::kOpaque_Alpha == alpha) {
if (encodedColorType == PNG_COLOR_TYPE_GRAY_ALPHA) {
png_color_8p sigBits;
if (png_get_sBIT(fPng_ptr, fInfo_ptr, &sigBits)) {
if (8 == sigBits->alpha && kGraySigBit_GrayAlphaIsJustAlpha == sigBits->gray) {
imageInfo = imageInfo.makeColorType(kAlpha_8_SkColorType);
}
}
} else if (SkEncodedInfo::kOpaque_Alpha == alpha) {
png_color_8p sigBits;
if (png_get_sBIT(fPng_ptr, fInfo_ptr, &sigBits)) {
if (5 == sigBits->red && 6 == sigBits->green && 5 == sigBits->blue) {

19
src/codec/SkPngPriv.h Normal file
View File

@ -0,0 +1,19 @@
/*
* Copyright 2017 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SkPngPriv_DEFINED
#define SkPngPriv_DEFINED
#include "SkTypes.h"
// We store kAlpha_8 images as GrayAlpha in png. Our private signal is significant bits for gray.
// If that is set to 1, we assume the gray channel can be ignored, and we output just alpha.
// We tried 0 at first, but png doesn't like a 0 sigbit for a channel it expects, hence we chose 1.
static constexpr int kGraySigBit_GrayAlphaIsJustAlpha = 1;
#endif

View File

@ -357,6 +357,16 @@ static void fast_swizzle_grayalpha_to_n32_premul(
SkOpts::grayA_to_rgbA((uint32_t*) dst, src + offset, width);
}
static void swizzle_grayalpha_to_a8(void* dst, const uint8_t* src, int width, int bpp,
int deltaSrc, int offset, const SkPMColor[]) {
src += offset;
uint8_t* dst8 = (uint8_t*)dst;
for (int x = 0; x < width; ++x) {
dst8[x] = src[1]; // src[0] is gray, ignored
src += deltaSrc;
}
}
// kBGR
static void swizzle_bgr_to_565(
@ -906,6 +916,9 @@ SkSwizzler* SkSwizzler::CreateSwizzler(const SkEncodedInfo& encodedInfo,
}
}
break;
case kAlpha_8_SkColorType:
proc = &swizzle_grayalpha_to_a8;
break;
default:
return nullptr;
}

View File

@ -91,6 +91,18 @@ static inline void transform_scanline_565(char* SK_RESTRICT dst, const char* SK_
}
}
/**
* Transform from kAlpha_8_Config to 2-bytes-per-pixel GrayAlpha.
*/
static inline void transform_scanline_A8_to_GrayAlpha(char* SK_RESTRICT dst,
const char* SK_RESTRICT src,
int width, int, const SkPMColor*) {
for (int i = 0; i < width; i++) {
*dst++ = 0; // gray (ignored)
*dst++ = *src++; // alpha
}
}
/**
* Transform from kRGBA_8888_SkColorType to 3-bytes-per-pixel RGB.
* Alpha channel data is abandoned.

View File

@ -15,6 +15,7 @@
#include "SkStream.h"
#include "SkString.h"
#include "SkPngEncoder.h"
#include "SkPngPriv.h"
#include "png.h"
@ -149,6 +150,12 @@ bool SkPngEncoderMgr::setHeader(const SkImageInfo& srcInfo, const SkPngEncoder::
fPngBytesPerPixel = 3;
SkASSERT(srcInfo.isOpaque());
break;
case kAlpha_8_SkColorType: // store as gray+alpha, but ignore gray
sigBit.gray = kGraySigBit_GrayAlphaIsJustAlpha;
sigBit.alpha = 8;
pngColorType = PNG_COLOR_TYPE_GRAY_ALPHA;
fPngBytesPerPixel = 2;
break;
default:
return false;
}
@ -254,6 +261,8 @@ static transform_scanline_proc choose_proc(const SkImageInfo& info,
SkASSERT(false);
return nullptr;
}
case kAlpha_8_SkColorType:
return transform_scanline_A8_to_GrayAlpha;
default:
SkASSERT(false);
return nullptr;