Home | History | Annotate | Line # | Download | only in ppmtochrpicon
      1  1.6   martin /*	$NetBSD: ppmtochrpicon.c,v 1.6 2008/04/28 20:23:33 martin 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  *
     19  1.1   lonhyn  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1   lonhyn  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1   lonhyn  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1   lonhyn  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1   lonhyn  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1   lonhyn  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1   lonhyn  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1   lonhyn  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1   lonhyn  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1   lonhyn  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1   lonhyn  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1   lonhyn  */
     31  1.1   lonhyn 
     32  1.1   lonhyn /*
     33  1.1   lonhyn  * Usage:
     34  1.1   lonhyn  *
     35  1.1   lonhyn  * ppmtochrpicon [ppmfile]
     36  1.1   lonhyn  *
     37  1.1   lonhyn  * This programs reads from either a single file given as an argument
     38  1.1   lonhyn  * or from stdin if no args are given. It tries to find a <ICON...>
     39  1.1   lonhyn  * tag in the file and read a CHRP style ASCII boot icon. It is not
     40  1.1   lonhyn  * overly intelligent at dealing with other confusing stuff in the
     41  1.2      wiz  * file or weird formatting due to the specialized nature of the input
     42  1.1   lonhyn  * files.
     43  1.1   lonhyn  *
     44  1.1   lonhyn  * It then produces a PPM file on stdout containing the boot icon's image.
     45  1.1   lonhyn  */
     46  1.3    lukem 
     47  1.3    lukem #include <sys/cdefs.h>
     48  1.6   martin __RCSID("$NetBSD: ppmtochrpicon.c,v 1.6 2008/04/28 20:23:33 martin Exp $");
     49  1.1   lonhyn 
     50  1.1   lonhyn #include <stdlib.h>
     51  1.1   lonhyn 
     52  1.1   lonhyn #include <pbm.h>
     53  1.1   lonhyn #include <ppm.h>
     54  1.1   lonhyn 
     55  1.1   lonhyn #include "chrpicon.h"
     56  1.1   lonhyn 
     57  1.1   lonhyn static char *hex_digits = "0123456789ABCDEF";
     58  1.1   lonhyn 
     59  1.1   lonhyn int
     60  1.1   lonhyn main(int argc, char *argv[])
     61  1.1   lonhyn {
     62  1.1   lonhyn     FILE *ifp;
     63  1.1   lonhyn     CHRPI_spec_rec img_rec;
     64  1.1   lonhyn     CHRPI_spec img = &img_rec;
     65  1.1   lonhyn     pixval maxval;
     66  1.1   lonhyn     pixel **pixels;
     67  1.1   lonhyn 
     68  1.1   lonhyn     ppm_init(&argc, argv);
     69  1.1   lonhyn 
     70  1.1   lonhyn     if (argc > 2)
     71  1.1   lonhyn 	pm_usage("[ppmfile]");
     72  1.1   lonhyn 
     73  1.1   lonhyn     /* either use stdin or open a file */
     74  1.1   lonhyn     if (argc > 1)
     75  1.1   lonhyn 	ifp = pm_openr(argv[1]);
     76  1.1   lonhyn     else
     77  1.1   lonhyn 	ifp = stdin;
     78  1.1   lonhyn 
     79  1.1   lonhyn     /* use the img struct to conveniently store some parameters */
     80  1.1   lonhyn     pixels = ppm_readppm(ifp, &img->width, &img->height, &maxval);
     81  1.1   lonhyn 
     82  1.1   lonhyn     if (maxval != 255)
     83  1.1   lonhyn         pm_error("can only use 8-bit per channel true-color "
     84  1.1   lonhyn                  "PPM files (maxval = 255)");
     85  1.1   lonhyn 
     86  1.1   lonhyn     if (img->width != 64 || img->height != 64)
     87  1.1   lonhyn         pm_message("CHRP only supports boot icons of "
     88  1.1   lonhyn                    "size 64x64, will truncate to fit");
     89  1.1   lonhyn 
     90  1.1   lonhyn     img->width = img->height = 64;
     91  1.1   lonhyn 
     92  1.1   lonhyn     /* the only supported color space is RGB = 3:3:2 */
     93  1.1   lonhyn     img->rbits = img->gbits = 3;
     94  1.1   lonhyn     img->bbits = 2;
     95  1.1   lonhyn 
     96  1.1   lonhyn     pm_close(ifp);
     97  1.1   lonhyn 
     98  1.1   lonhyn     CHRPI_writeicon(stdout, pixels, img);
     99  1.1   lonhyn 
    100  1.1   lonhyn     return 0;
    101  1.1   lonhyn }
    102  1.1   lonhyn 
    103  1.1   lonhyn void
    104  1.1   lonhyn CHRPI_writeicon(FILE *fp, pixel **pixels, CHRPI_spec img)
    105  1.1   lonhyn {
    106  1.1   lonhyn     CHRPI_putheader(stdout, img);
    107  1.1   lonhyn     CHRPI_putbitmap(stdout, pixels, img);
    108  1.1   lonhyn     CHRPI_putfooter(stdout, img);
    109  1.1   lonhyn }
    110  1.1   lonhyn 
    111  1.1   lonhyn void
    112  1.1   lonhyn CHRPI_putheader(FILE *fp, CHRPI_spec img)
    113  1.1   lonhyn {
    114  1.5  garbled     fprintf(fp, "<icon size=%d,%d color-space=%d,%d,%d>\n<bitmap>\n",
    115  1.1   lonhyn             img->height, img->width,
    116  1.1   lonhyn             img->rbits, img->gbits, img->bbits);
    117  1.1   lonhyn }
    118  1.1   lonhyn 
    119  1.1   lonhyn void
    120  1.1   lonhyn CHRPI_putfooter(FILE *fp, CHRPI_spec img)
    121  1.1   lonhyn {
    122  1.5  garbled     fprintf(fp, "</bitmap>\n</icon>\n");
    123  1.1   lonhyn }
    124  1.1   lonhyn 
    125  1.1   lonhyn 
    126  1.1   lonhyn void
    127  1.1   lonhyn CHRPI_putbitmap(FILE *fp, pixel** pixels, CHRPI_spec img)
    128  1.1   lonhyn {
    129  1.1   lonhyn     int row, col;
    130  1.1   lonhyn     pixel *pP;
    131  1.1   lonhyn 
    132  1.1   lonhyn     for (row = 0; row < img->height; row++) {
    133  1.1   lonhyn 
    134  1.1   lonhyn         pixval r, g, b;
    135  1.1   lonhyn         char pixbyte;
    136  1.1   lonhyn 
    137  1.1   lonhyn         pP = pixels[row];
    138  1.1   lonhyn 
    139  1.1   lonhyn         for (col = 0; col < img->width; col++) {
    140  1.1   lonhyn 
    141  1.1   lonhyn             r = PPM_GETR(*pP);
    142  1.1   lonhyn             g = PPM_GETG(*pP);
    143  1.1   lonhyn             b = PPM_GETB(*pP);
    144  1.1   lonhyn 
    145  1.1   lonhyn             pixbyte = (r & 0xe0)  | ((g >> 3) & 0x1c) | ((b >> 6) & 0x03);
    146  1.1   lonhyn 
    147  1.1   lonhyn             /* write the byte in hex */
    148  1.1   lonhyn             fputc(hex_digits[(pixbyte>>4) & 0x0f], fp);
    149  1.1   lonhyn             fputc(hex_digits[(pixbyte & 0x0f)], fp);
    150  1.5  garbled 	    fputc(' ', fp);
    151  1.1   lonhyn             pP++;
    152  1.5  garbled 	    if ((col+1)%16 == 0)
    153  1.5  garbled         	fputc('\n', fp);
    154  1.1   lonhyn         }
    155  1.1   lonhyn 
    156  1.1   lonhyn     }
    157  1.1   lonhyn }
    158