blob: 2af647d37e950d8751c16a5347338a89f25f86cb [file] [log] [blame]
Mike Reed43798692017-10-17 18:04:32 +00001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/codec/SkEncodedOrigin.h"
12#include "include/core/SkPixmap.h"
13#include "src/core/SkAutoPixmapStorage.h"
Mike Reed43798692017-10-17 18:04:32 +000014
15class SkPixmapPriv {
16public:
Mike Reed43798692017-10-17 18:04:32 +000017 /**
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 Osmanc337a632018-11-30 10:39:32 -050021 static bool Orient(const SkPixmap& dst, const SkPixmap& src, SkEncodedOrigin);
Leon Scroggins III0cbc10f2017-10-30 09:07:53 -040022
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 Osmanc337a632018-11-30 10:39:32 -050053 return Orient(dst, *tmp, origin);
Leon Scroggins III0cbc10f2017-10-30 09:07:53 -040054 }
55 return true;
56 }
Hal Canary99578d22017-12-14 21:13:47 -050057
58 static void ResetPixmapKeepInfo(SkPixmap* pm, const void* address, size_t rowBytes) {
59 pm->fRowBytes = rowBytes;
60 pm->fPixels = address;
61 }
Mike Reed43798692017-10-17 18:04:32 +000062};
63
64#endif