xref: /aosp_15_r20/external/skia/src/codec/SkPixmapUtilsPriv.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 SkPixmapUtilsPriv_DEFINED
9 #define SkPixmapUtilsPriv_DEFINED
10 
11 #include "include/codec/SkEncodedOrigin.h"
12 #include "include/codec/SkPixmapUtils.h"
13 #include "include/core/SkImageInfo.h"
14 #include "include/core/SkPixmap.h"
15 #include "src/core/SkAutoPixmapStorage.h"
16 
17 namespace SkPixmapUtils {
18 
19 /**
20  *  Decode an image and then copy into dst, applying origin.
21  *
22  *  @param dst SkPixmap to write the final image, after
23  *      applying the origin.
24  *  @param origin SkEncodedOrigin to apply to the raw pixels.
25  *  @param decode Function for decoding into a pixmap without
26  *      applying the origin.
27  */
28 
29 template <typename Fn>
Orient(const SkPixmap & dst,SkEncodedOrigin origin,Fn && decode)30 bool Orient(const SkPixmap& dst, SkEncodedOrigin origin, Fn&& decode) {
31     SkAutoPixmapStorage storage;
32     const SkPixmap* tmp = &dst;
33     if (origin != kTopLeft_SkEncodedOrigin) {
34         auto info = dst.info();
35         if (SkEncodedOriginSwapsWidthHeight(origin)) {
36             info = SwapWidthHeight(info);
37         }
38         if (!storage.tryAlloc(info)) {
39             return false;
40         }
41         tmp = &storage;
42     }
43     if (!decode(*tmp)) {
44         return false;
45     }
46     if (tmp != &dst) {
47         return Orient(dst, *tmp, origin);
48     }
49     return true;
50 }
51 
52 }  // namespace SkPixmapUtils
53 
54 #endif // SkPixmapUtilsPriv_DEFINED
55