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