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