X-Git-Url: http://git.mutantstargoat.com?a=blobdiff_plain;f=src%2Fpixmap.cc;h=8e50fa3caba353c843abbb9b4ce616a35f63edef;hb=e2626c41c841dbbfb64ddf6341b4e23089036299;hp=5c9b2425ab13bf0363a0fc2f4b4ff202fdafd11a;hpb=5449bf8fbca1d2cfbefa0386df7109afb2a5aa34;p=winnie
diff --git a/src/pixmap.cc b/src/pixmap.cc
index 5c9b242..8e50fa3 100644
--- a/src/pixmap.cc
+++ b/src/pixmap.cc
@@ -1,3 +1,24 @@
+/*
+winnie - an experimental window system
+
+Copyright (C) 2013 Eleni Maria Stea
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program 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 General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see .
+
+Author: Eleni Maria Stea
+*/
+
#include
#include
#include
@@ -9,6 +30,22 @@ Pixmap::Pixmap()
pixels = 0;
}
+Pixmap::Pixmap(const Pixmap &pixmap)
+{
+ width = height = 0;
+ pixels = 0;
+ set_image(pixmap.width, pixmap.height, pixmap.pixels);
+}
+
+Pixmap &Pixmap::operator=(const Pixmap &pixmap)
+{
+ if(this != &pixmap) {
+ set_image(pixmap.width, pixmap.height, pixmap.pixels);
+ }
+
+ return *this;
+}
+
Pixmap::~Pixmap()
{
if(pixels) {
@@ -28,7 +65,7 @@ int Pixmap::get_height() const
Rect Pixmap::get_rect() const
{
- Rect rect = {0, 0, width, height};
+ Rect rect(0, 0, width, height);
return rect;
}
@@ -58,7 +95,61 @@ unsigned char *Pixmap::get_image()
bool Pixmap::load(const char *fname)
{
- return false; // TODO
+ FILE *fp;
+ int hdrline = 0;
+
+ if(!(fp = fopen(fname, "rb"))) {
+ fprintf(stderr, "failed to open pixmap: %s: %s\n", fname, strerror(errno));
+ return false;
+ }
+
+ /* read ppm header */
+ while(hdrline < 3) {
+ char buf[64];
+
+ if(!fgets(buf, sizeof buf, fp))
+ goto err;
+
+ /* skip comments */
+ if(buf[0] == '#')
+ continue;
+
+ switch(hdrline++) {
+ case 0:
+ /* first header line should be P6 */
+ if(strcmp(buf, "P6\n") != 0)
+ goto err;
+ break;
+
+ case 1:
+ /* second header line contains the pixmap dimensions */
+ if(sscanf(buf, "%d %d", &width, &height) != 2)
+ goto err;
+ break;
+ }
+ }
+
+ set_image(width, height, 0);
+
+ for(int i=0; i