Browse Source

Introduce pixel format to the BNG format

rexim 5 years ago
parent
commit
34cb276d45
4 changed files with 34 additions and 9 deletions
  1. 1 1
      Makefile
  2. 2 0
      README.md
  3. 11 1
      bng.h
  4. 20 7
      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
+	./png2bng tsodinw.png tsodinw.bng
 
 bngviewer: bngviewer.c bng.h
 	$(CC) $(BNGVIEWER_CFLAGS) -O3 -o bngviewer bngviewer.c $(BNGVIEWER_LIBS)

+ 2 - 0
README.md

@@ -22,4 +22,6 @@
       - [x] Call a WASM function that turns the file into Image Data
       - [x] Take out the Image Data from WASM memory and display it
     - [ ] Add more interesting features to bng to test the support
+      - [ ] Different pixel formats
+      - [ ] Different compressions
     - [ ] Reusable BNG library that works both natively and being compiled to WASM

+ 11 - 1
bng.h

@@ -3,12 +3,22 @@
 
 #define BNG_MAGIC 0x21474E42
 
+#if defined(__GNUC__) || defined(__clang__)
+#  define PACKED __attribute__((packed))
+#else
+#  warning "Packed attributes for struct is not implemented for this compiler. Feel free to fix that and submit a Pull Request to https://github.com/tsoding/bng"
+#endif
+
 struct Bng
 {
     uint32_t magic;
     uint32_t width;
     uint32_t height;
+    uint8_t red_byte;           // 0
+    uint8_t green_byte;         // 1
+    uint8_t blue_byte;          // 2
+    uint8_t alpha_byte;         // 3
     uint8_t data[];
-};
+} PACKED;
 
 #endif  // BNG_H_

+ 20 - 7
png2bng.c

@@ -2,18 +2,31 @@
 #include "./stb_image.h"
 #include "./bng.h"
 
-int main()
+int main(int argc, char **argv)
 {
+    if (argc < 3) {
+        fprintf(stderr, "png2bng <input.png> <output.bng>\n");
+        exit(1);
+    }
+
+    const char *input_filepath = argv[1];
+    const char *output_filepath = argv[2];
+
     int x, y, n;
-    unsigned char *data = stbi_load("./tsodinw.png", &x, &y, &n, 4);
+    unsigned char *data = stbi_load(input_filepath, &x, &y, &n, 4);
     assert(data != NULL);
 
-    struct Bng bng = {};
-    bng.magic = BNG_MAGIC;
-    bng.width = x;
-    bng.height = y;
+    struct Bng bng = {
+        .magic      = BNG_MAGIC,
+        .width      = x,
+        .height     = y,
+        .red_byte   = 0,
+        .green_byte = 1,
+        .blue_byte  = 2,
+        .alpha_byte = 3,
+    };
 
-    FILE *out = fopen("./tsodinw.bng", "wb");
+    FILE *out = fopen(output_filepath, "wb");
     fwrite(&bng, 1, sizeof(bng), out);
     fwrite(data, 1, x * y * 4, out);
     fclose(out);