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 #include "include/codec/SkPixmapUtils.h"
9
10 #include "include/codec/SkEncodedOrigin.h"
11 #include "include/core/SkBitmap.h"
12 #include "include/core/SkBlendMode.h"
13 #include "include/core/SkCanvas.h"
14 #include "include/core/SkImage.h"
15 #include "include/core/SkMatrix.h"
16 #include "include/core/SkPaint.h"
17 #include "include/core/SkPixmap.h"
18 #include "include/core/SkRefCnt.h"
19 #include "include/core/SkSamplingOptions.h"
20 #include "include/core/SkSurface.h"
21
22 #include <utility>
23
draw_orientation(const SkPixmap & dst,const SkPixmap & src,SkEncodedOrigin origin)24 static bool draw_orientation(const SkPixmap& dst, const SkPixmap& src, SkEncodedOrigin origin) {
25 auto surf = SkSurfaces::WrapPixels(dst.info(), dst.writable_addr(), dst.rowBytes());
26 if (!surf) {
27 return false;
28 }
29
30 SkBitmap bm;
31 bm.installPixels(src);
32
33 SkMatrix m = SkEncodedOriginToMatrix(origin, dst.width(), dst.height());
34
35 SkPaint p;
36 p.setBlendMode(SkBlendMode::kSrc);
37 surf->getCanvas()->concat(m);
38 surf->getCanvas()->drawImage(SkImages::RasterFromBitmap(bm), 0, 0, SkSamplingOptions(), &p);
39 return true;
40 }
41
42 namespace SkPixmapUtils {
43
Orient(const SkPixmap & dst,const SkPixmap & src,SkEncodedOrigin origin)44 bool Orient(const SkPixmap& dst, const SkPixmap& src, SkEncodedOrigin origin) {
45 if (src.colorType() != dst.colorType()) {
46 return false;
47 }
48 // note: we just ignore alphaType and colorSpace for this transformation
49
50 int w = src.width();
51 int h = src.height();
52 if (SkEncodedOriginSwapsWidthHeight(origin)) {
53 using std::swap;
54 swap(w, h);
55 }
56 if (dst.width() != w || dst.height() != h) {
57 return false;
58 }
59 if (w == 0 || h == 0) {
60 return true;
61 }
62
63 // check for aliasing to self
64 if (src.addr() == dst.addr()) {
65 return kTopLeft_SkEncodedOrigin == origin;
66 }
67 return draw_orientation(dst, src, origin);
68 }
69
SwapWidthHeight(const SkImageInfo & info)70 SkImageInfo SwapWidthHeight(const SkImageInfo& info) {
71 return info.makeWH(info.height(), info.width());
72 }
73
74 }
75