/
ofImage.h
245 lines (193 loc) · 8.02 KB
/
ofImage.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#pragma once
#include "ofFileUtils.h"
#include "ofTexture.h"
#include "ofPixels.h"
#include "ofBaseTypes.h"
#include "ofConstants.h"
enum ofImageQualityType {
OF_IMAGE_QUALITY_BEST,
OF_IMAGE_QUALITY_HIGH,
OF_IMAGE_QUALITY_MEDIUM,
OF_IMAGE_QUALITY_LOW,
OF_IMAGE_QUALITY_WORST
};
enum ofImageFormat {
OF_IMAGE_FORMAT_BMP = 0,
OF_IMAGE_FORMAT_ICO = 1,
OF_IMAGE_FORMAT_JPEG = 2,
OF_IMAGE_FORMAT_JNG = 3,
OF_IMAGE_FORMAT_KOALA = 4,
OF_IMAGE_FORMAT_LBM = 5,
OF_IMAGE_FORMAT_IFF = OF_IMAGE_FORMAT_LBM,
OF_IMAGE_FORMAT_MNG = 6,
OF_IMAGE_FORMAT_PBM = 7,
OF_IMAGE_FORMAT_PBMRAW = 8,
OF_IMAGE_FORMAT_PCD = 9,
OF_IMAGE_FORMAT_PCX = 10,
OF_IMAGE_FORMAT_PGM = 11,
OF_IMAGE_FORMAT_PGMRAW = 12,
OF_IMAGE_FORMAT_PNG = 13,
OF_IMAGE_FORMAT_PPM = 14,
OF_IMAGE_FORMAT_PPMRAW = 15,
OF_IMAGE_FORMAT_RAS = 16,
OF_IMAGE_FORMAT_TARGA = 17,
OF_IMAGE_FORMAT_TIFF = 18,
OF_IMAGE_FORMAT_WBMP = 19,
OF_IMAGE_FORMAT_PSD = 20,
OF_IMAGE_FORMAT_CUT = 21,
OF_IMAGE_FORMAT_XBM = 22,
OF_IMAGE_FORMAT_XPM = 23,
OF_IMAGE_FORMAT_DDS = 24,
OF_IMAGE_FORMAT_GIF = 25,
OF_IMAGE_FORMAT_HDR = 26,
OF_IMAGE_FORMAT_FAXG3 = 27,
OF_IMAGE_FORMAT_SGI = 28,
OF_IMAGE_FORMAT_EXR = 29,
OF_IMAGE_FORMAT_J2K = 30,
OF_IMAGE_FORMAT_JP2 = 31,
OF_IMAGE_FORMAT_PFM = 32,
OF_IMAGE_FORMAT_PICT = 33,
OF_IMAGE_FORMAT_RAW = 34
};
//----------------------------------------------------
// FreeImage based stuff:
bool ofLoadImage(ofPixels & pix, string path);
bool ofLoadImage(ofPixels & pix, const ofBuffer & buffer);
bool ofLoadImage(ofFloatPixels & pix, string path);
bool ofLoadImage(ofFloatPixels & pix, const ofBuffer & buffer);
bool ofLoadImage(ofShortPixels & pix, string path);
bool ofLoadImage(ofShortPixels & pix, const ofBuffer & buffer);
bool ofLoadImage(ofTexture & tex, string path);
bool ofLoadImage(ofTexture & tex, const ofBuffer & buffer);
void ofSaveImage(ofPixels & pix, string path, ofImageQualityType qualityLevel = OF_IMAGE_QUALITY_BEST);
void ofSaveImage(ofPixels & pix, ofBuffer & buffer, ofImageFormat format = OF_IMAGE_FORMAT_PNG, ofImageQualityType qualityLevel = OF_IMAGE_QUALITY_BEST);
void ofSaveImage(ofFloatPixels & pix, string path, ofImageQualityType qualityLevel = OF_IMAGE_QUALITY_BEST);
void ofSaveImage(ofFloatPixels & pix, ofBuffer & buffer, ofImageFormat format = OF_IMAGE_FORMAT_PNG, ofImageQualityType qualityLevel = OF_IMAGE_QUALITY_BEST);
void ofSaveImage(ofShortPixels & pix, string path, ofImageQualityType qualityLevel = OF_IMAGE_QUALITY_BEST);
void ofSaveImage(ofShortPixels & pix, ofBuffer & buffer, ofImageFormat format = OF_IMAGE_FORMAT_PNG, ofImageQualityType qualityLevel = OF_IMAGE_QUALITY_BEST);
// when we exit, we shut down ofImage
void ofCloseFreeImage();
//----------------------------------------------------
template<typename PixelType>
class ofImage_ : public ofBaseImage_<PixelType>{
public :
ofImage_();
virtual ~ofImage_();
ofImage_(const ofPixels_<PixelType> & pix);
ofImage_(const ofFile & file);
ofImage_(const string & filename);
// allocation / deallocation routines
void allocate(int w, int h, ofImageType type);
void clear();
// default copy overwriting (for = or std::vector)
ofImage_(const ofImage_<PixelType>& mom);
ofImage_<PixelType>& operator= (const ofImage_<PixelType>& mom);
template<typename SrcType>
ofImage_(const ofImage_<SrcType>& mom);
template<typename SrcType>
ofImage_<PixelType>& operator= (const ofImage_<SrcType>& mom);
// copying:
template<typename SrcType>
void clone(const ofImage_<SrcType> &mom);
// enable or disable using the texture of this image
void setUseTexture(bool bUse);
bool isUsingTexture();
// for getting a reference to the texture
ofTexture & getTextureReference();
// quick texture binding shortcut
void bind();
void unbind();
// file loading / saving
bool loadImage(string fileName);
bool loadImage(const ofBuffer & buffer);
bool loadImage(const ofFile & file);
void saveImage(string fileName, ofImageQualityType compressionLevel = OF_IMAGE_QUALITY_BEST);
void saveImage(ofBuffer & buffer, ofImageQualityType compressionLevel = OF_IMAGE_QUALITY_BEST);
void saveImage(const ofFile & file, ofImageQualityType compressionLevel = OF_IMAGE_QUALITY_BEST);
// Sosolimited: texture compression and mipmaps
void setCompression(ofTexCompression compression);
// getting the data
PixelType * getPixels(); // up to you to get this right
ofPixels_<PixelType> & getPixelsRef();
operator ofPixels_<PixelType>&();
ofColor_<PixelType> getColor(int x, int y) const;
// alter the image
void setColor(int x, int y, ofColor_<PixelType> color);
void setFromPixels(const PixelType * pixels, int w, int h, ofImageType type, bool bOrderIsRGB = true);
void setFromPixels(const ofPixels_<PixelType> & pixels);
void setImageType(ofImageType type);
void resize(int newWidth, int newHeight);
void grabScreen(int x, int y, int w, int h); // grab pixels from opengl, using glreadpixels
// this does an inplace crop.
// NOTE: this reallocates memory.
void crop(int x, int y, int w, int h);
// this does a crop from another image.
// NOTE: this will reallocate memory if the image types are different, or if the w & h do not
// equal this images w & h
void cropFrom(ofImage_<PixelType> & otherImage, int x, int y, int w, int h);
// perform rotation of 90 degress clockwise rotation amont times.
void rotate90(int rotation);
void mirror(bool vertical, bool horizontal);
// if you've altered the pixels (e.g., from getPixels())
// call update() to see a change (move the pixels to the texture)
void update();
// the anchor is the point the image is drawn around.
// this can be useful if you want to rotate an image around a particular point.
void setAnchorPercent(float xPct, float yPct); // set the anchor as a percentage of the image width/height ( 0.0-1.0 range )
void setAnchorPoint(float x, float y); // set the anchor point in pixels
void resetAnchor(); // resets the anchor to (0, 0)
// draw:
void draw(const ofRectangle & r);
void draw(const ofPoint & p, float w, float h);
void draw(float x, float y, float w, float h);
void draw(float x, float y, float z, float w, float h);
void draw(const ofPoint & p);
void draw(float x, float y);
void draw(float x, float y, float z);
float getHeight();
float getWidth();
bool bAllocated() {return pixels.isAllocated();}; // legacy function
bool isAllocated() {return pixels.isAllocated();};
void reloadTexture();
int width, height, bpp; // w,h, bits per pixel
int type; // OF_IMAGE_GRAYSCALE, OF_IMAGE_COLOR, OF_IMAGE_COLOR_ALPHA
ofImage_<PixelType> & operator=(ofPixels_<PixelType> & pixels);
protected:
void changeTypeOfPixels(ofPixels_<PixelType> &pix, ofImageType type);
void resizePixels(ofPixels_<PixelType> &pix, int newWidth, int newHeight);
ofPixels_<PixelType> pixels;
bool bUseTexture;
ofTexture tex;
};
typedef ofImage_<unsigned char> ofImage;
typedef ofImage_<float> ofFloatImage;
typedef ofImage_<unsigned short> ofShortImage;
//----------------------------------------------------------
template<typename PixelType>
template<typename SrcType>
ofImage_<PixelType>& ofImage_<PixelType>::operator=(const ofImage_<SrcType>& mom) {
clone(mom);
update();
return *this;
}
//----------------------------------------------------------
template<typename PixelType>
template<typename SrcType>
ofImage_<PixelType>::ofImage_(const ofImage_<SrcType>& mom) {
clear();
clone(mom);
update();
}
//------------------------------------
template<typename PixelType>
template<typename SrcType>
void ofImage_<PixelType>::clone(const ofImage_<SrcType> &mom){
ofImage_<SrcType> & nonConst = const_cast<ofImage_<SrcType> & >(mom);
pixels = nonConst.getPixelsRef();
tex.clear();
bUseTexture = nonConst.isUsingTexture();
if (bUseTexture == true){
tex.allocate(pixels.getWidth(), pixels.getHeight(), ofGetGlInternalFormat(pixels));
}
update();
}