12 Pixmap::Pixmap(const Pixmap &pixmap)
16 set_image(pixmap.width, pixmap.height, pixmap.pixels);
19 Pixmap &Pixmap::operator=(const Pixmap &pixmap)
22 set_image(pixmap.width, pixmap.height, pixmap.pixels);
35 int Pixmap::get_width() const
40 int Pixmap::get_height() const
45 Rect Pixmap::get_rect() const
47 Rect rect(0, 0, width, height);
51 bool Pixmap::set_image(int x, int y, unsigned char *pix)
55 pixels = new unsigned char[x * y * 4];
60 memcpy(pixels, pix, x * y * 4);
65 const unsigned char *Pixmap::get_image() const
70 unsigned char *Pixmap::get_image()
75 bool Pixmap::load(const char *fname)
80 if(!(fp = fopen(fname, "rb"))) {
81 fprintf(stderr, "failed to open pixmap: %s: %s\n", fname, strerror(errno));
89 if(!fgets(buf, sizeof buf, fp))
98 /* first header line should be P6 */
99 if(strcmp(buf, "P6\n") != 0)
104 /* second header line contains the pixmap dimensions */
105 if(sscanf(buf, "%d %d", &width, &height) != 2)
111 set_image(width, height, 0);
113 for(int i=0; i<width * height * 4; i++) {
129 fprintf(stderr, "failed to load pixmap: %s\n", fname);
134 bool Pixmap::save(const char *fname) const
140 FILE *fp = fopen(fname, "wb");
142 fprintf(stderr, "failed to save pixmap: %s: %s\n", fname, strerror(errno));
146 fprintf(fp, "P6\n%d %d\n255\n", width, height);
148 for(int i=0; i<width * height; i++) {
149 fputc(pixels[i * 4], fp);
150 fputc(pixels[i * 4 + 1], fp);
151 fputc(pixels[i * 4 + 2], fp);