Home | History | Annotate | Line # | Download | only in ppmtochrpicon
      1 /*	$NetBSD: ppmtochrpicon.c,v 1.6 2008/04/28 20:23:33 martin 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  *
     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.
     30  */
     31 
     32 /*
     33  * Usage:
     34  *
     35  * ppmtochrpicon [ppmfile]
     36  *
     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.
     43  *
     44  * It then produces a PPM file on stdout containing the boot icon's image.
     45  */
     46 
     47 #include <sys/cdefs.h>
     48 __RCSID("$NetBSD: ppmtochrpicon.c,v 1.6 2008/04/28 20:23:33 martin Exp $");
     49 
     50 #include <stdlib.h>
     51 
     52 #include <pbm.h>
     53 #include <ppm.h>
     54 
     55 #include "chrpicon.h"
     56 
     57 static char *hex_digits = "0123456789ABCDEF";
     58 
     59 int
     60 main(int argc, char *argv[])
     61 {
     62     FILE *ifp;
     63     CHRPI_spec_rec img_rec;
     64     CHRPI_spec img = &img_rec;
     65     pixval maxval;
     66     pixel **pixels;
     67 
     68     ppm_init(&argc, argv);
     69 
     70     if (argc > 2)
     71 	pm_usage("[ppmfile]");
     72 
     73     /* either use stdin or open a file */
     74     if (argc > 1)
     75 	ifp = pm_openr(argv[1]);
     76     else
     77 	ifp = stdin;
     78 
     79     /* use the img struct to conveniently store some parameters */
     80     pixels = ppm_readppm(ifp, &img->width, &img->height, &maxval);
     81 
     82     if (maxval != 255)
     83         pm_error("can only use 8-bit per channel true-color "
     84                  "PPM files (maxval = 255)");
     85 
     86     if (img->width != 64 || img->height != 64)
     87         pm_message("CHRP only supports boot icons of "
     88                    "size 64x64, will truncate to fit");
     89 
     90     img->width = img->height = 64;
     91 
     92     /* the only supported color space is RGB = 3:3:2 */
     93     img->rbits = img->gbits = 3;
     94     img->bbits = 2;
     95 
     96     pm_close(ifp);
     97 
     98     CHRPI_writeicon(stdout, pixels, img);
     99 
    100     return 0;
    101 }
    102 
    103 void
    104 CHRPI_writeicon(FILE *fp, pixel **pixels, CHRPI_spec img)
    105 {
    106     CHRPI_putheader(stdout, img);
    107     CHRPI_putbitmap(stdout, pixels, img);
    108     CHRPI_putfooter(stdout, img);
    109 }
    110 
    111 void
    112 CHRPI_putheader(FILE *fp, CHRPI_spec img)
    113 {
    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);
    117 }
    118 
    119 void
    120 CHRPI_putfooter(FILE *fp, CHRPI_spec img)
    121 {
    122     fprintf(fp, "</bitmap>\n</icon>\n");
    123 }
    124 
    125 
    126 void
    127 CHRPI_putbitmap(FILE *fp, pixel** pixels, CHRPI_spec img)
    128 {
    129     int row, col;
    130     pixel *pP;
    131 
    132     for (row = 0; row < img->height; row++) {
    133 
    134         pixval r, g, b;
    135         char pixbyte;
    136 
    137         pP = pixels[row];
    138 
    139         for (col = 0; col < img->width; col++) {
    140 
    141             r = PPM_GETR(*pP);
    142             g = PPM_GETG(*pP);
    143             b = PPM_GETB(*pP);
    144 
    145             pixbyte = (r & 0xe0)  | ((g >> 3) & 0x1c) | ((b >> 6) & 0x03);
    146 
    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);
    154         }
    155 
    156     }
    157 }
    158