X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=raster.cc;h=f814ada21712d2454afa0b59f74fb8403e96b96d;hb=9f13ae5fe777dd4f7a9302f6dcf3e0d99a1ce65d;hp=66109e756406ca78f37ed71457d0d5a36ef262e6;hpb=a737ad45e690daa3b39b8885e2b6574d2ea48833;p=chaz%2Frasterize diff --git a/raster.cc b/raster.cc index 66109e7..f814ada 100644 --- 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;