Sync usage with man page.
[netbsd-mini2440.git] / sys / arch / powerpc / tools / chrpicon / ppmtochrpicon / ppmtochrpicon.c
blob00056b01793e161ab3ff895cd8bb6efd8be7a823
1 /* $NetBSD: ppmtochrpicon.c,v 1.5 2008/01/23 23:22:02 garbled Exp $ */
3 /*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
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
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.
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 * Usage:
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
42 * files.
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 $");
50 #include <stdlib.h>
52 #include <pbm.h>
53 #include <ppm.h>
55 #include "chrpicon.h"
57 static char *hex_digits = "0123456789ABCDEF";
59 int
60 main(int argc, char *argv[])
62 FILE *ifp;
63 CHRPI_spec_rec img_rec;
64 CHRPI_spec img = &img_rec;
65 pixval maxval;
66 pixel **pixels;
68 ppm_init(&argc, argv);
70 if (argc > 2)
71 pm_usage("[ppmfile]");
73 /* either use stdin or open a file */
74 if (argc > 1)
75 ifp = pm_openr(argv[1]);
76 else
77 ifp = stdin;
79 /* use the img struct to conveniently store some parameters */
80 pixels = ppm_readppm(ifp, &img->width, &img->height, &maxval);
82 if (maxval != 255)
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;
94 img->bbits = 2;
96 pm_close(ifp);
98 CHRPI_writeicon(stdout, pixels, img);
100 return 0;
103 void
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);
111 void
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);
119 void
120 CHRPI_putfooter(FILE *fp, CHRPI_spec img)
122 fprintf(fp, "</bitmap>\n</icon>\n");
126 void
127 CHRPI_putbitmap(FILE *fp, pixel** pixels, CHRPI_spec img)
129 int row, col;
130 pixel *pP;
132 for (row = 0; row < img->height; row++) {
134 pixval r, g, b;
135 char pixbyte;
137 pP = pixels[row];
139 for (col = 0; col < img->width; col++) {
141 r = PPM_GETR(*pP);
142 g = PPM_GETG(*pP);
143 b = PPM_GETB(*pP);
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);
150 fputc(' ', fp);
151 pP++;
152 if ((col+1)%16 == 0)
153 fputc('\n', fp);