ppmtochrpicon.c revision 1.2.22.3 1 /* $NetBSD: ppmtochrpicon.c,v 1.2.22.3 2004/09/21 13:20:59 skrll Exp $ */
2
3 /*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lonhyn T. Jasinskyj.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Usage:
41 *
42 * ppmtochrpicon [ppmfile]
43 *
44 * This programs reads from either a single file given as an argument
45 * or from stdin if no args are given. It tries to find a <ICON...>
46 * tag in the file and read a CHRP style ASCII boot icon. It is not
47 * overly intelligent at dealing with other confusing stuff in the
48 * file or weird formatting due to the specialized nature of the input
49 * files.
50 *
51 * It then produces a PPM file on stdout containing the boot icon's image.
52 */
53
54 #include <sys/cdefs.h>
55 __RCSID("$NetBSD: ppmtochrpicon.c,v 1.2.22.3 2004/09/21 13:20:59 skrll Exp $");
56
57 #include <stdlib.h>
58
59 #include <pbm.h>
60 #include <ppm.h>
61
62 #include "chrpicon.h"
63
64 static char *hex_digits = "0123456789ABCDEF";
65
66 int
67 main(int argc, char *argv[])
68 {
69 FILE *ifp;
70 CHRPI_spec_rec img_rec;
71 CHRPI_spec img = &img_rec;
72 pixval maxval;
73 pixel **pixels;
74
75 ppm_init(&argc, argv);
76
77 if (argc > 2)
78 pm_usage("[ppmfile]");
79
80 /* either use stdin or open a file */
81 if (argc > 1)
82 ifp = pm_openr(argv[1]);
83 else
84 ifp = stdin;
85
86 /* use the img struct to conveniently store some parameters */
87 pixels = ppm_readppm(ifp, &img->width, &img->height, &maxval);
88
89 if (maxval != 255)
90 pm_error("can only use 8-bit per channel true-color "
91 "PPM files (maxval = 255)");
92
93 if (img->width != 64 || img->height != 64)
94 pm_message("CHRP only supports boot icons of "
95 "size 64x64, will truncate to fit");
96
97 img->width = img->height = 64;
98
99 /* the only supported color space is RGB = 3:3:2 */
100 img->rbits = img->gbits = 3;
101 img->bbits = 2;
102
103 pm_close(ifp);
104
105 CHRPI_writeicon(stdout, pixels, img);
106
107 return 0;
108 }
109
110 void
111 CHRPI_writeicon(FILE *fp, pixel **pixels, CHRPI_spec img)
112 {
113 CHRPI_putheader(stdout, img);
114 CHRPI_putbitmap(stdout, pixels, img);
115 CHRPI_putfooter(stdout, img);
116 }
117
118 void
119 CHRPI_putheader(FILE *fp, CHRPI_spec img)
120 {
121 fprintf(fp, "<ICON SIZE=%d,%d COLOR-SPACE=%d,%d,%d>\n<BITMAP>\n",
122 img->height, img->width,
123 img->rbits, img->gbits, img->bbits);
124 }
125
126 void
127 CHRPI_putfooter(FILE *fp, CHRPI_spec img)
128 {
129 fprintf(fp, "</BITMAP>\n</ICON>\n");
130 }
131
132
133 void
134 CHRPI_putbitmap(FILE *fp, pixel** pixels, CHRPI_spec img)
135 {
136 int row, col;
137 pixel *pP;
138
139 for (row = 0; row < img->height; row++) {
140
141 pixval r, g, b;
142 char pixbyte;
143
144 pP = pixels[row];
145
146 for (col = 0; col < img->width; col++) {
147
148 r = PPM_GETR(*pP);
149 g = PPM_GETG(*pP);
150 b = PPM_GETB(*pP);
151
152 pixbyte = (r & 0xe0) | ((g >> 3) & 0x1c) | ((b >> 6) & 0x03);
153
154 /* write the byte in hex */
155 fputc(hex_digits[(pixbyte>>4) & 0x0f], fp);
156 fputc(hex_digits[(pixbyte & 0x0f)], fp);
157 pP++;
158 }
159
160 fputc('\n', fp);
161 }
162 }
163