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 "SkPixmap.h"
12 #include "SkEncodedOrigin.h"
13 #include "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      */
Orient(const SkPixmap & dst,SkEncodedOrigin origin,std::function<bool (const SkPixmap &)> decode)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) {
53             return Orient(dst, *tmp, origin);
54         }
55         return true;
56     }
57 
ResetPixmapKeepInfo(SkPixmap * pm,const void * address,size_t rowBytes)58     static void ResetPixmapKeepInfo(SkPixmap* pm, const void* address, size_t rowBytes) {
59         pm->fRowBytes = rowBytes;
60         pm->fPixels = address;
61     }
62 };
63 
64 #endif
65