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 
11 #include "include/codec/SkEncodedOrigin.h"
12 #include "include/core/SkPixmap.h"
13 #include "src/core/SkAutoPixmapStorage.h"
14 
15 class SkPixmapPriv {
16 public:
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      */
21     static bool Orient(const SkPixmap& dst, const SkPixmap& src, SkEncodedOrigin);
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 
36     template <typename Fn>
Orient(const SkPixmap & dst,SkEncodedOrigin origin,Fn && decode)37     static bool Orient(const SkPixmap& dst, SkEncodedOrigin origin, Fn&& decode) {
38         SkAutoPixmapStorage storage;
39         const SkPixmap* tmp = &dst;
40         if (origin != kTopLeft_SkEncodedOrigin) {
41             auto info = dst.info();
42             if (ShouldSwapWidthHeight(origin)) {
43                 info = SwapWidthHeight(info);
44             }
45             if (!storage.tryAlloc(info)) {
46                 return false;
47             }
48             tmp = &storage;
49         }
50         if (!decode(*tmp)) {
51             return false;
52         }
53         if (tmp != &dst) {
54             return Orient(dst, *tmp, origin);
55         }
56         return true;
57     }
58 
ResetPixmapKeepInfo(SkPixmap * pm,const void * address,size_t rowBytes)59     static void ResetPixmapKeepInfo(SkPixmap* pm, const void* address, size_t rowBytes) {
60         pm->fRowBytes = rowBytes;
61         pm->fPixels = address;
62     }
63 };
64 
65 #endif
66