summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2010-01-13 11:46:50 -0800
committerLinus Torvalds <torvalds@linux-foundation.org>2010-01-13 11:46:50 -0800
commit5107cd54456f8e0fb31823434d30a60323a9224c (patch)
tree33e0c0d6007a49311bed4fd79b3ec68f15e91a17
parent7a7221a3b06882d314d4256d31cdf4a68c2f6b5d (diff)
downloadpesconvert-5107cd54456f8e0fb31823434d30a60323a9224c.tar.gz
Add stupid PNG support
It's stupid because: - it's fixed to output 128x128 png images (think preview icons) - it doesn't actually draw lines, it just does them as dots at the endpoints but it does kind of work.
-rw-r--r--Makefile6
-rw-r--r--pes.c2
-rw-r--r--png.c52
3 files changed, 56 insertions, 4 deletions
diff --git a/Makefile b/Makefile
index e2ebd02..091b838 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
CC=gcc
-CFLAGS=-Wall
+CFLAGS=-Wall -g
-pesconvert: pes.o svg.o
- $(CC) -o $@ $^
+pesconvert: pes.o svg.o png.o
+ $(CC) -o $@ $^ -lpng
diff --git a/pes.c b/pes.c
index 7a9a0e9..7bd6f3e 100644
--- a/pes.c
+++ b/pes.c
@@ -321,7 +321,7 @@ int main(int argc, char **argv)
if (parse_pes(&region, &pes) < 0)
die("Unable to parse PES file\n");
- output_svg(&pes);
+ output_png(&pes);
return 0;
}
diff --git a/png.c b/png.c
new file mode 100644
index 0000000..25a5967
--- /dev/null
+++ b/png.c
@@ -0,0 +1,52 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <png.h>
+
+#include "pes.h"
+
+void output_png(struct pes *pes)
+{
+ int i;
+ int width = pes->max_x - pes->min_x + 1;
+ int height = pes->max_y - pes->min_y + 1;
+ int outw = 128, outh = 128;
+ png_byte **rows;
+ struct pes_block *block;
+ png_structp png_ptr;
+ png_infop info_ptr;
+
+ rows = calloc(sizeof(*rows), outh);
+ for (i = 0; i < height; i++)
+ rows[i] = calloc(sizeof(png_byte)*4, outw);
+
+ block = pes->blocks;
+ while (block) {
+ struct color *c = block->color;
+ struct stitch *stitch = block->stitch;
+ int i;
+
+ for (i = 0; i < block->nr_stitches; i++, stitch++) {
+ int x = (stitch->x - pes->min_x) * outw / width;
+ int y = (stitch->y - pes->min_y) * outh / height;
+ png_byte *ptr = rows[y] + x*4;
+
+ ptr[0] = c->r;
+ ptr[1] = c->g;
+ ptr[2] = c->b;
+ ptr[3] = 255;
+ }
+ block = block->next;
+ }
+
+ png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+ info_ptr = png_create_info_struct(png_ptr);
+ png_init_io(png_ptr, stdout);
+ png_set_IHDR(png_ptr, info_ptr, outw, outh,
+ 8, PNG_COLOR_TYPE_RGBA, PNG_INTERLACE_NONE,
+ PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
+
+ png_write_info(png_ptr, info_ptr);
+
+ png_write_image(png_ptr, rows);
+ png_write_end(png_ptr, NULL);
+}