33 #define imRef(im, x, y) (im->access[y][x]) 36 #define imPtr(im, x, y) &(im->access[y][x]) 44 return ((a.
r == b.
r) && (a.
g == b.
g) && (a.
b == b.
b));
48 template <
class T>
class image {
58 void init(
const T &val);
84 for (
int i = 0; i < h; i++)
89 memset(
data, 0, w * h *
sizeof(T));
98 T *ptr =
imPtr(
this, 0, 0);
99 T *end =
imPtr(
this, w-1, h-1);
107 memcpy(im->
data,
data, w * h *
sizeof(T));
133 std::ifstream file(name, std::ios::in | std::ios::binary);
135 if (strncmp(buf,
"P5", 2)) {
136 std::cout <<
"ERROR: Could not read file " << name << std::endl;
141 int width = atoi(buf);
146 if (atoi(buf) > UCHAR_MAX) {
147 std::cout <<
"ERROR: Could not read file " << name << std::endl;
153 file.read((
char *)
imPtr(im, 0, 0), width * height *
sizeof(
uchar));
161 std::ofstream file(name, std::ios::out | std::ios::binary);
163 file <<
"P5\n" << width <<
" " << height <<
"\n" << UCHAR_MAX <<
"\n";
164 file.write((
char *)
imPtr(im, 0, 0), width * height *
sizeof(
uchar));
void savePGM(image< uchar > *im, const char *name)
Definition: image.h:158
bool operator==(const rgb &a, const rgb &b)
Definition: image.h:43
void pnm_read(std::ifstream &file, char *buf)
Definition: image.h:113
image(const int width, const int height, const bool init=false)
Definition: image.h:77
uchar r
Definition: image.h:41
#define BUF_SIZE
Definition: image.h:38
image< uchar > * loadPGM(const char *name)
Definition: image.h:129
uchar b
Definition: image.h:41
T ** access
Definition: image.h:71
int width() const
Definition: image.h:64
image< T > * copy() const
Definition: image.h:105
~image()
Definition: image.h:92
int height() const
Definition: image.h:65
unsigned char uchar
Definition: image.h:40
T * data
Definition: image.h:68
uchar g
Definition: image.h:41
#define imPtr(im, x, y)
Definition: image.h:36
void init(const T &val)
Definition: image.h:97