Mike Reed | 4379869 | 2017-10-17 18:04:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #ifndef SkPixmapPriv_DEFINED |
| 9 | #define SkPixmapPriv_DEFINED |
| 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/codec/SkEncodedOrigin.h" |
| 12 | #include "include/core/SkPixmap.h" |
| 13 | #include "src/core/SkAutoPixmapStorage.h" |
Mike Reed | 4379869 | 2017-10-17 18:04:32 +0000 | [diff] [blame] | 14 | |
| 15 | class SkPixmapPriv { |
| 16 | public: |
Mike Reed | 4379869 | 2017-10-17 18:04:32 +0000 | [diff] [blame] | 17 | /** |
| 18 | * Copy the pixels in this pixmap into dst, applying the orientation transformations specified |
| 19 | * by the flags. If the inputs are invalid, this returns false and no copy is made. |
| 20 | */ |
Brian Osman | c337a63 | 2018-11-30 10:39:32 -0500 | [diff] [blame] | 21 | static bool Orient(const SkPixmap& dst, const SkPixmap& src, SkEncodedOrigin); |
Leon Scroggins III | 0cbc10f | 2017-10-30 09:07:53 -0400 | [diff] [blame] | 22 | |
| 23 | static bool ShouldSwapWidthHeight(SkEncodedOrigin o); |
| 24 | static SkImageInfo SwapWidthHeight(const SkImageInfo& info); |
| 25 | |
| 26 | /** |
| 27 | * Decode an image and then copy into dst, applying origin. |
| 28 | * |
| 29 | * @param dst SkPixmap to write the final image, after |
| 30 | * applying the origin. |
| 31 | * @param origin SkEncodedOrigin to apply to the raw pixels. |
| 32 | * @param decode Function for decoding into a pixmap without |
| 33 | * applying the origin. |
| 34 | */ |
| 35 | static bool Orient(const SkPixmap& dst, SkEncodedOrigin origin, |
| 36 | std::function<bool(const SkPixmap&)> decode) { |
| 37 | SkAutoPixmapStorage storage; |
| 38 | const SkPixmap* tmp = &dst; |
| 39 | if (origin != kTopLeft_SkEncodedOrigin) { |
| 40 | auto info = dst.info(); |
| 41 | if (ShouldSwapWidthHeight(origin)) { |
| 42 | info = SwapWidthHeight(info); |
| 43 | } |
| 44 | if (!storage.tryAlloc(info)) { |
| 45 | return false; |
| 46 | } |
| 47 | tmp = &storage; |
| 48 | } |
| 49 | if (!decode(*tmp)) { |
| 50 | return false; |
| 51 | } |
| 52 | if (tmp != &dst) { |
Brian Osman | c337a63 | 2018-11-30 10:39:32 -0500 | [diff] [blame] | 53 | return Orient(dst, *tmp, origin); |
Leon Scroggins III | 0cbc10f | 2017-10-30 09:07:53 -0400 | [diff] [blame] | 54 | } |
| 55 | return true; |
| 56 | } |
Hal Canary | 99578d2 | 2017-12-14 21:13:47 -0500 | [diff] [blame] | 57 | |
| 58 | static void ResetPixmapKeepInfo(SkPixmap* pm, const void* address, size_t rowBytes) { |
| 59 | pm->fRowBytes = rowBytes; |
| 60 | pm->fPixels = address; |
| 61 | } |
Mike Reed | 4379869 | 2017-10-17 18:04:32 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | #endif |