reference

This documentation is automatically generated from the openFrameworks source code using doxygen and refers to the most recent release, version 0.12.0.

ofImage.h
Go to the documentation of this file.
1#pragma once
2
3#include "ofTexture.h"
4#include "ofGLBaseTypes.h"
6#include "ofGLUtils.h"
7#include "ofConstants.h"
8
9template<typename T>
10class ofPixels_;
14
15class ofFile;
16class ofBuffer;
17
27
44
84
85inline std::string ofImageFormatExtension(ofImageFormat format){
86 switch(format){
87 case OF_IMAGE_FORMAT_BMP: return "bmp";
88 case OF_IMAGE_FORMAT_ICO: return "ico";
89 case OF_IMAGE_FORMAT_JPEG: return "jpg";
90 case OF_IMAGE_FORMAT_JNG: return "jng";
91 case OF_IMAGE_FORMAT_KOALA: return "koala";
92 case OF_IMAGE_FORMAT_LBM: return "lbm";
93 case OF_IMAGE_FORMAT_MNG: return "mng";
94 case OF_IMAGE_FORMAT_PBM: return "pbm";
95 case OF_IMAGE_FORMAT_PBMRAW: return "pbm";
96 case OF_IMAGE_FORMAT_PCD: return "pcd";
97 case OF_IMAGE_FORMAT_PCX: return "pcx";
98 case OF_IMAGE_FORMAT_PGM: return "pgm";
99 case OF_IMAGE_FORMAT_PGMRAW: return "pgm";
100 case OF_IMAGE_FORMAT_PNG: return "png";
101 case OF_IMAGE_FORMAT_PPM: return "ppm";
102 case OF_IMAGE_FORMAT_PPMRAW: return "ppm";
103 case OF_IMAGE_FORMAT_RAS: return "ras";
104 case OF_IMAGE_FORMAT_TARGA: return "tga";
105 case OF_IMAGE_FORMAT_TIFF: return "tif";
106 case OF_IMAGE_FORMAT_WBMP: return "wbmp";
107 case OF_IMAGE_FORMAT_PSD: return "psd";
108 case OF_IMAGE_FORMAT_CUT: return "cut";
109 case OF_IMAGE_FORMAT_XBM: return "xbm";
110 case OF_IMAGE_FORMAT_XPM: return "xpm";
111 case OF_IMAGE_FORMAT_DDS: return "dds";
112 case OF_IMAGE_FORMAT_GIF: return "gif";
113 case OF_IMAGE_FORMAT_HDR: return "hdr";
114 case OF_IMAGE_FORMAT_FAXG3: return "faxg3";
115 case OF_IMAGE_FORMAT_SGI: return "sgi";
116 case OF_IMAGE_FORMAT_EXR: return "exr";
117 case OF_IMAGE_FORMAT_J2K: return "j2k";
118 case OF_IMAGE_FORMAT_JP2: return "jp2";
119 case OF_IMAGE_FORMAT_PFM: return "pfm";
120 case OF_IMAGE_FORMAT_PICT: return "pict";
121 case OF_IMAGE_FORMAT_RAW: return "raw";
122 default: return "unkown";
123 }
124}
125
126
129 bool accurate = false;
130 bool exifRotate = false;
131 bool grayscale = false;
132 bool separateCMYK = false;
134};
135
136//----------------------------------------------------
137// FreeImage based stuff
138
139
141bool ofLoadImage(ofPixels & pix, const of::filesystem::path& path, const ofImageLoadSettings &settings = ofImageLoadSettings());
142bool ofLoadImage(ofPixels & pix, const ofBuffer & buffer, const ofImageLoadSettings &settings = ofImageLoadSettings());
143bool ofLoadImage(ofFloatPixels & pix, const of::filesystem::path& path, const ofImageLoadSettings &settings = ofImageLoadSettings());
144bool ofLoadImage(ofFloatPixels & pix, const ofBuffer & buffer, const ofImageLoadSettings &settings = ofImageLoadSettings());
145bool ofLoadImage(ofShortPixels & pix, const of::filesystem::path& path, const ofImageLoadSettings &settings = ofImageLoadSettings());
146bool ofLoadImage(ofShortPixels & pix, const ofBuffer & buffer, const ofImageLoadSettings &settings = ofImageLoadSettings());
147
149bool ofLoadImage(ofTexture & tex, const of::filesystem::path& path, const ofImageLoadSettings &settings = ofImageLoadSettings());
150bool ofLoadImage(ofTexture & tex, const ofBuffer & buffer, const ofImageLoadSettings &settings = ofImageLoadSettings());
151
153bool ofSaveImage(const ofPixels & pix, const of::filesystem::path& path, ofImageQualityType qualityLevel = OF_IMAGE_QUALITY_BEST);
155
157bool ofSaveImage(const ofFloatPixels & pix, const of::filesystem::path& path, ofImageQualityType qualityLevel = OF_IMAGE_QUALITY_BEST);
159
161bool ofSaveImage(const ofShortPixels & pix, const of::filesystem::path& path, ofImageQualityType qualityLevel = OF_IMAGE_QUALITY_BEST);
163
167void ofCloseFreeImage();
168
171template<typename PixelType>
172class ofImage_ : public ofBaseImage_<PixelType>{
173public:
176
177 ofImage_();
178
179 ofImage_(const ofPixels_<PixelType> & pix);
180 ofImage_(const of::filesystem::path & fileName, const ofImageLoadSettings &settings = ofImageLoadSettings());
181 ofImage_(const ofImage_<PixelType>& mom);
183
184 template<typename SrcType>
186
187
203 void allocate(int w, int h, ofImageType type);
204
208 bool isAllocated() const {return pixels.isAllocated();}
209
214 OF_DEPRECATED_MSG("Use isAllocated()", bool bAllocated());
215
217 void clear();
218
224 bool load(const of::filesystem::path& fileName, const ofImageLoadSettings &settings = ofImageLoadSettings());
225
231 bool load(const ofBuffer & buffer, const ofImageLoadSettings &settings = ofImageLoadSettings());
232
233 OF_DEPRECATED_MSG("Use load instead",bool loadImage(const std::string& fileName));
234 OF_DEPRECATED_MSG("Use load instead",bool loadImage(const ofBuffer & buffer));
235 OF_DEPRECATED_MSG("Use load instead",bool loadImage(const ofFile & file));
236
237 virtual ~ofImage_();
238
242
243 using ofBaseDraws::draw;
244
249 void draw(float x, float y) const;
250
256 void draw(float x, float y, float z) const;
257
258 void draw(const glm::vec3 & pos) const;
259
266 void draw(float x, float y, float w, float h) const;
267
275 void draw(float x, float y, float z, float w, float h) const;
276
277 void draw(const glm::vec3 & pos, float w, float h) const;
278
290 void drawSubsection(float x, float y, float w, float h, float sx, float sy) const;
291
304 void drawSubsection(float x, float y, float z, float w, float h, float sx, float sy) const;
305
319 void drawSubsection(float x, float y, float w, float h, float sx, float sy, float sw, float sh) const;
320
335 void drawSubsection(float x, float y, float z, float w, float h, float sx, float sy, float sw, float sh) const;
336
340
347 void update();
348
352 void setUseTexture(bool bUse);
353
358 bool isUsingTexture() const;
359
368
371 const ofTexture & getTexture() const;
372
373 OF_DEPRECATED_MSG("Use getTexture",ofTexture & getTextureReference());
374 OF_DEPRECATED_MSG("Use getTexture",const ofTexture & getTextureReference() const);
375
378 void bind(int textureLocation=0) const;
379
383 void unbind(int textureLocation=0) const;
384
388 void setCompression(ofTexCompression compression);
389
390
394
408 const ofPixels_<PixelType> & getPixels() const;
409
416 OF_DEPRECATED_MSG("Use getPixels() instead ", ofPixels_<PixelType> & getPixelsRef());
417 OF_DEPRECATED_MSG("Use getPixels() instead ", const ofPixels_<PixelType> & getPixelsRef() const);
418
419 operator ofPixels_<PixelType>&();
420
426 ofColor_<PixelType> getColor(int x, int y) const;
427
432 ofColor_<PixelType> getColor(int index) const;
433
436 float getHeight() const;
437
440 float getWidth() const;
441
445
454 void setColor(int x, int y, const ofColor_<PixelType>& color);
455
460 void setColor(int index, const ofColor_<PixelType>& color);
461
464 void setColor(const ofColor_<PixelType>& color);
465
481 void setFromPixels(const PixelType * pixels, int w, int h, ofImageType type, bool bOrderIsRGB = true);
482
488 void setFromPixels(const ofPixels_<PixelType> & pixels);
489
501 void grabScreen(int x, int y, int w, int h);
502
513 void setImageType(ofImageType type);
515
519
525 void resize(int newWidth, int newHeight);
526
535 void crop(int x, int y, int w, int h);
536
547 void cropFrom(const ofImage_<PixelType>& otherImage, int x, int y, int w, int h);
548
552 void rotate90(int rotation);
553
557 void mirror(bool vertical, bool horizontal);
558
562
576 void setAnchorPercent(float xPct, float yPct);
577
587 void setAnchorPoint(float x, float y);
588
593 void resetAnchor();
594
596
599
600
606 bool save(const of::filesystem::path & fileName, ofImageQualityType compressionLevel = OF_IMAGE_QUALITY_BEST) const;
607
613 bool save(ofBuffer & buffer, ofImageFormat imageFormat = OF_IMAGE_FORMAT_PNG, ofImageQualityType compressionLevel = OF_IMAGE_QUALITY_BEST) const;
614
615 OF_DEPRECATED_MSG("Use save instead",void saveImage(const std::string& fileName, ofImageQualityType compressionLevel = OF_IMAGE_QUALITY_BEST) const);
616 OF_DEPRECATED_MSG("Use save instead",void saveImage(ofBuffer & buffer, ofImageQualityType compressionLevel = OF_IMAGE_QUALITY_BEST) const);
617 OF_DEPRECATED_MSG("Use save instead",void saveImage(const ofFile & file, ofImageQualityType compressionLevel = OF_IMAGE_QUALITY_BEST) const);
618
622
624
625 // default copy overwriting (for = or std::vector)
627
628 template<typename SrcType>
630
631 //move assignment
633
636
637protected:
639 void changeTypeOfPixels(ofPixels_<PixelType> &pix, ofImageType type);
640 void resizePixels(ofPixels_<PixelType> &pix, int newWidth, int newHeight);
641 void unloadTexture();
642
644 bool bUseTexture;
645 ofTexture tex;
646
647 int width;
648 int height;
649 int bpp;
650 ofImageType type;
651
652 template<typename SrcType>
653 void clone(const ofImage_<SrcType> &mom);
655};
656
663
664
665
666//----------------------------------------------------------
667template<typename PixelType>
668template<typename SrcType>
670 clone(mom);
671 return *this;
672}
673
674//----------------------------------------------------------
675template<typename PixelType>
676template<typename SrcType>
678 clear();
679 clone(mom);
680}
681
682//------------------------------------
683template<typename PixelType>
684template<typename SrcType>
686 pixels = mom.getPixels();
687
688 tex.clear();
689 bUseTexture = mom.isUsingTexture();
690 if (bUseTexture == true && mom.getTexture().isAllocated()){
691 tex.allocate(pixels.getWidth(), pixels.getHeight(), ofGetGLInternalFormat(pixels));
692 }
693
694 update();
695}
696
697
698template<typename T>
699std::string ofToString(const T & v);
700template<>
701std::string ofToString(const ofImageType & imgType);
virtual void draw(float x, float y) const
Draw at a position at the native size.
Definition ofGraphicsBaseTypes.h:150
A base class represeting an image.
Definition ofGLBaseTypes.h:76
Definition ofFileUtils.h:15
ofColor represents a color in openFrameworks.
Definition ofColor.h:20
Definition ofFileUtils.h:472
A class representing an image using memory and gpu based pixels.
Definition ofImage.h:172
virtual ~ofImage_()
Definition ofImage.cpp:693
void crop(int x, int y, int w, int h)
This crops the image to the w,h passed in from the x,y position.
Definition ofImage.cpp:1127
void drawSubsection(float x, float y, float w, float h, float sx, float sy) const
Draws a subsection of the image.
Definition ofImage.cpp:840
void rotate90(int rotation)
Rotates the image by a multiple of 90 degrees.
Definition ofImage.cpp:1147
bool isAllocated() const
Whether the image has been allocated either by a call to allocate or by loading pixel data into the i...
Definition ofImage.h:208
float getWidth() const
Get width of image as a float.
Definition ofImage.cpp:1250
ofImage_()
Definition ofImage.cpp:579
bool save(const of::filesystem::path &fileName, ofImageQualityType compressionLevel=OF_IMAGE_QUALITY_BEST) const
Saves the image to the file path in fileName with the image quality specified by compressionLevel.
Definition ofImage.cpp:751
OF_DEPRECATED_MSG("Use getPixels() instead ", const ofPixels_< PixelType > &getPixelsRef() const)
OF_DEPRECATED_MSG("Use getTexture", const ofTexture &getTextureReference() const)
void setColor(int x, int y, const ofColor_< PixelType > &color)
Sets the pixel at the x,y position passed in.
Definition ofImage.cpp:993
void resetAnchor()
Removes anchor positioning.
Definition ofImage.cpp:796
bool isUsingTexture() const
Returns whether the ofImage has a texture or not.
Definition ofImage.cpp:1061
void cropFrom(const ofImage_< PixelType > &otherImage, int x, int y, int w, int h)
Replaces region in caller image specified by w,h,x,y with pixels from otherImage.
Definition ofImage.cpp:1137
OF_DEPRECATED_MSG("Use load instead", bool loadImage(const std::string &fileName))
OF_DEPRECATED_MSG("Use save instead", void saveImage(const std::string &fileName, ofImageQualityType compressionLevel=OF_IMAGE_QUALITY_BEST) const)
void clear()
This clears the texture and pixels contained within the ofImage.
Definition ofImage.cpp:895
ofColor_< PixelType > getColor(int x, int y) const
This returns the ofColor representing the pixels at the x and y position passed in.
Definition ofImage.cpp:981
ofImage_(const ofImage_< SrcType > &mom)
Definition ofImage.h:677
void setFromPixels(const PixelType *pixels, int w, int h, ofImageType type, bool bOrderIsRGB=true)
Sets the pixels of the image from an array of values.
Definition ofImage.cpp:1011
void unbind(int textureLocation=0) const
Unbinds the ofTexture instance that the ofImage contains.
Definition ofImage.cpp:974
void draw(float x, float y) const
Draw the image at it's normal size.
Definition ofImage.cpp:802
void mirror(bool vertical, bool horizontal)
This reflects the pixels of the image across the vertical and/or horizontal axis.
Definition ofImage.cpp:1154
ofPixels_< PixelType > & getPixels()
Returns a raw pointer to the pixel data.
Definition ofImage.cpp:912
float getHeight() const
Get height of image as a float.
Definition ofImage.cpp:1244
void allocate(int w, int h, ofImageType type)
Definition ofImage.cpp:864
ofImage_< PixelType > & operator=(ofPixels_< PixelType > &pixels)
Definition ofImage.cpp:1031
OF_DEPRECATED_MSG("Use load instead", bool loadImage(const ofBuffer &buffer))
void setAnchorPercent(float xPct, float yPct)
Change the drawing anchor from top-left corner to a position specified by xPct and yPct.
Definition ofImage.cpp:784
OF_DEPRECATED_MSG("Use getPixels() instead ", ofPixels_< PixelType > &getPixelsRef())
This returns an ofPixels reference that you can use to manipulate the raw pixel data of the ofImage.
void setUseTexture(bool bUse)
Turns on or off the allocation and use of a texture.
Definition ofImage.cpp:1055
OF_DEPRECATED_MSG("Use getTexture", ofTexture &getTextureReference())
OF_DEPRECATED_MSG("Use isAllocated()", bool bAllocated())
Whether the image has been allocated either by a call to allocate or by loading pixel data into the i...
void resize(int newWidth, int newHeight)
Resizes the image to a new size (w, h); Can be used to scale up or down an image.
Definition ofImage.cpp:1117
OF_DEPRECATED_MSG("Use save instead", void saveImage(const ofFile &file, ofImageQualityType compressionLevel=OF_IMAGE_QUALITY_BEST) const)
ofTexture & getTexture()
Returns a reference to the texture that the ofImage contains.
Definition ofImage.cpp:942
void bind(int textureLocation=0) const
Binds the oftexture instance that the ofImage contains so that it can be used for advanced drawing.
Definition ofImage.cpp:967
void setImageType(ofImageType type)
Set type of image to one of the following: OF_IMAGE_GRAYSCALE, OF_IMAGE_COLOR, OF_IMAGE_COLOR_ALPHA.
Definition ofImage.cpp:1104
void setCompression(ofTexCompression compression)
This sets the compression level used when creating mipmaps for the ofTexture contained by the ofImage...
Definition ofImage.cpp:1258
ofImageType getImageType() const
Definition ofImage.cpp:1111
void update()
Call to ensure that changes to pixels are reflected in the ofTexture of the image.
Definition ofImage.cpp:1038
void grabScreen(int x, int y, int w, int h)
Grabs pixels from the opengl window specified by the region (x, y, w, h) and turns them into an image...
Definition ofImage.cpp:1097
OF_DEPRECATED_MSG("Use save instead", void saveImage(ofBuffer &buffer, ofImageQualityType compressionLevel=OF_IMAGE_QUALITY_BEST) const)
bool load(const of::filesystem::path &fileName, const ofImageLoadSettings &settings=ofImageLoadSettings())
Loads an image given by fileName.
Definition ofImage.cpp:705
OF_DEPRECATED_MSG("Use load instead", bool loadImage(const ofFile &file))
void setAnchorPoint(float x, float y)
Changes drawing position from top-left corner to position specified by x,y.
Definition ofImage.cpp:790
A class representing a collection of pixels.
Definition ofPixels.h:170
A wrapper class for an OpenGL texture.
Definition ofTexture.h:253
virtual void allocate(const ofTextureData &textureData)
Allocate the texture using the given settings.
Definition ofTexture.cpp:408
void clear()
Clears the texture.
Definition ofTexture.cpp:289
bool isAllocated() const
Determine whether the texture has been allocated.
Definition ofTexture.cpp:256
unsigned int bpp
Definition ofAppEGLWindow.cpp:126
unsigned int height
Definition ofAppEGLWindow.cpp:125
unsigned int width
Definition ofAppEGLWindow.cpp:124
int ofGetGLInternalFormat(const ofPixels &pixels)
Definition ofGLUtils.cpp:53
ofImageType
Used to represent the available channel types in ofImage.
Definition ofGraphicsConstants.h:154
ofPixels_< float > ofFloatPixels
Definition ofImage.h:12
void ofCloseFreeImage()
Deallocates FreeImage resources.
Definition ofImage.cpp:570
bool ofSaveImage(const ofPixels &pix, const of::filesystem::path &path, ofImageQualityType qualityLevel=OF_IMAGE_QUALITY_BEST)
Definition ofImage.cpp:440
bool ofLoadImage(ofPixels &pix, const of::filesystem::path &path, const ofImageLoadSettings &settings=ofImageLoadSettings())
Definition ofImage.cpp:302
ofPixels_< unsigned char > ofPixels
Definition ofImage.h:11
ofImage_< float > ofFloatImage
Definition ofImage.h:660
ofImageFormat
Definition ofImage.h:46
@ OF_IMAGE_FORMAT_CUT
Definition ofImage.h:69
@ OF_IMAGE_FORMAT_GIF
Definition ofImage.h:73
@ OF_IMAGE_FORMAT_PGMRAW
Definition ofImage.h:60
@ OF_IMAGE_FORMAT_XBM
Definition ofImage.h:70
@ OF_IMAGE_FORMAT_JPEG
Definition ofImage.h:49
@ OF_IMAGE_FORMAT_IFF
Definition ofImage.h:53
@ OF_IMAGE_FORMAT_BMP
Definition ofImage.h:47
@ OF_IMAGE_FORMAT_HDR
Definition ofImage.h:74
@ OF_IMAGE_FORMAT_PFM
Definition ofImage.h:80
@ OF_IMAGE_FORMAT_PBMRAW
Definition ofImage.h:56
@ OF_IMAGE_FORMAT_PCD
Definition ofImage.h:57
@ OF_IMAGE_FORMAT_LBM
Definition ofImage.h:52
@ OF_IMAGE_FORMAT_JP2
Definition ofImage.h:79
@ OF_IMAGE_FORMAT_JNG
Definition ofImage.h:50
@ OF_IMAGE_FORMAT_FAXG3
Definition ofImage.h:75
@ OF_IMAGE_FORMAT_KOALA
Definition ofImage.h:51
@ OF_IMAGE_FORMAT_PCX
Definition ofImage.h:58
@ OF_IMAGE_FORMAT_MNG
Definition ofImage.h:54
@ OF_IMAGE_FORMAT_PICT
Definition ofImage.h:81
@ OF_IMAGE_FORMAT_TARGA
Definition ofImage.h:65
@ OF_IMAGE_FORMAT_RAS
Definition ofImage.h:64
@ OF_IMAGE_FORMAT_XPM
Definition ofImage.h:71
@ OF_IMAGE_FORMAT_EXR
Definition ofImage.h:77
@ OF_IMAGE_FORMAT_ICO
Definition ofImage.h:48
@ OF_IMAGE_FORMAT_PGM
Definition ofImage.h:59
@ OF_IMAGE_FORMAT_DDS
Definition ofImage.h:72
@ OF_IMAGE_FORMAT_PPMRAW
Definition ofImage.h:63
@ OF_IMAGE_FORMAT_J2K
Definition ofImage.h:78
@ OF_IMAGE_FORMAT_PBM
Definition ofImage.h:55
@ OF_IMAGE_FORMAT_SGI
Definition ofImage.h:76
@ OF_IMAGE_FORMAT_TIFF
Definition ofImage.h:66
@ OF_IMAGE_FORMAT_RAW
Definition ofImage.h:82
@ OF_IMAGE_FORMAT_WBMP
Definition ofImage.h:67
@ OF_IMAGE_FORMAT_PNG
Definition ofImage.h:61
@ OF_IMAGE_FORMAT_PSD
Definition ofImage.h:68
@ OF_IMAGE_FORMAT_PPM
Definition ofImage.h:62
std::string ofToString(const T &v)
Convert a value to a string.
Definition ofUtils.h:657
ofImageQualityType
Used for controlling the save quality of JPEGs.
Definition ofImage.h:32
@ OF_IMAGE_QUALITY_HIGH
Equivalent to FreeImage's JPEG_QUALITYGOOD (75:1 ratio)
Definition ofImage.h:36
@ OF_IMAGE_QUALITY_BEST
Equivalent to FreeImage's JPEG_QUALITYSUPERB (100:1 ratio)
Definition ofImage.h:34
@ OF_IMAGE_QUALITY_MEDIUM
Equivalent to FreeImage's JPEG_QUALITYNORMAL (50:1 ratio)
Definition ofImage.h:38
@ OF_IMAGE_QUALITY_LOW
Equivalent to FreeImage's JPEG_QUALITYAVERAGE (25:1 ratio)
Definition ofImage.h:40
@ OF_IMAGE_QUALITY_WORST
Equivalent to FreeImage's JPEG_QUALITYBAD (10:1 ratio)
Definition ofImage.h:42
ofPixels_< unsigned short > ofShortPixels
Definition ofImage.h:13
ofImage_< unsigned short > ofShortImage
Definition ofImage.h:661
std::string ofImageFormatExtension(ofImageFormat format)
Definition ofImage.h:85
ofImage_< unsigned char > ofImage
Definition ofImage.h:659
ofTexCompression
Texture compression types.
Definition ofTexture.h:150
Definition ofImage.h:128
bool accurate
Definition ofImage.h:129
bool separateCMYK
Definition ofImage.h:132
bool grayscale
Definition ofImage.h:131
bool exifRotate
Definition ofImage.h:130
int freeImageFlags
Definition ofImage.h:133