1 /* $NetBSD: ppmtochrpicon.c,v 1.5 2008/01/23 23:22:02 garbled 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.
35 * ppmtochrpicon [ppmfile]
37 * This programs reads from either a single file given as an argument
38 * or from stdin if no args are given. It tries to find a <ICON...>
39 * tag in the file and read a CHRP style ASCII boot icon. It is not
40 * overly intelligent at dealing with other confusing stuff in the
41 * file or weird formatting due to the specialized nature of the input
44 * It then produces a PPM file on stdout containing the boot icon's image.
47 #include <sys/cdefs.h>
48 __RCSID("$NetBSD: ppmtochrpicon.c,v 1.5 2008/01/23 23:22:02 garbled Exp $");
57 static char *hex_digits
= "0123456789ABCDEF";
60 main(int argc
, char *argv
[])
63 CHRPI_spec_rec img_rec
;
64 CHRPI_spec img
= &img_rec
;
68 ppm_init(&argc
, argv
);
71 pm_usage("[ppmfile]");
73 /* either use stdin or open a file */
75 ifp
= pm_openr(argv
[1]);
79 /* use the img struct to conveniently store some parameters */
80 pixels
= ppm_readppm(ifp
, &img
->width
, &img
->height
, &maxval
);
83 pm_error("can only use 8-bit per channel true-color "
84 "PPM files (maxval = 255)");
86 if (img
->width
!= 64 || img
->height
!= 64)
87 pm_message("CHRP only supports boot icons of "
88 "size 64x64, will truncate to fit");
90 img
->width
= img
->height
= 64;
92 /* the only supported color space is RGB = 3:3:2 */
93 img
->rbits
= img
->gbits
= 3;
98 CHRPI_writeicon(stdout
, pixels
, img
);
104 CHRPI_writeicon(FILE *fp
, pixel
**pixels
, CHRPI_spec img
)
106 CHRPI_putheader(stdout
, img
);
107 CHRPI_putbitmap(stdout
, pixels
, img
);
108 CHRPI_putfooter(stdout
, img
);
112 CHRPI_putheader(FILE *fp
, CHRPI_spec img
)
114 fprintf(fp
, "<icon size=%d,%d color-space=%d,%d,%d>\n<bitmap>\n",
115 img
->height
, img
->width
,
116 img
->rbits
, img
->gbits
, img
->bbits
);
120 CHRPI_putfooter(FILE *fp
, CHRPI_spec img
)
122 fprintf(fp
, "</bitmap>\n</icon>\n");
127 CHRPI_putbitmap(FILE *fp
, pixel
** pixels
, CHRPI_spec img
)
132 for (row
= 0; row
< img
->height
; row
++) {
139 for (col
= 0; col
< img
->width
; col
++) {
145 pixbyte
= (r
& 0xe0) | ((g
>> 3) & 0x1c) | ((b
>> 6) & 0x03);
147 /* write the byte in hex */
148 fputc(hex_digits
[(pixbyte
>>4) & 0x0f], fp
);
149 fputc(hex_digits
[(pixbyte
& 0x0f)], fp
);