]> Dogcows Code - chaz/rasterize/blobdiff - raster.c
add opengl support
[chaz/rasterize] / raster.c
index 9cb12c5637f64694fdf9c2631eec65b1b36e0d9f..dcbe76fe767015fff447f1d5206a35618fc1fb17 100644 (file)
--- a/raster.c
+++ b/raster.c
@@ -103,6 +103,27 @@ color_t raster_color(const raster_t* p, vec_t pt)
     return p->pixels[p->w * (p->h - v - 1) + u];
 }
 
+int raster_width(const raster_t* p)
+{
+    return p->w;
+}
+
+int raster_height(const raster_t* p)
+{
+    return p->h;
+}
+
+void* raster_data(const raster_t* p)
+{
+    size_t size = p->w * p->h;
+    rgba_t* data = mem_alloc(size * sizeof(rgba_t));
+
+    for (int i = 0; i < size; ++i) {
+        data[i] = rgba_from_color(p->pixels[i]);
+    }
+    return data;
+}
+
 
 void raster_clear(raster_t* p, color_t fill)
 {
@@ -252,12 +273,10 @@ fail:   fprintf(stderr, "Cannot write to %s: %s\n", filename, strerror(errno));
     size_t size = width * height;
     for (int i = 0; i < size; ++i)
     {
-        rgbachan_t a, r, g, b;
-        color_split(p->pixels[i], &r, &g, &b, &a);
-        uint32_t argb = PACK(argb, 3, a);
-        argb = PACK(argb, 2, r);
-        argb = PACK(argb, 1, g);
-        argb = PACK(argb, 0, b);
+        struct {
+            rgbachan_t b, g, r, a;
+        } argb;
+        color_split(p->pixels[i], &argb.r, &argb.g, &argb.b, &argb.a);
         _DO_OR_DIE(fwrite(&argb, sizeof(argb), 1, file));
     }
     
@@ -311,11 +330,17 @@ raster_t* raster_import_ppm(const char* filename)
                 fprintf(stderr, "Failed reading color values from %s: %s\n", filename, strerror(errno));
                 return NULL;
             }
-            rgba_t rgba = PACK(rgba, 3, (uint8_t)r);
-            rgba = PACK(rgba, 2, (uint8_t)g);
-            rgba = PACK(rgba, 1, (uint8_t)b);
-            rgba = PACK(rgba, 0, 255);
-            p->pixels[y * w + x] = color_from_rgba(rgba);
+            union {
+                rgba_t rgba;
+                struct {
+                    rgbachan_t r, g, b, a;
+                } chan;
+            } u;
+            u.chan.r = (rgbachan_t)r;
+            u.chan.g = (rgbachan_t)g;
+            u.chan.b = (rgbachan_t)b;
+            u.chan.a = 255;
+            p->pixels[y * w + x] = color_from_rgba(u.rgba);
         }
     }
 
@@ -378,13 +403,25 @@ raster_t* raster_import_bmp(const char* filename)
     size_t size = width * height;
     for (int i = 0; i < size; ++i)
     {
-        uint32_t argb;
-        _DO_OR_DIE(fread(&argb, sizeof(argb), 1, file));
-        rgba_t rgba = PACK(rgba, 3, UNPACK(argb, 2));
-        rgba = PACK(rgba, 2, UNPACK(argb, 1));
-        rgba = PACK(rgba, 1, UNPACK(argb, 0));
-        rgba = PACK(rgba, 0, UNPACK(argb, 3));
-        p->pixels[i] = color_from_rgba(rgba);
+        union {
+            rgba_t rgba;
+            struct {
+                rgbachan_t r, g, b, a;
+            } chan;
+        } u;
+        _DO_OR_DIE(fread(&u, sizeof(u), 1, file));
+        rgbachan_t t = u.chan.r;
+        u.chan.r = u.chan.a;
+        u.chan.a = t;
+        t = u.chan.g;
+        u.chan.g = u.chan.b;
+        u.chan.b = t;
+        t = u.chan.r;
+        u.chan.r = u.chan.g;
+        u.chan.g = u.chan.b;
+        u.chan.b = u.chan.a;
+        u.chan.a = t;
+        p->pixels[i] = color_from_rgba(u.rgba);
     }
 
     fclose(file);
@@ -414,14 +451,15 @@ void raster_draw_model(raster_t* p, const model_t* model)
     raster_material(p, model_specular(model), model_shininess(model));
     p->current = model;
     IF_RENDER_PROGRESS(tri = 0);
-    for (const list_t* ti = model_geometry(model); ti; ti = ti->link) {
+    array_it_t it = array_begin(model_geometry(model));
+    for (tri_t* t; t = array_it_tri_next(&it);) {
 #if VERBOSITY >= 4
         if (++tri % 100 == 0) {
             printf(PROGRESS_FMT, model_name(model), tri, model_size(model));
             fflush(stdout);
         }
 #endif
-        raster_draw_tri(p, (tri_t*)ti->val);
+        raster_draw_tri(p, t);
     }
 #if VERBOSITY >= 4
     printf(PROGRESS_FMT"\n", model_name(model), tri, model_size(model));
This page took 0.019521 seconds and 4 git commands to generate.