From b8b364c301625401cd64eeb2cb0ed74cc89754ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-S=E9bastien=20Guay?= Date: Sat, 9 Aug 2014 00:34:49 -0400 Subject: [PATCH 1/3] stbi_write_tga can now write 1 and 2 channel (grayscale and grayscale-alpha) images. --- stb_image_write.h | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/stb_image_write.h b/stb_image_write.h index 4bdd4bc..453c4c2 100644 --- a/stb_image_write.h +++ b/stb_image_write.h @@ -128,8 +128,9 @@ static void write_pixels(FILE *f, int rgb_dir, int vdir, int x, int y, int comp, if (write_alpha < 0) fwrite(&d[comp-1], 1, 1, f); switch (comp) { - case 1: - case 2: write3(f, d[0],d[0],d[0]); + case 1: fwrite(d, 1, 1, f); + break; + case 2: fwrite(d, 2, 1, f); break; case 4: if (!write_alpha) { @@ -179,8 +180,11 @@ int stbi_write_bmp(char const *filename, int x, int y, int comp, const void *dat int stbi_write_tga(char const *filename, int x, int y, int comp, const void *data) { int has_alpha = !(comp & 1); + int colorbytes = comp - has_alpha; + int format = colorbytes < 2 ? 3 : 2; // 3 color channels (RGB/RGBA) = 3, 1 color channel (Y/YA) = 2 + if (format == 2) colorbytes = 3; // is 2 color channels (RG/RGA) even possible? write as RGB/RGBA return outfile(filename, -1,-1, x, y, comp, (void *) data, has_alpha, 0, - "111 221 2222 11", 0,0,2, 0,0,0, 0,0,x,y, 24+8*has_alpha, 8*has_alpha); + "111 221 2222 11", 0,0,format, 0,0,0, 0,0,x,y, (colorbytes+has_alpha)*8, has_alpha*8); } // stretchy buffer; stbiw__sbpush() == vector<>::push_back() -- stbiw__sbcount() == vector<>::size() From 26a71f67feefd19f2a8622d54207c3e6a2e21176 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-S=E9bastien=20Guay?= Date: Sat, 9 Aug 2014 01:08:43 -0400 Subject: [PATCH 2/3] comment was wrong way around... code was right. --- stb_image_write.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stb_image_write.h b/stb_image_write.h index 453c4c2..fb10dfa 100644 --- a/stb_image_write.h +++ b/stb_image_write.h @@ -181,7 +181,7 @@ int stbi_write_tga(char const *filename, int x, int y, int comp, const void *dat { int has_alpha = !(comp & 1); int colorbytes = comp - has_alpha; - int format = colorbytes < 2 ? 3 : 2; // 3 color channels (RGB/RGBA) = 3, 1 color channel (Y/YA) = 2 + int format = colorbytes < 2 ? 3 : 2; // 3 color channels (RGB/RGBA) = 2, 1 color channel (Y/YA) = 3 if (format == 2) colorbytes = 3; // is 2 color channels (RG/RGA) even possible? write as RGB/RGBA return outfile(filename, -1,-1, x, y, comp, (void *) data, has_alpha, 0, "111 221 2222 11", 0,0,format, 0,0,0, 0,0,x,y, (colorbytes+has_alpha)*8, has_alpha*8); From ebc2d23d474f63579803e5598864329a935aecd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-S=E9bastien=20Guay?= Date: Sat, 9 Aug 2014 09:16:57 -0400 Subject: [PATCH 3/3] realized the 2 colorbytes case is impossible. also updated the comment at the top of the file to reflect stbi_write_tga's new capabilities. --- stb_image_write.h | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/stb_image_write.h b/stb_image_write.h index fb10dfa..8c60f0d 100644 --- a/stb_image_write.h +++ b/stb_image_write.h @@ -42,7 +42,7 @@ USAGE: a row of pixels to the first byte of the next row of pixels. PNG creates output files with the same number of components as the input. - The BMP and TGA formats expand Y to RGB in the file format. BMP does not + The BMP format expands Y to RGB in the file format and does not output alpha. PNG supports writing rectangles of data even when the bytes storing rows of @@ -128,9 +128,8 @@ static void write_pixels(FILE *f, int rgb_dir, int vdir, int x, int y, int comp, if (write_alpha < 0) fwrite(&d[comp-1], 1, 1, f); switch (comp) { - case 1: fwrite(d, 1, 1, f); - break; - case 2: fwrite(d, 2, 1, f); + case 1: + case 2: fwrite(d, 1, 1, f); break; case 4: if (!write_alpha) { @@ -182,7 +181,6 @@ int stbi_write_tga(char const *filename, int x, int y, int comp, const void *dat int has_alpha = !(comp & 1); int colorbytes = comp - has_alpha; int format = colorbytes < 2 ? 3 : 2; // 3 color channels (RGB/RGBA) = 2, 1 color channel (Y/YA) = 3 - if (format == 2) colorbytes = 3; // is 2 color channels (RG/RGA) even possible? write as RGB/RGBA return outfile(filename, -1,-1, x, y, comp, (void *) data, has_alpha, 0, "111 221 2222 11", 0,0,format, 0,0,0, 0,0,x,y, (colorbytes+has_alpha)*8, has_alpha*8); }