aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/SkPixmapPriv.h
blob: cced3e20b9ad761ba5a589a4fb2cd09f34db243f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
 * 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 "SkEncodedOrigin.h"
#include "SkAutoPixmapStorage.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(SkEncodedOrigin);

    /**
     *  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);

    static bool ShouldSwapWidthHeight(SkEncodedOrigin o);
    static SkImageInfo SwapWidthHeight(const SkImageInfo& info);

    /**
     *  Decode an image and then copy into dst, applying origin.
     *
     *  @param dst SkPixmap to write the final image, after
     *      applying the origin.
     *  @param origin SkEncodedOrigin to apply to the raw pixels.
     *  @param decode Function for decoding into a pixmap without
     *      applying the origin.
     */
    static bool Orient(const SkPixmap& dst, SkEncodedOrigin origin,
            std::function<bool(const SkPixmap&)> decode) {
        SkAutoPixmapStorage storage;
        const SkPixmap* tmp = &dst;
        if (origin != kTopLeft_SkEncodedOrigin) {
            auto info = dst.info();
            if (ShouldSwapWidthHeight(origin)) {
                info = SwapWidthHeight(info);
            }
            if (!storage.tryAlloc(info)) {
                return false;
            }
            tmp = &storage;
        }
        if (!decode(*tmp)) {
            return false;
        }
        if (tmp != &dst) {
            return Orient(dst, *tmp, OriginToOrient(origin));
        }
        return true;
    }

    static void ResetPixmapKeepInfo(SkPixmap* pm, const void* address, size_t rowBytes) {
        pm->fRowBytes = rowBytes;
        pm->fPixels = address;
    }
};

#endif