commit bc58a701d118e074c935bf29af095d72b95f8a31
parent 372aec2290b9215335a7fbc3c3f42da629b0aa5f
Author: FRIGN <dev@frign.de>
Date:   Tue, 10 Nov 2015 18:35:22 +0100
Fix 16-bit RGB-expands and header-endianness properly
Diffstat:
2 files changed, 6 insertions(+), 4 deletions(-)
diff --git a/ff2png.c b/ff2png.c
@@ -46,8 +46,8 @@ main(int argc, char *argv[])
 		fprintf(stderr, "invalid magic in header\n");
 		return 1;
 	}
-	width = be32toh((hdr[9] << 0) | (hdr[10] << 8) | (hdr[11] << 16) | (hdr[12] << 24));
-	height = be32toh((hdr[13] << 0) | (hdr[14] << 8) | (hdr[15] << 16) | (hdr[16] << 24));
+	width = be32toh(*((uint32_t *)(hdr + 8)));
+	height = be32toh(*((uint32_t *)(hdr + 12)));
 
 	/* load png */
 	png_struct_p = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
@@ -76,7 +76,8 @@ main(int argc, char *argv[])
 				fprintf(stderr, "unexpected EOF or row-skew\n");
 				return 1;
 			}
-			png_row[j] = be16toh(tmp16) / (1 << 8);
+			/* ((2^16-1) / 255) == 257 */
+			png_row[j] = (uint8_t)(be16toh(tmp16) / 257);
 		}
 		png_write_row(png_struct_p, png_row);
 	}
diff --git a/png2ff.c b/png2ff.c
@@ -64,7 +64,8 @@ main(int argc, char *argv[])
 	/* TODO: allow 16 bit PNGs to be converted losslessly */
 	for (r = 0; r < height; ++r) {
 		for (i = 0; i < png_row_len; i++) {
-			tmp16 = htobe16((uint16_t)png_row_p[r][i]);
+			/* ((2^16-1) / 255) == 257 */
+			tmp16 = htobe16(257 * png_row_p[r][i]);
 			fwrite(&tmp16, sizeof(uint16_t), 1, stdout);
 		}
 	}