diff options
author | 2017-10-16 11:42:53 -0400 | |
---|---|---|
committer | 2017-10-16 17:33:22 +0000 | |
commit | fa15877f487333bec876e7315cf584c0d598d098 (patch) | |
tree | 7ee6ce5183e8cf12505f69d0f1b5bcb7abfccd85 /src/core | |
parent | d28a79d49549568c3ebac36288ea3965496333f8 (diff) |
apply codec origin in generator
Bug: skia:
Change-Id: I383dacb49b1e3c88467ccdbf3288764bb1bbf01a
Reviewed-on: https://skia-review.googlesource.com/58600
Reviewed-by: Leon Scroggins <scroggo@google.com>
Commit-Queue: Mike Reed <reed@google.com>
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/SkPixmap.cpp | 82 | ||||
-rw-r--r-- | src/core/SkPixmapPriv.h | 33 |
2 files changed, 114 insertions, 1 deletions
diff --git a/src/core/SkPixmap.cpp b/src/core/SkPixmap.cpp index 1cee0a9d02..7ce97d23f2 100644 --- a/src/core/SkPixmap.cpp +++ b/src/core/SkPixmap.cpp @@ -15,9 +15,10 @@ #include "SkMask.h" #include "SkNx.h" #include "SkPM4f.h" -#include "SkPixmap.h" +#include "SkPixmapPriv.h" #include "SkReadPixelsRec.h" #include "SkSurface.h" +#include "SkTemplates.h" #include "SkUtils.h" ///////////////////////////////////////////////////////////////////////////////////////////////// @@ -381,3 +382,82 @@ bool SkPixmap::computeIsOpaque() const { } return false; } + +////////////////////////////////////////////////////////////////////////////////////////////////// + +template <typename T, typename F> +void apply(const SkPixmap& dst, const SkPixmap& src, unsigned flags, F getAddr) { + const int maxX = dst.width() - 1; + const int maxY = dst.height() - 1; + T* dstRow = (T*)dst.writable_addr(); + for (int dy = 0; dy < dst.height(); ++dy) { + for (int dx = 0; dx < dst.width(); ++dx) { + int sx = dx; + int sy = dy; + if (flags & SkPixmapPriv::kMirrorX) { + sx = maxX - sx; + } + if (flags & SkPixmapPriv::kMirrorY) { + sy = maxY - sy; + } + if (flags & SkPixmapPriv::kSwapXY) { + SkTSwap<int>(sx, sy); + } + dstRow[dx] = getAddr(src, sx, sy); + } + dstRow = SkTAddOffset<T>(dstRow, dst.rowBytes()); + } +} + +static bool apply_orientation(const SkPixmap& dst, const SkPixmap& src, unsigned flags) { + SkASSERT(dst.colorType() == src.colorType()); + + switch (dst.info().bytesPerPixel()) { + case 1: + apply<uint8_t>(dst, src, flags, [](const SkPixmap& pm, int x, int y) { + return *pm.addr8(x, y); + }); break; + case 2: + apply<uint16_t>(dst, src, flags, [](const SkPixmap& pm, int x, int y) { + return *pm.addr16(x, y); + }); break; + case 4: + apply<uint32_t>(dst, src, flags, [](const SkPixmap& pm, int x, int y) { + return *pm.addr32(x, y); + }); break; + case 8: + apply<uint64_t>(dst, src, flags, [](const SkPixmap& pm, int x, int y) { + return *pm.addr64(x, y); + }); break; + default: + return false; + } + return true; +} + +bool SkPixmapPriv::Orient(const SkPixmap& dst, const SkPixmap& src, OrientFlags flags) { + SkASSERT((flags & ~(kMirrorX | kMirrorY | kSwapXY)) == 0); + if (src.colorType() != dst.colorType()) { + return false; + } + // note: we just ignore alphaType and colorSpace for this transformation + + int w = src.width(); + int h = src.height(); + if (flags & kSwapXY) { + SkTSwap(w, h); + } + if (dst.width() != w || dst.height() != h) { + return false; + } + if (w == 0 || h == 0) { + return true; + } + + // check for aliasing to self + if (src.addr() == dst.addr()) { + return flags == 0; + } + return apply_orientation(dst, src, flags); +} + diff --git a/src/core/SkPixmapPriv.h b/src/core/SkPixmapPriv.h new file mode 100644 index 0000000000..dfc9ce2dd0 --- /dev/null +++ b/src/core/SkPixmapPriv.h @@ -0,0 +1,33 @@ +/* + * Copyright 2015 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef SkPixmapPriv_DEFINED +#define SkPixmapPriv_DEFINED + +#include "SkPixmap.h" +#include "SkCodec.h" + +class SkPixmapPriv { +public: + // These flag are applied in this order (swap is applied last) + enum OrientFlags { + kMirrorX = 1 << 0, + kMirrorY = 1 << 1, + kSwapXY = 1 << 2, + }; + + static OrientFlags OriginToOrient(SkCodec::Origin); + + /** + * Copy the pixels in this pixmap into dst, applying the orientation transformations specified + * by the flags. If the inputs are invalid, this returns false and no copy is made. + */ + static bool Orient(const SkPixmap& dst, const SkPixmap& src, OrientFlags); +}; + +#endif + |