]> Dogcows Code - chaz/rasterize/blobdiff - raster.cc
added texturing
[chaz/rasterize] / raster.cc
index 66109e756406ca78f37ed71457d0d5a36ef262e6..f814ada21712d2454afa0b59f74fb8403e96b96d 100644 (file)
--- a/raster.cc
+++ b/raster.cc
@@ -48,6 +48,22 @@ color_t* raster_color(const raster_t* p, int x, int y)
     return p->pixels + p->w * y + x;
 }
 
+color_t raster_uv(const raster_t* p, vec_t uv)
+{
+    uv.x = scal_clamp(uv.x, S(0.0), S(1.0));
+    uv.y = scal_clamp(uv.y, S(0.0), S(1.0));
+    uv.y = S(1.0) - uv.y;
+    int x = (int)((scal_t)p->w * uv.x);
+    int y = (int)((scal_t)p->h * uv.y);
+    if (p->w <= x) {
+        x = p->w - 1;
+    }
+    if (p->h <= y) {
+        y = p->h - 1;
+    }
+    return *raster_color(p, x, y);
+}
+
 int raster_width(const raster_t* p)
 {
     return p->w;
@@ -290,6 +306,11 @@ raster_t* raster_import_bmp(const char* filename)
     _DO_OR_DIE(fread(&paletteColorCount, sizeof(paletteColorCount), 1, file));
     _DO_OR_DIE(fread(&importantPaletteColorCount, sizeof(importantPaletteColorCount), 1, file));
 
+    if ((int)width < 0 || (int)height < 0) {
+        fprintf(stderr, "Unexpected file format in %s: Try a ppm file instead\n", filename);
+        fclose(file);
+        return NULL;
+    }
     p = raster_alloc((int)width, (int)height, COLOR_WHITE);
 
     size = width * height;
This page took 0.02205 seconds and 4 git commands to generate.