Explorar o código

Add support for custom pixel formats to bngviewer

rexim %!s(int64=5) %!d(string=hai) anos
pai
achega
be72b23739
Modificáronse 4 ficheiros con 29 adicións e 17 borrados
  1. 1 1
      Makefile
  2. 16 1
      bng.h
  3. 11 2
      bngviewer.c
  4. 1 13
      png2bng.c

+ 1 - 1
Makefile

@@ -7,7 +7,7 @@ png2bng: png2bng.c bng.h stb_image.h
 	$(CC) -O3 -o png2bng png2bng.c -lm
 
 tsodinw.bng: png2bng tsodinw.png
-	./png2bng tsodinw.png tsodinw.bng
+	./png2bng tsodinw.png tsodinw.bng RAGB
 
 bngviewer: bngviewer.c bng.h
 	$(CC) $(BNGVIEWER_CFLAGS) -O3 -o bngviewer bngviewer.c $(BNGVIEWER_LIBS)

+ 16 - 1
bng.h

@@ -35,7 +35,22 @@ struct Bng
     uint32_t width;
     uint32_t height;
     struct Bng_Pixel_Format pixel_format;
-    uint8_t data[];
+    uint32_t pixels[];
 } PACKED;
 
+#define GET_BYTE(bytes, index) ((bytes & (0xFF << ((4 - index - 1) * 8))) >> ((4 - index - 1) * 8))
+#define SET_BYTE(bytes, index, byte) bytes = (bytes | (byte << ((4 - index - 1) * 8)))
+
+uint32_t convert_pixel(uint32_t pixel,
+                       struct Bng_Pixel_Format source,
+                       struct Bng_Pixel_Format desired)
+{
+    uint32_t result = 0;
+    SET_BYTE(result, desired.red_byte,   GET_BYTE(pixel, source.red_byte));
+    SET_BYTE(result, desired.green_byte, GET_BYTE(pixel, source.green_byte));
+    SET_BYTE(result, desired.blue_byte,  GET_BYTE(pixel, source.blue_byte));
+    SET_BYTE(result, desired.alpha_byte, GET_BYTE(pixel, source.alpha_byte));
+    return result;
+}
+
 #endif  // BNG_H_

+ 11 - 2
bngviewer.c

@@ -17,7 +17,16 @@ void* read_whole_file(const char *filename)
 
 struct Bng *bng_load(const char *filepath)
 {
-    return read_whole_file(filepath);
+    struct Bng *bng = read_whole_file(filepath);
+
+    for (size_t i = 0; i < bng->width * bng->height; ++i) {
+        bng->pixels[i] = convert_pixel(
+            bng->pixels[i],
+            bng->pixel_format,
+            RGBA);
+    }
+
+    return bng;
 }
 
 int main(int argc, char *argv[])
@@ -39,7 +48,7 @@ int main(int argc, char *argv[])
     struct Bng *bng = bng_load("tsodinw.bng");
 
     SDL_Surface* image_surface =
-        SDL_CreateRGBSurfaceFrom(bng->data,
+        SDL_CreateRGBSurfaceFrom(bng->pixels,
                                  bng->width,
                                  bng->height,
                                  32,

+ 1 - 13
png2bng.c

@@ -2,20 +2,8 @@
 #include "./stb_image.h"
 #include "./bng.h"
 
-#define GET_BYTE(bytes, index) ((bytes & (0xFF << ((4 - index - 1) * 8))) >> ((4 - index - 1) * 8))
-#define SET_BYTE(bytes, index, byte) bytes = (bytes | (byte << ((4 - index - 1) * 8)))
 
-uint32_t convert_pixel(uint32_t pixel,
-                       struct Bng_Pixel_Format source,
-                       struct Bng_Pixel_Format desired)
-{
-    uint32_t result = 0;
-    SET_BYTE(result, desired.red_byte,   GET_BYTE(pixel, source.red_byte));
-    SET_BYTE(result, desired.green_byte, GET_BYTE(pixel, source.green_byte));
-    SET_BYTE(result, desired.blue_byte,  GET_BYTE(pixel, source.blue_byte));
-    SET_BYTE(result, desired.alpha_byte, GET_BYTE(pixel, source.alpha_byte));
-    return result;
-}
+
 
 struct Bng_Pixel_Format pixel_format_by_name(const char *name)
 {