OLD | NEW |
| (Empty) |
1 // Copyright 2010 The Native Client Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can | |
3 // be found in the LICENSE file. | |
4 | |
5 #ifndef C_SALT_IMAGE_H_ | |
6 #define C_SALT_IMAGE_H_ | |
7 | |
8 #include <stdint.h> | |
9 #include <boost/scoped_array.hpp> | |
10 | |
11 namespace c_salt { | |
12 // Image class is a container for ABGR-8888 pixels. Image files with other | |
13 // formats are converted when reading | |
14 class Image { | |
15 public: | |
16 // Ctor creates an empty, invalid surface. Use Resize() | |
17 // to fully initialize the Image. | |
18 Image(); | |
19 // Create deep copy of |other|. | |
20 Image(const Image& other); | |
21 | |
22 virtual ~Image(); | |
23 | |
24 // Create deep copy of |other|. | |
25 Image& operator=(const Image& other); | |
26 | |
27 // Change the size of the surface to (width, height). The minimum dimension | |
28 // is clamped to (1, 1). | |
29 void Resize(int width, int height); | |
30 | |
31 // Set all image pixels to the current background color. | |
32 void Erase(); | |
33 | |
34 // Load pixels from raw data. Only JPEG is supported for now. | |
35 bool InitWithData(const void* data, size_t data_length); | |
36 | |
37 int width() const {return width_;} | |
38 int height() const {return height_;} | |
39 | |
40 // A Image is valid if it has a dimension of at least (1, 1) and there is | |
41 // a pixel store allocated. | |
42 bool is_valid() const {return (size_ > 0) && (pixels_.get() != NULL);} | |
43 | |
44 void set_background_color(uint32_t color) {background_color_ = color;} | |
45 uint32_t background_color() const {return background_color_;} | |
46 | |
47 // Used to get the address of a pixel at location (|x|, |y|). | |
48 // Does not check bounds. | |
49 inline uint32_t* PixelAddress(int x, int y) { | |
50 return &pixels_[x + y * width_]; | |
51 } | |
52 | |
53 inline const uint32_t* PixelAddress(int x, int y) const { | |
54 return &pixels_[x + y * width_]; | |
55 } | |
56 | |
57 // Sets a pixel at a linear offset in memory. Does not check bounds or if | |
58 // the Image is valid. | |
59 void SetPixelAt(int i, uint32_t color) {pixels_[i] = color;} | |
60 | |
61 // Get the pixel value at a linear offset in memory. Does not check bounds | |
62 // or if the Image is valid. | |
63 uint32_t GetPixelAt(int i) const {return pixels_[i];} | |
64 | |
65 // Set the pixel at 2D address (|x|, |y|) to |color| without any bounds | |
66 // checking on the values of |x| and |y|. | |
67 void SetPixelNoClip(int x, int y, uint32_t color) { | |
68 SetPixelAt(y * width_ + x, color); | |
69 } | |
70 | |
71 // Retrieve the pixel value at 2D address (|x|, |y|) without any bounds | |
72 // checking on the values of |x| and |y|. | |
73 uint32_t GetPixelNoClip(int x, int y) const { | |
74 return GetPixelAt(y * width_ + x); | |
75 } | |
76 | |
77 // Set the pixel at 2D address (|x|, |y|) to |color| with bounds checking | |
78 // on the values of |x| and |y|. If either |x| or |y| are out of bounds, | |
79 // this method does nothing. | |
80 void SetPixel(int x, int y, uint32_t color) { | |
81 if (is_valid() && (x >= 0) && (x < width_) && (y >= 0) && (y < height_)) { | |
82 SetPixelAt(y * width_ + x, color); | |
83 } | |
84 } | |
85 | |
86 // Retrieve the pixel value at 2D address (|x|, |y|) with bounds checking | |
87 // on the values of |x| and |y|. If either |x| or |y| are out of bounds, | |
88 // return the background color. | |
89 uint32_t GetPixel(int x, int y) const { | |
90 if (!is_valid() || (x < 0) || (x >= width_) || (y < 0) || (y >= height_)) { | |
91 return background_color_; | |
92 } | |
93 return GetPixelAt(y * width_ + x); | |
94 } | |
95 | |
96 private: | |
97 int width_; | |
98 int height_; | |
99 size_t size_; | |
100 uint32_t background_color_; | |
101 boost::scoped_array<uint32_t> pixels_; | |
102 }; | |
103 } // namespace c_salt | |
104 | |
105 #endif // C_SALT_IMAGE_H_ | |
106 | |
OLD | NEW |