21 #ifndef GR_IMAGE_DATA__H
22 #define GR_IMAGE_DATA__H
34 class ImageDataWrapper ;
35 class ImageDataWriter ;
49 G_EXCEPTION( Error ,
"image data error" ) ;
51 { Segmented , Contiguous } ;
96 void resize(
int dx ,
int dy ,
int channels ) ;
99 unsigned char r(
int x ,
int y )
const ;
102 unsigned char g(
int x ,
int y )
const ;
105 unsigned char b(
int x ,
int y )
const ;
108 unsigned int rgb(
int x ,
int y )
const ;
111 void rgb(
int x ,
int y ,
unsigned char r ,
unsigned char g ,
unsigned char b ) ;
115 void fill(
unsigned char r ,
unsigned char g ,
unsigned char b ) ;
119 bool draw_background ,
bool wrap_on_nl ) ;
123 bool draw_background ) ;
126 unsigned char *
row(
int y ) ;
129 const unsigned char *
row(
int y )
const ;
135 const unsigned char *
const *
rowPointers()
const ;
141 const unsigned char *
p()
const ;
145 unsigned char *
p() ;
149 void copyRowIn(
int y ,
const unsigned char * row_buffer_in ,
size_t row_buffer_in_size ,
int channels_in ,
150 bool use_colourspace =
false ,
int scale = 1 ) ;
158 void copyIn(
const char * data_in ,
size_t data_size_in ,
int dx_in ,
int dy_in ,
int channels_in ,
159 bool use_colourspace =
false ,
int scale = 1 ) ;
169 void copyIn(
const ImageBuffer & data_in ,
int dx_in ,
int dy_in ,
int channels_in ,
170 bool use_colourspace =
false ,
int scale = 1 ) ;
174 void copyRowOut(
int y , std::vector<char> & out ,
int scale = 1 ,
bool monochrome_out =
false ,
bool use_colourspace =
true )
const ;
178 void copyTo( std::vector<char> & out )
const ;
186 void scale(
int factor ,
bool monochrome ,
bool use_colourspace ) ;
189 void dim(
unsigned int shift ) ;
192 void dim(
unsigned int numerator ,
unsigned int denominator ) ;
196 void mix(
const ImageData & _1 ,
const ImageData & _2 ,
unsigned int numerator_1 ,
unsigned int numerator_2 ,
unsigned int denominator ) ;
208 void crop(
int dx ,
int dy ) ;
212 void expand(
int dx ,
int dy ) ;
220 void copyRowInImp(
unsigned char * ,
const unsigned char * ,
int ,
bool ,
int ) ;
221 unsigned char * storerow(
int y ) ;
222 const unsigned char * storerow(
int y )
const ;
223 void setRows()
const ;
225 bool contiguous()
const ;
226 template <
typename Fn>
void modify( Fn fn ) ;
227 template <
typename Fn>
void modify(
const Gr::ImageData & other , Fn fn ) ;
237 mutable bool m_rows_set ;
238 mutable std::vector<unsigned char*> m_rows ;
260 const unsigned char *
row(
int y )
const ;
263 unsigned char r(
int x ,
int y )
const ;
266 unsigned char g(
int x ,
int y )
const ;
269 unsigned char b(
int x ,
int y )
const ;
272 unsigned int rgb(
int x ,
int y )
const ;
291 bool draw_background ,
bool wrap_on_nl ) ;
294 template <
typename T>
void write( T p , T end ) ;
310 bool m_draw_background ;
314 template <
typename T>
317 for( ; p != end ; ++p )
321 m_data->
write( *p , m_x , m_y , m_foreground , m_background , m_draw_background ) ;
347 unsigned char * Gr::ImageData::storerow(
int y )
349 return reinterpret_cast<unsigned char*
>( &(m_data[y])[0] ) ;
353 const unsigned char * Gr::ImageData::storerow(
int y )
const
355 return reinterpret_cast<const unsigned char*
>( &(m_data[y])[0] ) ;
362 m_type == Contiguous ?
363 ( storerow(0) + sizet(y,m_dx,m_channels) ) :
371 m_type == Contiguous ?
372 ( storerow(0) + sizet(y,m_dx,m_channels) ) :
379 int x_offset = m_channels == 1 ? x : (x*3) ;
380 unsigned char * out_p = row(y) + x_offset ;
382 if( m_channels != 1 )
392 const unsigned char * row_p = m_rows_set ? m_rows[y] : row(y) ;
393 return m_channels == 1 ? row_p[x] : row_p[x*3] ;
399 const unsigned char * row_p = m_rows_set ? m_rows[y] : row(y) ;
400 return m_channels == 1 ? row_p[x] : row_p[x*3+1] ;
406 const unsigned char * row_p = m_rows_set ? m_rows[y] : row(y) ;
407 return m_channels == 1 ? row_p[x] : row_p[x*3+2] ;
414 ( static_cast<unsigned int>(r(x,y)) << 16 ) |
415 (
static_cast<unsigned int>(g(x,y)) << 8 ) |
416 (
static_cast<unsigned int>(r(x,y)) << 0 ) ;
422 m_image_buffer(image_buffer) ,
427 if( dx <= 0 || dy <= 0 || (channels != 1 && channels != 3) ||
428 ( image_buffer.size() != 1U && image_buffer.size() != sizet(dy) ) ||
429 imagebuffer::size_of(image_buffer) != sizet(dx,dy,channels) )
430 throw ImageData::Error() ;
454 if( m_image_buffer.size() == 1U && y > 0 && y < m_dy )
455 return reinterpret_cast<const unsigned char*>(&(m_image_buffer.at(0))[0]) + sizet(y,m_dx,m_channels) ;
457 return reinterpret_cast<const unsigned char*
>(&(m_image_buffer.at(y))[0]) ;
463 const unsigned char * row_p = row( y ) ;
464 return m_channels == 1 ? row_p[x] : row_p[x*3] ;
470 const unsigned char * row_p = row( y ) ;
471 return m_channels == 1 ? row_p[x] : row_p[x*3+1] ;
477 const unsigned char * row_p = row( y ) ;
478 return m_channels == 1 ? row_p[x] : row_p[x*3+2] ;
485 ( static_cast<unsigned int>(r(x,y)) << 16 ) |
486 (
static_cast<unsigned int>(g(x,y)) << 8 ) |
487 (
static_cast<unsigned int>(r(x,y)) << 0 ) ;
unsigned char b(int x, int y) const
Returns the B-value for a point. Returns the R-value if there is only one channel.
size_t rowsize() const
Returns the row size, ie. dx()*channels().
int dx() const
Returns the width.
A holder for image data, having eight bits per sample and one or three channels.
void mix(const ImageData &_1, const ImageData &_2, unsigned int numerator_1, unsigned int numerator_2, unsigned int denominator)
Creates a mixed image from two equally-shaped sources images.
void write(T p, T end)
Calls ImageData::write() for each character in the range.
size_t size() const
Returns the total image size, ie. dx()*dy()*channels().
void resize(int dx, int dy, int channels)
Resizes the image data.
unsigned char r(int x, int y) const
Returns the R-value for a point.
void expand(int dx, int dy)
Expands the image so that the new image has the dimensions given, with the original image centered in...
Type type() const
Returns the contiguous/segmented enumeration.
unsigned char * row(int y)
Returns a pointer to the y'th row.
const unsigned char * row(int y) const
Returns a pointer to the y-th row.
void scale(int factor, bool monochrome, bool use_colourspace)
Scales-down the image by sub-sampling.
A simple rgb colour structure.
Vectors ImageBuffer
An ImageBuffer is used to hold raw image data, typically in more than one chunk.
unsigned char r(int x, int y) const
Returns the R-value for a point.
bool empty() const
Returns true if the size() is zero.
unsigned char g(int x, int y) const
Returns the G-value for a point. Returns the R-value if there is only one channel.
unsigned char g(int x, int y) const
Returns the G-value for a point. Returns the R-value if there is only one channel.
void subtract(const ImageData &other)
Subtracts the given image data from this.
unsigned int rgb(int x, int y) const
Returns the colour of a pixel as rgb values packed into one integer.
unsigned char ** rowPointers()
Returns a pointer to the array of row pointers.
void copyTo(std::vector< char > &out) const
Copies the image to the given output buffer, resizing the output vector as necessary.
int channels() const
Returns the number of channels.
unsigned char b(int x, int y) const
Returns the B-value for a point. Returns the R-value if there is only one channel.
int dy() const
Returns the height.
ImageDataWriter(Gr::ImageData &data, int x, int y, Gr::Colour foreground, Gr::Colour background, bool draw_background, bool wrap_on_nl)
Constructor.
void copyIn(const char *data_in, size_t data_size_in, int dx_in, int dy_in, int channels_in, bool use_colourspace=false, int scale=1)
Copies the image in from a raw buffer, with channel-count adjustment and optional scaling...
int channels() const
Returns the number of channels (zero, one or three).
void copyRowIn(int y, const unsigned char *row_buffer_in, size_t row_buffer_in_size, int channels_in, bool use_colourspace=false, int scale=1)
Sets a row of pixels by copying, with channel-count adjustment and optional scaling.
A class that wraps a read-only image buffer with a few methods that are similar to those of Gr::Image...
void dim(unsigned int shift)
Dims the image by right-shifting all pixel values.
const unsigned char * p() const
Returns a const pointer to the image data, but throws if the data is not contiguous.
unsigned int rgb(int x, int y) const
Returns the colour of a pixel as rgb values packed into one integer.
ImageData(Type=Segmented)
Default constructor for a zero-size image.
void fill(unsigned char r, unsigned char g, unsigned char b)
Fills the image with a solid colour as if calling rgb() for every pixel.
ImageDataWriter writer(int x, int y, Colour foreground, Colour background, bool draw_background, bool wrap_on_nl)
Returns a functor that calls write().
void crop(int dx, int dy)
Crops the image so that it fits inside the given dimensions.
void copyRowOut(int y, std::vector< char > &out, int scale=1, bool monochrome_out=false, bool use_colourspace=true) const
Copies a row into the given output buffer, resizing the output vector as necessary.
ImageDataWrapper(const ImageBuffer &, int dx, int dy, int channels)
Constructor.
void add(const ImageData &other)
Adds the given image data to this.
int dy() const
Returns the height.
int dx() const
Returns the width.
void write(char c, int x, int y, Colour foreground, Colour background, bool draw_background)
Draws a latin-1 character into the image at the given position.