1// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef SKIA_EXT_IMAGE_OPERATIONS_H_
6#define SKIA_EXT_IMAGE_OPERATIONS_H_
7
8#include "third_party/skia/include/core/SkBitmap.h"
9#include "third_party/skia/include/core/SkTypes.h"
10
11struct SkIRect;
12
13namespace skia {
14
15class SK_API ImageOperations {
16 public:
17  enum ResizeMethod {
18    //
19    // Quality Methods
20    //
21    // Those enumeration values express a desired quality/speed tradeoff.
22    // They are translated into an algorithm-specific method that depends
23    // on the capabilities (CPU, GPU) of the underlying platform.
24    // It is possible for all three methods to be mapped to the same
25    // algorithm on a given platform.
26
27    // Good quality resizing. Fastest resizing with acceptable visual quality.
28    // This is typically intended for use during interactive layouts
29    // where slower platforms may want to trade image quality for large
30    // increase in resizing performance.
31    //
32    // For example the resizing implementation may devolve to linear
33    // filtering if this enables GPU acceleration to be used.
34    //
35    // Note that the underlying resizing method may be determined
36    // on the fly based on the parameters for a given resize call.
37    // For example an implementation using a GPU-based linear filter
38    // in the common case may still use a higher-quality software-based
39    // filter in cases where using the GPU would actually be slower - due
40    // to too much latency - or impossible - due to image format or size
41    // constraints.
42    RESIZE_GOOD,
43
44    // Medium quality resizing. Close to high quality resizing (better
45    // than linear interpolation) with potentially some quality being
46    // traded-off for additional speed compared to RESIZE_BEST.
47    //
48    // This is intended, for example, for generation of large thumbnails
49    // (hundreds of pixels in each dimension) from large sources, where
50    // a linear filter would produce too many artifacts but where
51    // a RESIZE_HIGH might be too costly time-wise.
52    RESIZE_BETTER,
53
54    // High quality resizing. The algorithm is picked to favor image quality.
55    RESIZE_BEST,
56
57    //
58    // Algorithm-specific enumerations
59    //
60
61    // Box filter. This is a weighted average of all of the pixels touching
62    // the destination pixel. For enlargement, this is nearest neighbor.
63    //
64    // You probably don't want this, it is here for testing since it is easy to
65    // compute. Use RESIZE_LANCZOS3 instead.
66    RESIZE_BOX,
67
68    // 1-cycle Hamming filter. This is tall is the middle and falls off towards
69    // the window edges but without going to 0. This is about 40% faster than
70    // a 2-cycle Lanczos.
71    RESIZE_HAMMING1,
72
73    // 2-cycle Lanczos filter. This is tall in the middle, goes negative on
74    // each side, then returns to zero. Does not provide as good a frequency
75    // response as a 3-cycle Lanczos but is roughly 30% faster.
76    RESIZE_LANCZOS2,
77
78    // 3-cycle Lanczos filter. This is tall in the middle, goes negative on
79    // each side, then oscillates 2 more times. It gives nice sharp edges.
80    RESIZE_LANCZOS3,
81
82    // Lanczos filter + subpixel interpolation. If subpixel rendering is not
83    // appropriate we automatically fall back to Lanczos.
84    RESIZE_SUBPIXEL,
85
86    // enum aliases for first and last methods by algorithm or by quality.
87    RESIZE_FIRST_QUALITY_METHOD = RESIZE_GOOD,
88    RESIZE_LAST_QUALITY_METHOD = RESIZE_BEST,
89    RESIZE_FIRST_ALGORITHM_METHOD = RESIZE_BOX,
90    RESIZE_LAST_ALGORITHM_METHOD = RESIZE_SUBPIXEL,
91  };
92
93  // Resizes the given source bitmap using the specified resize method, so that
94  // the entire image is (dest_size) big. The dest_subset is the rectangle in
95  // this destination image that should actually be returned.
96  //
97  // The output image will be (dest_subset.width(), dest_subset.height()). This
98  // will save work if you do not need the entire bitmap.
99  //
100  // The destination subset must be smaller than the destination image.
101  static SkBitmap Resize(const SkBitmap& source,
102                         ResizeMethod method,
103                         int dest_width, int dest_height,
104                         const SkIRect& dest_subset,
105                         SkBitmap::Allocator* allocator = NULL);
106
107  // Alternate version for resizing and returning the entire bitmap rather than
108  // a subset.
109  static SkBitmap Resize(const SkBitmap& source,
110                         ResizeMethod method,
111                         int dest_width, int dest_height,
112                         SkBitmap::Allocator* allocator = NULL);
113
114 private:
115  ImageOperations();  // Class for scoping only.
116
117  // Supports all methods except RESIZE_SUBPIXEL.
118  static SkBitmap ResizeBasic(const SkBitmap& source,
119                              ResizeMethod method,
120                              int dest_width, int dest_height,
121                              const SkIRect& dest_subset,
122                              SkBitmap::Allocator* allocator = NULL);
123
124  // Subpixel renderer.
125  static SkBitmap ResizeSubpixel(const SkBitmap& source,
126                                 int dest_width, int dest_height,
127                                 const SkIRect& dest_subset,
128                                 SkBitmap::Allocator* allocator = NULL);
129};
130
131}  // namespace skia
132
133#endif  // SKIA_EXT_IMAGE_OPERATIONS_H_
134