2 winnie - an experimental window system
4 Copyright (C) 2013 Eleni Maria Stea
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 Author: Eleni Maria Stea <elene.mst@gmail.com>
33 Pixmap::Pixmap(const Pixmap &pixmap)
37 set_image(pixmap.width, pixmap.height, pixmap.pixels);
40 Pixmap &Pixmap::operator=(const Pixmap &pixmap)
43 set_image(pixmap.width, pixmap.height, pixmap.pixels);
56 int Pixmap::get_width() const
61 int Pixmap::get_height() const
66 Rect Pixmap::get_rect() const
68 Rect rect(0, 0, width, height);
72 bool Pixmap::set_image(int x, int y, unsigned char *pix)
76 pixels = new unsigned char[x * y * 4];
81 memcpy(pixels, pix, x * y * 4);
86 const unsigned char *Pixmap::get_image() const
91 unsigned char *Pixmap::get_image()
96 bool Pixmap::load(const char *fname)
101 if(!(fp = fopen(fname, "rb"))) {
102 fprintf(stderr, "failed to open pixmap: %s: %s\n", fname, strerror(errno));
106 /* read ppm header */
110 if(!fgets(buf, sizeof buf, fp))
119 /* first header line should be P6 */
120 if(strcmp(buf, "P6\n") != 0)
125 /* second header line contains the pixmap dimensions */
126 if(sscanf(buf, "%d %d", &width, &height) != 2)
132 set_image(width, height, 0);
134 for(int i=0; i<width * height * 4; i++) {
150 fprintf(stderr, "failed to load pixmap: %s\n", fname);
155 bool Pixmap::save(const char *fname) const
161 FILE *fp = fopen(fname, "wb");
163 fprintf(stderr, "failed to save pixmap: %s: %s\n", fname, strerror(errno));
167 fprintf(fp, "P6\n%d %d\n255\n", width, height);
169 for(int i=0; i<width * height; i++) {
170 fputc(pixels[i * 4], fp);
171 fputc(pixels[i * 4 + 1], fp);
172 fputc(pixels[i * 4 + 2], fp);