1 /* $NetBSD: chrpicontoppm.c,v 1.3 2005/12/11 12:18:47 christos Exp $ */
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lonhyn T. Jasinskyj.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * chrpicontoppm.c - read a CHRP style boot icon file and convert
40 * chrpicontoppm [chrpiconfile]
42 * This programs reads from either a single file given as an argument
43 * or from stdin if no args are given. It expects a true color
44 * PPM file as the input. The image should be 64x64, otherwise it
45 * is cropped to that size.
47 * It then produces a CHRP style boot icon file on stdout.
50 #include <sys/cdefs.h>
51 __RCSID("$NetBSD: chrpicontoppm.c,v 1.3 2005/12/11 12:18:47 christos Exp $");
62 main(int argc
, char *argv
[])
65 CHRPI_spec_rec img_rec
;
66 CHRPI_spec img
= &img_rec
;
74 ppm_init(&argc
, argv
);
77 pm_usage("[chrpiconfile]");
79 /* either use stdin or open a file */
81 if ((ifp
= fopen(argv
[1], "r")) == NULL
) {
82 perror("ppmfile open");
89 if (CHRPI_getheader(ifp
, img
))
90 pm_error("can't find <ICON...> header in boot icon file");
92 if (CHRPI_getbitmap(ifp
, img
))
93 pm_error("can't read <BITMAP...> section in boot icon file");
95 if (img
->rbits
!= 3 || img
->gbits
!= 3 || img
->bbits
!= 2)
96 pm_error("can only handle RGB 3:3:2 colorspace icon files");
98 ppm_writeppminit(stdout
, img
->width
, img
->height
, maxval
, PLAIN_PPM
);
99 pixelrow
= ppm_allocrow(img
->width
);
101 for (row
= 0; row
< img
->height
; row
++) {
106 imgP
= img
->pixels
[row
];
108 for (col
= 0; col
< img
->width
; col
++) {
110 r
= ((*imgP
>> 5) & 7);
111 g
= ((*imgP
>> 2) & 7);
114 r
= (r
<< 5) | (r
<< 2) | (r
>> 1);
115 g
= (g
<< 5) | (g
<< 2) | (g
>> 1);
116 b
= (b
<< 6) | (b
<< 4) | (b
>> 4) | b
;
118 PPM_ASSIGN(*pP
, r
, g
, b
);
124 ppm_writeppmrow(stdout
, pixelrow
, img
->width
, maxval
, PLAIN_PPM
);
127 ppm_freerow(pixelrow
);
136 CHRPI_allocrow(int cols
)
138 return calloc(cols
, sizeof(chrpi_pixel
));
142 CHRPI_getheader(FILE *fp
, CHRPI_spec img
)
144 char line
[MAX_LINE_LENGTH
+ 1];
146 while (fgets(line
, MAX_LINE_LENGTH
, fp
)) {
147 if (strstr(line
, ICON_TAG
)) {
148 /* found the ICON identifier, primitively parse it */
149 if (sscanf(line
, " %*s SIZE=%d,%d COLOR-SPACE=%d,%d,%d",
150 &img
->height
, &img
->width
,
151 &img
->rbits
, &img
->gbits
, &img
->bbits
164 CHRPI_getbitmap(FILE *fp
, CHRPI_spec img
)
166 char line
[MAX_LINE_LENGTH
+ 1];
168 char hexstr
[3] = { 0, 0, 0 };
173 /* first find the BITMAP tag */
174 while (fgets(line
, MAX_LINE_LENGTH
, fp
)) {
175 if (strncmp(line
, BITMAP_TAG
, strlen(BITMAP_TAG
)) == 0) {
184 if ((img
->pixels
= calloc(img
->height
, sizeof(chrpi_pixel
*))) == NULL
)
187 for (r
= 0; r
< img
->height
; r
++)
188 if ((img
->pixels
[r
] = CHRPI_allocrow(img
->width
)) == NULL
)
191 for (r
= 0; r
< img
->height
; r
++) {
194 if ((p
= fgets(line
, MAX_LINE_LENGTH
, fp
)) == NULL
) {
198 /* go down the pixels and convert them */
199 for (c
= 0; c
< img
->width
; c
++) {
203 img
->pixels
[r
][c
] = (chrpi_pixel
)(strtoul(hexstr
, NULL
, 16));