diff options
author | Linus Torvalds <torvalds@linux-foundation.org> | 2010-01-13 11:46:50 -0800 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2010-01-13 11:46:50 -0800 |
commit | 5107cd54456f8e0fb31823434d30a60323a9224c (patch) | |
tree | 33e0c0d6007a49311bed4fd79b3ec68f15e91a17 | |
parent | 7a7221a3b06882d314d4256d31cdf4a68c2f6b5d (diff) | |
download | pesconvert-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-- | Makefile | 6 | ||||
-rw-r--r-- | pes.c | 2 | ||||
-rw-r--r-- | png.c | 52 |
3 files changed, 56 insertions, 4 deletions
@@ -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 @@ -321,7 +321,7 @@ int main(int argc, char **argv) if (parse_pes(®ion, &pes) < 0) die("Unable to parse PES file\n"); - output_svg(&pes); + output_png(&pes); return 0; } @@ -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); +} |