1 /* $NetBSD: ppmtochrpicon.c,v 1.2 2001/08/20 12:20:06 wiz 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 <stdlib.h> 55 56 #include <pbm.h> 57 #include <ppm.h> 58 59 #include "chrpicon.h" 60 61 static char *hex_digits = "0123456789ABCDEF"; 62 63 int 64 main(int argc, char *argv[]) 65 { 66 FILE *ifp; 67 CHRPI_spec_rec img_rec; 68 CHRPI_spec img = &img_rec; 69 pixval maxval; 70 pixel **pixels; 71 72 ppm_init(&argc, argv); 73 74 if (argc > 2) 75 pm_usage("[ppmfile]"); 76 77 /* either use stdin or open a file */ 78 if (argc > 1) 79 ifp = pm_openr(argv[1]); 80 else 81 ifp = stdin; 82 83 /* use the img struct to conveniently store some parameters */ 84 pixels = ppm_readppm(ifp, &img->width, &img->height, &maxval); 85 86 if (maxval != 255) 87 pm_error("can only use 8-bit per channel true-color " 88 "PPM files (maxval = 255)"); 89 90 if (img->width != 64 || img->height != 64) 91 pm_message("CHRP only supports boot icons of " 92 "size 64x64, will truncate to fit"); 93 94 img->width = img->height = 64; 95 96 /* the only supported color space is RGB = 3:3:2 */ 97 img->rbits = img->gbits = 3; 98 img->bbits = 2; 99 100 pm_close(ifp); 101 102 CHRPI_writeicon(stdout, pixels, img); 103 104 return 0; 105 } 106 107 void 108 CHRPI_writeicon(FILE *fp, pixel **pixels, CHRPI_spec img) 109 { 110 CHRPI_putheader(stdout, img); 111 CHRPI_putbitmap(stdout, pixels, img); 112 CHRPI_putfooter(stdout, img); 113 } 114 115 void 116 CHRPI_putheader(FILE *fp, CHRPI_spec img) 117 { 118 fprintf(fp, "<ICON SIZE=%d,%d COLOR-SPACE=%d,%d,%d>\n<BITMAP>\n", 119 img->height, img->width, 120 img->rbits, img->gbits, img->bbits); 121 } 122 123 void 124 CHRPI_putfooter(FILE *fp, CHRPI_spec img) 125 { 126 fprintf(fp, "</BITMAP>\n</ICON>\n"); 127 } 128 129 130 void 131 CHRPI_putbitmap(FILE *fp, pixel** pixels, CHRPI_spec img) 132 { 133 int row, col; 134 pixel *pP; 135 136 for (row = 0; row < img->height; row++) { 137 138 pixval r, g, b; 139 char pixbyte; 140 141 pP = pixels[row]; 142 143 for (col = 0; col < img->width; col++) { 144 145 r = PPM_GETR(*pP); 146 g = PPM_GETG(*pP); 147 b = PPM_GETB(*pP); 148 149 pixbyte = (r & 0xe0) | ((g >> 3) & 0x1c) | ((b >> 6) & 0x03); 150 151 /* write the byte in hex */ 152 fputc(hex_digits[(pixbyte>>4) & 0x0f], fp); 153 fputc(hex_digits[(pixbyte & 0x0f)], fp); 154 pP++; 155 } 156 157 fputc('\n', fp); 158 } 159 } 160