Debian Codegraph @

third_party/spiro/ppedit/image.c

raw symbols SCIP
1 /*
2 ppedit - A pattern plate editor for Spiro splines.
3 Copyright (C) 2007 Raph Levien
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 02110-1301, USA.
19
20 */
21 #include <stdio.h>
22 #include <string.h>
23 #include "zmisc.h"
24 #include "image.h"
25
26 /* An image loaded into memory. */
27 struct _image {
28 unsigned char *buf;
29 int width;
30 int height;
31 int rowstride;
32 };
33
34 static image *
35 load_ppm_file(FILE *f, char **reason)
36 {
37 image *result;
38 char line[256];
39 int xs, ys;
40 int depth;
41 int n;
42
43 fseek(f, 0, SEEK_SET);
44 fgets(line, sizeof(line), f);
45 do {
46 fgets(line, sizeof(line), f);
47 } while (line[0] == '#');
48 n = sscanf(line, "%d %d", &xs, &ys);
49 if (n != 2) {
50 *reason = "Error reading ppmraw size line";
51 fclose(f);
52 return NULL;
53 }
54 do {
55 fgets(line, sizeof(line), f);
56 } while (line[0] == '#');
57 n = sscanf(line, "%d", &depth);
58 if (n != 1) {
59 *reason = "Error reading ppmraw depth line";
60 fclose(f);
61 return NULL;
62 }
63 result = znew(image, 1);
64 result->rowstride = 3 * xs;
65 result->buf = zalloc(ys * result->rowstride);
66 result->width = xs;
67 result->height = ys;
68 fread(result->buf, 1, ys * result->rowstride, f);
69 fclose(f);
70 return result;
71 }
72
73 image *
74 load_image_file(const char *fn, char **reason)
75 {
76 FILE *f = fopen(fn, "rb");
77 unsigned char buf[256];
78 int n;
79
80 if (f == NULL) {
81 *reason = "Error opening file";
82 return NULL;
83 }
84 n = fread(buf, 1, sizeof(buf), f);
85 if (n < 4) {
86 *reason = "Short file";
87 fclose(f);
88 return NULL;
89 }
90 if (buf[0] != 'P' || buf[1] != '6') {
91 *reason = "Unrecognized magic";
92 fclose(f);
93 return NULL;
94 }
95 return load_ppm_file(f, reason);
96 }
97
98 void
99 free_image(image *im)
100 {
101 zfree(im->buf);
102 zfree(im);
103 }
104
105 void
106 render_image(image *im, const double affine[6],
107 unsigned char *buf, int rowstride, int x0, int y0, int x1, int y1)
108 {
109 int y;
110 unsigned char *dest_line = buf;
111 int src_x0 = x0;
112
113 for (y = y0; y < y1; y++) {
114 int src_y = y;
115
116 if (src_y >= 0 && src_y < im->height) {
117 unsigned char *img_line = im->buf + src_y * im->rowstride;
118 int left_pad = -src_x0;
119 int img_run, img_off, right_pad;
120
121 if (left_pad > x1 - x0) left_pad = x1 - x0;
122 if (left_pad > 0) {
123 memset(dest_line, 255, 3 * left_pad);
124 } else left_pad = 0;
125 img_off = src_x0;
126 if (img_off < 0) img_off = 0;
127 img_run = x1 - x0 - left_pad;
128 if (img_run > im->width - img_off) img_run = im->width - img_off;
129 if (img_run > 0) {
130 memcpy(dest_line + 3 * left_pad, img_line + 3 * img_off, 3 * img_run);
131 } else img_run = 0;
132 right_pad = x1 - x0 - left_pad - img_run;
133 if (right_pad > 0) {
134 memset(dest_line + 3 * (left_pad + img_run), 255, 3 * right_pad);
135 }
136 } else {
137 memset(dest_line, 255, rowstride);
138 }
139 dest_line += rowstride;
140 }
141 }