Move SkCodec::Origin into its own file

It is now used by SkPixmap and will soon be in SkJpegEncoder. No need
for those to depend on SkCodec.

Bug: 768878
TBR=reed@google.com
(reed@ already approved the API change in
https://skia-review.googlesource.com/60721)
Change-Id: If1a6e1d5b60a7a3d8c97818e15a48d28ba804668
Reviewed-on: https://skia-review.googlesource.com/61680
Reviewed-by: Leon Scroggins <scroggo@google.com>
Commit-Queue: Leon Scroggins <scroggo@google.com>
This commit is contained in:
Leon Scroggins III 2017-10-18 14:42:43 -04:00 committed by Skia Commit-Bot
parent 2240be96d9
commit b6ab10f34b
8 changed files with 57 additions and 45 deletions

View File

@ -14,6 +14,7 @@
#include "SkColorSpaceXform.h"
#include "SkEncodedImageFormat.h"
#include "SkEncodedInfo.h"
#include "SkEncodedOrigin.h"
#include "SkImageInfo.h"
#include "SkPixmap.h"
#include "SkSize.h"
@ -170,24 +171,11 @@ public:
const SkEncodedInfo& getEncodedInfo() const { return fEncodedInfo; }
enum Origin {
kTopLeft_Origin = 1, // Default
kTopRight_Origin = 2, // Reflected across y-axis
kBottomRight_Origin = 3, // Rotated 180
kBottomLeft_Origin = 4, // Reflected across x-axis
kLeftTop_Origin = 5, // Reflected across x-axis, Rotated 90 CCW
kRightTop_Origin = 6, // Rotated 90 CW
kRightBottom_Origin = 7, // Reflected across x-axis, Rotated 90 CW
kLeftBottom_Origin = 8, // Rotated 90 CCW
kDefault_Origin = kTopLeft_Origin,
kLast_Origin = kLeftBottom_Origin,
};
/**
* Returns the image orientation stored in the EXIF data.
* If there is no EXIF data, or if we cannot read the EXIF data, returns kTopLeft.
*/
Origin getOrigin() const { return fOrigin; }
SkEncodedOrigin getOrigin() const { return fOrigin; }
/**
* Return a size that approximately supports the desired scale factor.
@ -682,7 +670,7 @@ protected:
XformFormat srcFormat,
std::unique_ptr<SkStream>,
sk_sp<SkColorSpace>,
Origin = kTopLeft_Origin);
SkEncodedOrigin = kTopLeft_SkEncodedOrigin);
/**
* Allows the subclass to set the recommended SkImageInfo
@ -691,7 +679,7 @@ protected:
const SkImageInfo&,
XformFormat srcFormat,
std::unique_ptr<SkStream>,
Origin = kTopLeft_Origin);
SkEncodedOrigin = kTopLeft_SkEncodedOrigin);
virtual SkISize onGetScaledDimensions(float /*desiredScale*/) const {
// By default, scaling is not supported.
@ -841,7 +829,7 @@ private:
const XformFormat fSrcXformFormat;
std::unique_ptr<SkStream> fStream;
bool fNeedsRewind;
const Origin fOrigin;
const SkEncodedOrigin fOrigin;
SkImageInfo fDstInfo;
Options fOptions;

View File

@ -0,0 +1,23 @@
/*
* 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 SkEncodedOrigin_DEFINED
#define SkEncodedOrigin_DEFINED
// These values match the orientation www.exif.org/Exif2-2.PDF.
enum SkEncodedOrigin {
kTopLeft_SkEncodedOrigin = 1, // Default
kTopRight_SkEncodedOrigin = 2, // Reflected across y-axis
kBottomRight_SkEncodedOrigin = 3, // Rotated 180
kBottomLeft_SkEncodedOrigin = 4, // Reflected across x-axis
kLeftTop_SkEncodedOrigin = 5, // Reflected across x-axis, Rotated 90 CCW
kRightTop_SkEncodedOrigin = 6, // Rotated 90 CW
kRightBottom_SkEncodedOrigin = 7, // Reflected across x-axis, Rotated 90 CW
kLeftBottom_SkEncodedOrigin = 8, // Rotated 90 CCW
kDefault_SkEncodedOrigin = kTopLeft_SkEncodedOrigin,
kLast_SkEncodedOrigin = kLeftBottom_SkEncodedOrigin,
};
#endif // SkEncodedOrigin_DEFINED

View File

@ -128,7 +128,7 @@ std::unique_ptr<SkCodec> SkCodec::MakeFromData(sk_sp<SkData> data, SkPngChunkRea
SkCodec::SkCodec(int width, int height, const SkEncodedInfo& info,
XformFormat srcFormat, std::unique_ptr<SkStream> stream,
sk_sp<SkColorSpace> colorSpace, Origin origin)
sk_sp<SkColorSpace> colorSpace, SkEncodedOrigin origin)
: fEncodedInfo(info)
, fSrcInfo(info.makeImageInfo(width, height, std::move(colorSpace)))
, fSrcXformFormat(srcFormat)
@ -142,7 +142,8 @@ SkCodec::SkCodec(int width, int height, const SkEncodedInfo& info,
{}
SkCodec::SkCodec(const SkEncodedInfo& info, const SkImageInfo& imageInfo,
XformFormat srcFormat, std::unique_ptr<SkStream> stream, Origin origin)
XformFormat srcFormat, std::unique_ptr<SkStream> stream,
SkEncodedOrigin origin)
: fEncodedInfo(info)
, fSrcInfo(imageInfo)
, fSrcXformFormat(srcFormat)

View File

@ -15,23 +15,23 @@
#define kSwapXY SkPixmapPriv::kSwapXY
const uint8_t gOrientationFlags[] = {
0, // kTopLeft_Origin
kMirrorX, // kTopRight_Origin
kMirrorX | kMirrorY, // kBottomRight_Origin
kMirrorY, // kBottomLeft_Origin
kSwapXY, // kLeftTop_Origin
kMirrorX | kSwapXY, // kRightTop_Origin
kMirrorX | kMirrorY | kSwapXY, // kRightBottom_Origin
kMirrorY | kSwapXY, // kLeftBottom_Origin
0, // kTopLeft_SkEncodedOrigin
kMirrorX, // kTopRight_SkEncodedOrigin
kMirrorX | kMirrorY, // kBottomRight_SkEncodedOrigin
kMirrorY, // kBottomLeft_SkEncodedOrigin
kSwapXY, // kLeftTop_SkEncodedOrigin
kMirrorX | kSwapXY, // kRightTop_SkEncodedOrigin
kMirrorX | kMirrorY | kSwapXY, // kRightBottom_SkEncodedOrigin
kMirrorY | kSwapXY, // kLeftBottom_SkEncodedOrigin
};
SkPixmapPriv::OrientFlags SkPixmapPriv::OriginToOrient(SkCodec::Origin o) {
SkPixmapPriv::OrientFlags SkPixmapPriv::OriginToOrient(SkEncodedOrigin o) {
unsigned io = static_cast<int>(o) - 1;
SkASSERT(io < SK_ARRAY_COUNT(gOrientationFlags));
return static_cast<SkPixmapPriv::OrientFlags>(gOrientationFlags[io]);
}
static bool should_swap_width_height(SkCodec::Origin o) {
static bool should_swap_width_height(SkEncodedOrigin o) {
return SkToBool(SkPixmapPriv::OriginToOrient(o) & kSwapXY);
}
@ -73,12 +73,12 @@ SkData* SkCodecImageGenerator::onRefEncodedData() {
bool SkCodecImageGenerator::onGetPixels(const SkImageInfo& requestInfo, void* requestPixels,
size_t requestRowBytes, const Options& opts) {
const SkCodec::Origin origin = fCodec->getOrigin();
const auto origin = fCodec->getOrigin();
const SkPixmap request(requestInfo, requestPixels, requestRowBytes);
const SkPixmap* codecMap = &request;
SkAutoPixmapStorage storage; // used if we have to post-orient the output from the codec
if (origin != SkCodec::kTopLeft_Origin) {
if (origin != kTopLeft_SkEncodedOrigin) {
SkImageInfo info = requestInfo;
if (should_swap_width_height(origin)) {
info = swap_width_height(info);

View File

@ -45,7 +45,7 @@ static uint32_t get_endian_int(const uint8_t* data, bool littleEndian) {
const uint32_t kExifHeaderSize = 14;
const uint32_t kExifMarker = JPEG_APP0 + 1;
static bool is_orientation_marker(jpeg_marker_struct* marker, SkCodec::Origin* orientation) {
static bool is_orientation_marker(jpeg_marker_struct* marker, SkEncodedOrigin* orientation) {
if (kExifMarker != marker->marker || marker->data_length < kExifHeaderSize) {
return false;
}
@ -87,8 +87,8 @@ static bool is_orientation_marker(jpeg_marker_struct* marker, SkCodec::Origin* o
uint32_t count = get_endian_int(data + 4, littleEndian);
if (kOriginTag == tag && kOriginType == type && 1 == count) {
uint16_t val = get_endian_short(data + 8, littleEndian);
if (0 < val && val <= SkCodec::kLast_Origin) {
*orientation = (SkCodec::Origin) val;
if (0 < val && val <= kLast_SkEncodedOrigin) {
*orientation = (SkEncodedOrigin) val;
return true;
}
}
@ -97,15 +97,15 @@ static bool is_orientation_marker(jpeg_marker_struct* marker, SkCodec::Origin* o
return false;
}
static SkCodec::Origin get_exif_orientation(jpeg_decompress_struct* dinfo) {
SkCodec::Origin orientation;
static SkEncodedOrigin get_exif_orientation(jpeg_decompress_struct* dinfo) {
SkEncodedOrigin orientation;
for (jpeg_marker_struct* marker = dinfo->marker_list; marker; marker = marker->next) {
if (is_orientation_marker(marker, &orientation)) {
return orientation;
}
}
return SkCodec::kDefault_Origin;
return kDefault_SkEncodedOrigin;
}
static bool is_icc_marker(jpeg_marker_struct* marker) {
@ -227,7 +227,7 @@ SkCodec::Result SkJpegCodec::ReadHeader(SkStream* stream, SkCodec** codecOut,
// Create image info object and the codec
SkEncodedInfo info = SkEncodedInfo::Make(color, SkEncodedInfo::kOpaque_Alpha, 8);
Origin orientation = get_exif_orientation(decoderMgr->dinfo());
SkEncodedOrigin orientation = get_exif_orientation(decoderMgr->dinfo());
sk_sp<SkData> iccData = get_icc_profile(decoderMgr->dinfo());
sk_sp<SkColorSpace> colorSpace = nullptr;
if (iccData) {
@ -284,7 +284,7 @@ std::unique_ptr<SkCodec> SkJpegCodec::MakeFromStream(std::unique_ptr<SkStream> s
SkJpegCodec::SkJpegCodec(int width, int height, const SkEncodedInfo& info,
std::unique_ptr<SkStream> stream, JpegDecoderMgr* decoderMgr,
sk_sp<SkColorSpace> colorSpace, Origin origin)
sk_sp<SkColorSpace> colorSpace, SkEncodedOrigin origin)
: INHERITED(width, height, info, SkColorSpaceXform::kRGBA_8888_ColorFormat, std::move(stream),
std::move(colorSpace), origin)
, fDecoderMgr(decoderMgr)

View File

@ -107,7 +107,7 @@ private:
* takes ownership
*/
SkJpegCodec(int width, int height, const SkEncodedInfo& info, std::unique_ptr<SkStream> stream,
JpegDecoderMgr* decoderMgr, sk_sp<SkColorSpace> colorSpace, Origin origin);
JpegDecoderMgr* decoderMgr, sk_sp<SkColorSpace> colorSpace, SkEncodedOrigin origin);
/*
* Checks if the conversion between the input image and the requested output

View File

@ -9,7 +9,7 @@
#define SkPixmapPriv_DEFINED
#include "SkPixmap.h"
#include "SkCodec.h"
#include "SkEncodedOrigin.h"
class SkPixmapPriv {
public:
@ -20,7 +20,7 @@ public:
kSwapXY = 1 << 2,
};
static OrientFlags OriginToOrient(SkCodec::Origin);
static OrientFlags OriginToOrient(SkEncodedOrigin);
/**
* Copy the pixels in this pixmap into dst, applying the orientation transformations specified

View File

@ -18,11 +18,11 @@ DEF_TEST(ExifOrientation, r) {
std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(std::move(stream)));
REPORTER_ASSERT(r, nullptr != codec);
SkCodec::Origin origin = codec->getOrigin();
REPORTER_ASSERT(r, SkCodec::kTopRight_Origin == origin);
SkEncodedOrigin origin = codec->getOrigin();
REPORTER_ASSERT(r, kTopRight_SkEncodedOrigin == origin);
codec = SkCodec::MakeFromStream(GetResourceAsStream("mandrill_512_q075.jpg"));
REPORTER_ASSERT(r, nullptr != codec);
origin = codec->getOrigin();
REPORTER_ASSERT(r, SkCodec::kTopLeft_Origin == origin);
REPORTER_ASSERT(r, kTopLeft_SkEncodedOrigin == origin);
}