/*
Copyright (C) 1997, 1998, 1999, 2000 by Alex Pfaffe
(Digital Dawn Graphics Inc)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef _ddgImage_Class_
#define _ddgImage_Class_
#include "util/ddgerror.h"
#include "util/ddgutil.h"
class WEXP ddgImage {
unsigned char* _pixbuffer;
unsigned short _channels;
unsigned short _cols;
unsigned short _rows;
static unsigned int _maxval[5];
bool _readBWold(char *filename);
public:
ddgImage( unsigned short ch = 4)
: _pixbuffer(0), _channels(ch) {}
ddgImage( unsigned short r, unsigned short c, unsigned short ch = 4)
: _pixbuffer(0), _channels(ch) { allocate(r,c); }
~ddgImage();
unsigned char *buffer(void) { return _pixbuffer; }
unsigned short channels(void) { return _channels; }
void channels(unsigned short c ) { _channels = c; }
unsigned short cols(void) { return _cols; }
unsigned short rows(void) { return _rows; }
bool allocate( unsigned short r, unsigned short c, unsigned short ch = 0 );
void set(unsigned short r, unsigned short c,
unsigned char d1,
unsigned char d2 = 0,
unsigned char d3 = 0,
unsigned char d4 = 0)
{
switch (_channels)
{
case 4:
_pixbuffer[_channels*(r*_cols+c)+3] = d4;
case 3:
_pixbuffer[_channels*(r*_cols+c)+2] = d3;
case 2:
_pixbuffer[_channels*(r*_cols+c)+1] = d2;
case 1:
_pixbuffer[_channels*(r*_cols+c)+0] = d1;
break;
}
}
void get(unsigned short r, unsigned short c,
unsigned char *d1,
unsigned char *d2 = 0,
unsigned char *d3 = 0,
unsigned char *d4 = 0)
{
switch (_channels)
{
case 4:
*d4 = _pixbuffer[_channels*(r*_cols+c)+3];
case 3:
*d3 = _pixbuffer[_channels*(r*_cols+c)+2];
case 2:
*d2 = _pixbuffer[_channels*(r*_cols+c)+1];
case 1:
*d1 = _pixbuffer[_channels*(r*_cols+c)+0];
break;
}
}
unsigned char *get(unsigned short r, unsigned short c, unsigned char **p)
{
return *p = &_pixbuffer[_channels*(r*_cols+c)];
}
void copy(unsigned short sr, unsigned short sc,unsigned short dr, unsigned short dc)
{
unsigned int sb = _channels*(sr*_cols+sc),
db = _channels*(dr*_cols+dc);
switch (_channels)
{
case 4:
_pixbuffer[db+3] = _pixbuffer[sb+3];
case 3:
_pixbuffer[db+2] = _pixbuffer[sb+2];
case 2:
_pixbuffer[db+1] = _pixbuffer[sb+1];;
case 1:
_pixbuffer[db+0] = _pixbuffer[sb+0];;
break;
default:
ddgAsserts(0,"Invalid number of channels in image");
}
}
void import( ddgImage * isrc,
unsigned int sc, unsigned int sr,
unsigned int dc, unsigned int dr,
unsigned int nc, unsigned int nr)
{
unsigned char d1, d2, d3, d4;
for (unsigned int r = 0; r < nr; r++)
for (unsigned int c = 0; c < nc; c++)
{
// TODO: Optimize using memcpy.
isrc->get(sc+c,sr+r,&d1,&d2,&d3,&d4);
set(dc+c,dr+r,d1,d2,d3,d4);
}
}
bool readMemory( unsigned char *buf );
bool readFile( ddgStr *file );
bool readRGB( const char *filename);
bool readTGA(const char *filename);
bool readPNG(const char *filename);
bool readBW(const char *filename);
bool writeTGA(const char *filename);
bool writePNG(const char *filename);
bool mapColorToAlpha( unsigned char *color = NULL);
bool mapLuminanceToRGBA( void );
bool mapLuminanceToRGB( void );
bool mapBrightNessToAlpha( void );
};
#endif