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