chrpicontoppm.c revision 1.1.1.1.8.2 1 1.1.1.1.8.2 bouyer /* $NetBSD: chrpicontoppm.c,v 1.1.1.1.8.2 2000/11/20 20:31:22 bouyer Exp $ */
2 1.1.1.1.8.2 bouyer
3 1.1.1.1.8.2 bouyer /*-
4 1.1.1.1.8.2 bouyer * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 1.1.1.1.8.2 bouyer * All rights reserved.
6 1.1.1.1.8.2 bouyer *
7 1.1.1.1.8.2 bouyer * This code is derived from software contributed to The NetBSD Foundation
8 1.1.1.1.8.2 bouyer * by Lonhyn T. Jasinskyj.
9 1.1.1.1.8.2 bouyer *
10 1.1.1.1.8.2 bouyer * Redistribution and use in source and binary forms, with or without
11 1.1.1.1.8.2 bouyer * modification, are permitted provided that the following conditions
12 1.1.1.1.8.2 bouyer * are met:
13 1.1.1.1.8.2 bouyer * 1. Redistributions of source code must retain the above copyright
14 1.1.1.1.8.2 bouyer * notice, this list of conditions and the following disclaimer.
15 1.1.1.1.8.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.1.1.8.2 bouyer * notice, this list of conditions and the following disclaimer in the
17 1.1.1.1.8.2 bouyer * documentation and/or other materials provided with the distribution.
18 1.1.1.1.8.2 bouyer * 3. All advertising materials mentioning features or use of this software
19 1.1.1.1.8.2 bouyer * must display the following acknowledgement:
20 1.1.1.1.8.2 bouyer * This product includes software developed by the NetBSD
21 1.1.1.1.8.2 bouyer * Foundation, Inc. and its contributors.
22 1.1.1.1.8.2 bouyer * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1.1.1.8.2 bouyer * contributors may be used to endorse or promote products derived
24 1.1.1.1.8.2 bouyer * from this software without specific prior written permission.
25 1.1.1.1.8.2 bouyer *
26 1.1.1.1.8.2 bouyer * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1.1.1.8.2 bouyer * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1.1.1.8.2 bouyer * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1.1.1.8.2 bouyer * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1.1.1.8.2 bouyer * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1.1.1.8.2 bouyer * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1.1.1.8.2 bouyer * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1.1.1.8.2 bouyer * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1.1.1.8.2 bouyer * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1.1.1.8.2 bouyer * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1.1.1.8.2 bouyer * POSSIBILITY OF SUCH DAMAGE.
37 1.1.1.1.8.2 bouyer */
38 1.1.1.1.8.2 bouyer
39 1.1.1.1.8.2 bouyer /*
40 1.1.1.1.8.2 bouyer * chrpicontoppm.c - read a CHRP style boot icon file and convert
41 1.1.1.1.8.2 bouyer * it to a PPM.
42 1.1.1.1.8.2 bouyer */
43 1.1.1.1.8.2 bouyer
44 1.1.1.1.8.2 bouyer /*
45 1.1.1.1.8.2 bouyer * Usage:
46 1.1.1.1.8.2 bouyer *
47 1.1.1.1.8.2 bouyer * chrpicontoppm [chrpiconfile]
48 1.1.1.1.8.2 bouyer *
49 1.1.1.1.8.2 bouyer * This programs reads from either a single file given as an argument
50 1.1.1.1.8.2 bouyer * or from stdin if no args are given. It expects a true color
51 1.1.1.1.8.2 bouyer * PPM file as the input. The image should be 64x64, otherwise it
52 1.1.1.1.8.2 bouyer * is cropped to that size.
53 1.1.1.1.8.2 bouyer *
54 1.1.1.1.8.2 bouyer * It then produces a CHRP style boot icon file on stdout.
55 1.1.1.1.8.2 bouyer */
56 1.1.1.1.8.2 bouyer
57 1.1.1.1.8.2 bouyer #include <stdlib.h>
58 1.1.1.1.8.2 bouyer
59 1.1.1.1.8.2 bouyer #include <pbm.h>
60 1.1.1.1.8.2 bouyer #include <ppm.h>
61 1.1.1.1.8.2 bouyer
62 1.1.1.1.8.2 bouyer #include "chrpicon.h"
63 1.1.1.1.8.2 bouyer
64 1.1.1.1.8.2 bouyer
65 1.1.1.1.8.2 bouyer int
66 1.1.1.1.8.2 bouyer main(int argc, char *argv[])
67 1.1.1.1.8.2 bouyer {
68 1.1.1.1.8.2 bouyer FILE *ifp;
69 1.1.1.1.8.2 bouyer CHRPI_spec_rec img_rec;
70 1.1.1.1.8.2 bouyer CHRPI_spec img = &img_rec;
71 1.1.1.1.8.2 bouyer pixel *pixelrow;
72 1.1.1.1.8.2 bouyer pixel *pP;
73 1.1.1.1.8.2 bouyer chrpi_pixel *imgP;
74 1.1.1.1.8.2 bouyer int row, col;
75 1.1.1.1.8.2 bouyer pixval maxval = 255;
76 1.1.1.1.8.2 bouyer
77 1.1.1.1.8.2 bouyer
78 1.1.1.1.8.2 bouyer ppm_init(&argc, argv);
79 1.1.1.1.8.2 bouyer
80 1.1.1.1.8.2 bouyer if (argc > 2)
81 1.1.1.1.8.2 bouyer pm_usage("[chrpiconfile]");
82 1.1.1.1.8.2 bouyer
83 1.1.1.1.8.2 bouyer /* either use stdin or open a file */
84 1.1.1.1.8.2 bouyer if (argc > 1) {
85 1.1.1.1.8.2 bouyer if ((ifp = fopen(argv[1], "r")) == NULL) {
86 1.1.1.1.8.2 bouyer perror("ppmfile open");
87 1.1.1.1.8.2 bouyer exit(1);
88 1.1.1.1.8.2 bouyer }
89 1.1.1.1.8.2 bouyer }
90 1.1.1.1.8.2 bouyer else
91 1.1.1.1.8.2 bouyer ifp = stdin;
92 1.1.1.1.8.2 bouyer
93 1.1.1.1.8.2 bouyer if (CHRPI_getheader(ifp, img))
94 1.1.1.1.8.2 bouyer pm_error("can't find <ICON...> header in boot icon file");
95 1.1.1.1.8.2 bouyer
96 1.1.1.1.8.2 bouyer if (CHRPI_getbitmap(ifp, img))
97 1.1.1.1.8.2 bouyer pm_error("can't read <BITMAP...> section in boot icon file");
98 1.1.1.1.8.2 bouyer
99 1.1.1.1.8.2 bouyer if (img->rbits != 3 || img->gbits != 3 || img->bbits != 2)
100 1.1.1.1.8.2 bouyer pm_error("can only handle RGB 3:3:2 colorspace icon files");
101 1.1.1.1.8.2 bouyer
102 1.1.1.1.8.2 bouyer ppm_writeppminit(stdout, img->width, img->height, maxval, PLAIN_PPM);
103 1.1.1.1.8.2 bouyer pixelrow = ppm_allocrow(img->width);
104 1.1.1.1.8.2 bouyer
105 1.1.1.1.8.2 bouyer for (row = 0; row < img->height; row++) {
106 1.1.1.1.8.2 bouyer
107 1.1.1.1.8.2 bouyer pixval r, g, b;
108 1.1.1.1.8.2 bouyer
109 1.1.1.1.8.2 bouyer pP = pixelrow;
110 1.1.1.1.8.2 bouyer imgP = img->pixels[row];
111 1.1.1.1.8.2 bouyer
112 1.1.1.1.8.2 bouyer for (col = 0; col < img->width; col++) {
113 1.1.1.1.8.2 bouyer
114 1.1.1.1.8.2 bouyer r = ((*imgP >> 5) & 7);
115 1.1.1.1.8.2 bouyer g = ((*imgP >> 2) & 7);
116 1.1.1.1.8.2 bouyer b = (*imgP & 3);
117 1.1.1.1.8.2 bouyer
118 1.1.1.1.8.2 bouyer r = (r << 5) | (r << 2) | (r >> 1);
119 1.1.1.1.8.2 bouyer g = (g << 5) | (g << 2) | (g >> 1);
120 1.1.1.1.8.2 bouyer b = (b << 6) | (b << 4) | (b >> 4) | b;
121 1.1.1.1.8.2 bouyer
122 1.1.1.1.8.2 bouyer PPM_ASSIGN(*pP, r, g, b);
123 1.1.1.1.8.2 bouyer
124 1.1.1.1.8.2 bouyer pP++;
125 1.1.1.1.8.2 bouyer imgP++;
126 1.1.1.1.8.2 bouyer }
127 1.1.1.1.8.2 bouyer
128 1.1.1.1.8.2 bouyer ppm_writeppmrow(stdout, pixelrow, img->width, maxval, PLAIN_PPM);
129 1.1.1.1.8.2 bouyer }
130 1.1.1.1.8.2 bouyer
131 1.1.1.1.8.2 bouyer ppm_freerow(pixelrow);
132 1.1.1.1.8.2 bouyer
133 1.1.1.1.8.2 bouyer pm_close(ifp);
134 1.1.1.1.8.2 bouyer pm_close(stdout);
135 1.1.1.1.8.2 bouyer exit(0);
136 1.1.1.1.8.2 bouyer }
137 1.1.1.1.8.2 bouyer
138 1.1.1.1.8.2 bouyer
139 1.1.1.1.8.2 bouyer chrpi_pixel *
140 1.1.1.1.8.2 bouyer CHRPI_allocrow(int cols)
141 1.1.1.1.8.2 bouyer {
142 1.1.1.1.8.2 bouyer return calloc(cols, sizeof(chrpi_pixel));
143 1.1.1.1.8.2 bouyer }
144 1.1.1.1.8.2 bouyer
145 1.1.1.1.8.2 bouyer int
146 1.1.1.1.8.2 bouyer CHRPI_getheader(FILE *fp, CHRPI_spec img)
147 1.1.1.1.8.2 bouyer {
148 1.1.1.1.8.2 bouyer char line[MAX_LINE_LENGTH + 1];
149 1.1.1.1.8.2 bouyer
150 1.1.1.1.8.2 bouyer while (fgets(line, MAX_LINE_LENGTH, fp)) {
151 1.1.1.1.8.2 bouyer if (strstr(line, ICON_TAG)) {
152 1.1.1.1.8.2 bouyer /* found the ICON identifier, primitively parse it */
153 1.1.1.1.8.2 bouyer if (sscanf(line, " %*s SIZE=%d,%d COLOR-SPACE=%d,%d,%d",
154 1.1.1.1.8.2 bouyer &img->height, &img->width,
155 1.1.1.1.8.2 bouyer &img->rbits, &img->gbits, &img->bbits
156 1.1.1.1.8.2 bouyer ) != 5)
157 1.1.1.1.8.2 bouyer return -1;
158 1.1.1.1.8.2 bouyer
159 1.1.1.1.8.2 bouyer return 0;
160 1.1.1.1.8.2 bouyer }
161 1.1.1.1.8.2 bouyer }
162 1.1.1.1.8.2 bouyer
163 1.1.1.1.8.2 bouyer return -1;
164 1.1.1.1.8.2 bouyer }
165 1.1.1.1.8.2 bouyer
166 1.1.1.1.8.2 bouyer
167 1.1.1.1.8.2 bouyer int
168 1.1.1.1.8.2 bouyer CHRPI_getbitmap(FILE *fp, CHRPI_spec img)
169 1.1.1.1.8.2 bouyer {
170 1.1.1.1.8.2 bouyer char line[MAX_LINE_LENGTH + 1];
171 1.1.1.1.8.2 bouyer int foundtag = 0;
172 1.1.1.1.8.2 bouyer char hexstr[3] = { 0, 0, 0 };
173 1.1.1.1.8.2 bouyer char *p;
174 1.1.1.1.8.2 bouyer int r, c;
175 1.1.1.1.8.2 bouyer
176 1.1.1.1.8.2 bouyer
177 1.1.1.1.8.2 bouyer /* first find the BITMAP tag */
178 1.1.1.1.8.2 bouyer while (fgets(line, MAX_LINE_LENGTH, fp)) {
179 1.1.1.1.8.2 bouyer if (strncmp(line, BITMAP_TAG, strlen(BITMAP_TAG)) == 0) {
180 1.1.1.1.8.2 bouyer foundtag++;
181 1.1.1.1.8.2 bouyer break;
182 1.1.1.1.8.2 bouyer }
183 1.1.1.1.8.2 bouyer }
184 1.1.1.1.8.2 bouyer
185 1.1.1.1.8.2 bouyer if (!foundtag)
186 1.1.1.1.8.2 bouyer return -1;
187 1.1.1.1.8.2 bouyer
188 1.1.1.1.8.2 bouyer if ((img->pixels = calloc(img->height, sizeof(chrpi_pixel *))) == NULL)
189 1.1.1.1.8.2 bouyer return -1;
190 1.1.1.1.8.2 bouyer
191 1.1.1.1.8.2 bouyer for (r = 0; r < img->height; r++)
192 1.1.1.1.8.2 bouyer if ((img->pixels[r] = CHRPI_allocrow(img->width)) == NULL)
193 1.1.1.1.8.2 bouyer return -1;
194 1.1.1.1.8.2 bouyer
195 1.1.1.1.8.2 bouyer for (r = 0; r < img->height; r++) {
196 1.1.1.1.8.2 bouyer
197 1.1.1.1.8.2 bouyer /* get a row */
198 1.1.1.1.8.2 bouyer if ((p = fgets(line, MAX_LINE_LENGTH, fp)) == NULL) {
199 1.1.1.1.8.2 bouyer return -1;
200 1.1.1.1.8.2 bouyer }
201 1.1.1.1.8.2 bouyer
202 1.1.1.1.8.2 bouyer /* go down the pixels and convert them */
203 1.1.1.1.8.2 bouyer for (c = 0; c < img->width; c++) {
204 1.1.1.1.8.2 bouyer hexstr[0] = *p++;
205 1.1.1.1.8.2 bouyer hexstr[1] = *p++;
206 1.1.1.1.8.2 bouyer
207 1.1.1.1.8.2 bouyer img->pixels[r][c] = (chrpi_pixel)(strtoul(hexstr, NULL, 16));
208 1.1.1.1.8.2 bouyer }
209 1.1.1.1.8.2 bouyer }
210 1.1.1.1.8.2 bouyer
211 1.1.1.1.8.2 bouyer return 0;
212 1.1.1.1.8.2 bouyer }
213