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 SkPixmapUtils_DEFINED 9 #define SkPixmapUtils_DEFINED 10 11 #include "include/codec/SkEncodedOrigin.h" 12 #include "include/core/SkImageInfo.h" 13 #include "include/core/SkPixmap.h" 14 #include "src/core/SkAutoPixmapStorage.h" 15 16 class SkPixmapUtils { 17 public: 18 /** 19 * Copy the pixels in this pixmap into dst, applying the orientation transformations specified 20 * by the flags. If the inputs are invalid, this returns false and no copy is made. 21 */ 22 static bool Orient(const SkPixmap& dst, const SkPixmap& src, SkEncodedOrigin); 23 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 (SkEncodedOriginSwapsWidthHeight(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 59 }; 60 61 #endif // SkPixmapUtils_DEFINED 62