summaryrefslogtreecommitdiff
path: root/third_party/spiro/ppedit/image.c
blob: 215d27dd1dbedc7e9c229e2d8c75854c641a8c96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
ppedit - A pattern plate editor for Spiro splines.
Copyright (C) 2007 Raph Levien

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.

*/
#include <stdio.h>
#include <string.h>
#include "zmisc.h"
#include "image.h"

/* An image loaded into memory. */
struct _image {
    unsigned char *buf;
    int width;
    int height;
    int rowstride;
};

static image *
load_ppm_file(FILE *f, char **reason)
{
    image *result;
    char line[256];
    int xs, ys;
    int depth;
    int n;

    fseek(f, 0, SEEK_SET);
    fgets(line, sizeof(line), f);
    do {
	fgets(line, sizeof(line), f);
    } while (line[0] == '#');
    n = sscanf(line, "%d %d", &xs, &ys);
    if (n != 2) {
	*reason = "Error reading ppmraw size line";
	fclose(f);
	return NULL;
    }
    do {
	fgets(line, sizeof(line), f);
    } while (line[0] == '#');
    n = sscanf(line, "%d", &depth);
    if (n != 1) {
	*reason = "Error reading ppmraw depth line";
	fclose(f);
	return NULL;
    }
    result = znew(image, 1);
    result->rowstride = 3 * xs;
    result->buf = zalloc(ys * result->rowstride);
    result->width = xs;
    result->height = ys;
    fread(result->buf, 1, ys * result->rowstride, f);
    fclose(f);
    return result;
}

image *
load_image_file(const char *fn, char **reason)
{
    FILE *f = fopen(fn, "rb");
    unsigned char buf[256];
    int n;

    if (f == NULL) {
	*reason = "Error opening file";
	return NULL;
    }
    n = fread(buf, 1, sizeof(buf), f);
    if (n < 4) {
	*reason = "Short file";
	fclose(f);
	return NULL;
    }
    if (buf[0] != 'P' || buf[1] != '6') {
	*reason = "Unrecognized magic";
	fclose(f);
	return NULL;
    }
    return load_ppm_file(f, reason);
}

void
free_image(image *im)
{
    zfree(im->buf);
    zfree(im);
}

void
render_image(image *im, const double affine[6],
	     unsigned char *buf, int rowstride, int x0, int y0, int x1, int y1)
{
    int y;
    unsigned char *dest_line = buf;
    int src_x0 = x0;

    for (y = y0; y < y1; y++) {
	int src_y = y;

	if (src_y >= 0 && src_y < im->height) {
	    unsigned char *img_line = im->buf + src_y * im->rowstride;
	    int left_pad = -src_x0;
	    int img_run, img_off, right_pad;

	    if (left_pad > x1 - x0) left_pad = x1 - x0;
	    if (left_pad > 0) {
		memset(dest_line, 255, 3 * left_pad);
	    } else left_pad = 0;
	    img_off = src_x0;
	    if (img_off < 0) img_off = 0;
	    img_run = x1 - x0 - left_pad;
	    if (img_run > im->width - img_off) img_run = im->width - img_off;
	    if (img_run > 0) {
		memcpy(dest_line + 3 * left_pad, img_line + 3 * img_off, 3 * img_run);
	    } else img_run = 0;
	    right_pad = x1 - x0 - left_pad - img_run;
	    if (right_pad > 0) {
		memset(dest_line + 3 * (left_pad + img_run), 255, 3 * right_pad);
	    }
	} else {
	    memset(dest_line, 255, rowstride);
	}
	dest_line += rowstride;
    }
}