Home | History | Annotate | Line # | Download | only in chrpicontoppm
chrpicontoppm.c revision 1.1.1.1.32.2
      1  1.1.1.1.32.2   skrll /*	$NetBSD: chrpicontoppm.c,v 1.1.1.1.32.2 2004/09/18 14:39:30 skrll 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  * 3. All advertising materials mentioning features or use of this software
     19           1.1  lonhyn  *    must display the following acknowledgement:
     20           1.1  lonhyn  *	This product includes software developed by the NetBSD
     21           1.1  lonhyn  *	Foundation, Inc. and its contributors.
     22           1.1  lonhyn  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23           1.1  lonhyn  *    contributors may be used to endorse or promote products derived
     24           1.1  lonhyn  *    from this software without specific prior written permission.
     25           1.1  lonhyn  *
     26           1.1  lonhyn  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27           1.1  lonhyn  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28           1.1  lonhyn  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29           1.1  lonhyn  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30           1.1  lonhyn  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31           1.1  lonhyn  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32           1.1  lonhyn  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33           1.1  lonhyn  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34           1.1  lonhyn  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35           1.1  lonhyn  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36           1.1  lonhyn  * POSSIBILITY OF SUCH DAMAGE.
     37           1.1  lonhyn  */
     38           1.1  lonhyn 
     39           1.1  lonhyn /*
     40           1.1  lonhyn  * chrpicontoppm.c - read a CHRP style boot icon file and convert
     41           1.1  lonhyn  * it to a PPM.
     42           1.1  lonhyn  */
     43           1.1  lonhyn 
     44           1.1  lonhyn /*
     45           1.1  lonhyn  * Usage:
     46           1.1  lonhyn  *
     47           1.1  lonhyn  * chrpicontoppm [chrpiconfile]
     48           1.1  lonhyn  *
     49           1.1  lonhyn  * This programs reads from either a single file given as an argument
     50           1.1  lonhyn  * or from stdin if no args are given. It expects a true color
     51           1.1  lonhyn  * PPM file as the input. The image should be 64x64, otherwise it
     52           1.1  lonhyn  * is cropped to that size.
     53           1.1  lonhyn  *
     54           1.1  lonhyn  * It then produces a CHRP style boot icon file on stdout.
     55           1.1  lonhyn  */
     56           1.1  lonhyn 
     57  1.1.1.1.32.1   skrll #include <sys/cdefs.h>
     58  1.1.1.1.32.2   skrll __RCSID("$NetBSD: chrpicontoppm.c,v 1.1.1.1.32.2 2004/09/18 14:39:30 skrll Exp $");
     59  1.1.1.1.32.1   skrll 
     60           1.1  lonhyn #include <stdlib.h>
     61           1.1  lonhyn 
     62           1.1  lonhyn #include <pbm.h>
     63           1.1  lonhyn #include <ppm.h>
     64           1.1  lonhyn 
     65           1.1  lonhyn #include "chrpicon.h"
     66           1.1  lonhyn 
     67           1.1  lonhyn 
     68           1.1  lonhyn int
     69           1.1  lonhyn main(int argc, char *argv[])
     70           1.1  lonhyn {
     71           1.1  lonhyn     FILE *ifp;
     72           1.1  lonhyn     CHRPI_spec_rec img_rec;
     73           1.1  lonhyn     CHRPI_spec img = &img_rec;
     74           1.1  lonhyn     pixel *pixelrow;
     75           1.1  lonhyn     pixel *pP;
     76           1.1  lonhyn     chrpi_pixel *imgP;
     77           1.1  lonhyn     int row, col;
     78           1.1  lonhyn     pixval maxval = 255;
     79           1.1  lonhyn 
     80           1.1  lonhyn 
     81           1.1  lonhyn     ppm_init(&argc, argv);
     82           1.1  lonhyn 
     83           1.1  lonhyn     if (argc > 2)
     84           1.1  lonhyn 	pm_usage("[chrpiconfile]");
     85           1.1  lonhyn 
     86           1.1  lonhyn     /* either use stdin or open a file */
     87           1.1  lonhyn     if (argc > 1) {
     88           1.1  lonhyn 	if ((ifp = fopen(argv[1], "r")) == NULL) {
     89           1.1  lonhyn             perror("ppmfile open");
     90           1.1  lonhyn             exit(1);
     91           1.1  lonhyn         }
     92           1.1  lonhyn     }
     93           1.1  lonhyn     else
     94           1.1  lonhyn 	ifp = stdin;
     95           1.1  lonhyn 
     96           1.1  lonhyn     if (CHRPI_getheader(ifp, img))
     97           1.1  lonhyn 	pm_error("can't find <ICON...> header in boot icon file");
     98           1.1  lonhyn 
     99           1.1  lonhyn     if (CHRPI_getbitmap(ifp, img))
    100           1.1  lonhyn 	pm_error("can't read <BITMAP...> section in boot icon file");
    101           1.1  lonhyn 
    102           1.1  lonhyn     if (img->rbits != 3 || img->gbits != 3 || img->bbits != 2)
    103           1.1  lonhyn 	pm_error("can only handle RGB 3:3:2 colorspace icon files");
    104           1.1  lonhyn 
    105           1.1  lonhyn     ppm_writeppminit(stdout, img->width, img->height, maxval, PLAIN_PPM);
    106           1.1  lonhyn     pixelrow = ppm_allocrow(img->width);
    107           1.1  lonhyn 
    108           1.1  lonhyn     for (row = 0; row < img->height; row++) {
    109           1.1  lonhyn 
    110           1.1  lonhyn         pixval r, g, b;
    111           1.1  lonhyn 
    112           1.1  lonhyn         pP = pixelrow;
    113           1.1  lonhyn         imgP = img->pixels[row];
    114           1.1  lonhyn 
    115           1.1  lonhyn         for (col = 0; col < img->width; col++) {
    116           1.1  lonhyn 
    117           1.1  lonhyn             r = ((*imgP >> 5) & 7);
    118           1.1  lonhyn             g = ((*imgP >> 2) & 7);
    119           1.1  lonhyn             b = (*imgP & 3);
    120           1.1  lonhyn 
    121           1.1  lonhyn             r = (r << 5) | (r << 2) | (r >> 1);
    122           1.1  lonhyn             g = (g << 5) | (g << 2) | (g >> 1);
    123           1.1  lonhyn             b = (b << 6) | (b << 4) | (b >> 4) | b;
    124           1.1  lonhyn 
    125           1.1  lonhyn             PPM_ASSIGN(*pP, r, g, b);
    126           1.1  lonhyn 
    127           1.1  lonhyn             pP++;
    128           1.1  lonhyn             imgP++;
    129           1.1  lonhyn         }
    130           1.1  lonhyn 
    131           1.1  lonhyn         ppm_writeppmrow(stdout, pixelrow, img->width, maxval, PLAIN_PPM);
    132           1.1  lonhyn     }
    133           1.1  lonhyn 
    134           1.1  lonhyn     ppm_freerow(pixelrow);
    135           1.1  lonhyn 
    136           1.1  lonhyn     pm_close(ifp);
    137           1.1  lonhyn     pm_close(stdout);
    138           1.1  lonhyn     exit(0);
    139           1.1  lonhyn }
    140           1.1  lonhyn 
    141           1.1  lonhyn 
    142           1.1  lonhyn chrpi_pixel *
    143           1.1  lonhyn CHRPI_allocrow(int cols)
    144           1.1  lonhyn {
    145           1.1  lonhyn     return calloc(cols, sizeof(chrpi_pixel));
    146           1.1  lonhyn }
    147           1.1  lonhyn 
    148           1.1  lonhyn int
    149           1.1  lonhyn CHRPI_getheader(FILE *fp, CHRPI_spec img)
    150           1.1  lonhyn {
    151           1.1  lonhyn     char line[MAX_LINE_LENGTH + 1];
    152           1.1  lonhyn 
    153           1.1  lonhyn     while (fgets(line, MAX_LINE_LENGTH, fp)) {
    154           1.1  lonhyn         if (strstr(line, ICON_TAG)) {
    155           1.1  lonhyn             /* found the ICON identifier, primitively parse it */
    156           1.1  lonhyn             if (sscanf(line, " %*s SIZE=%d,%d COLOR-SPACE=%d,%d,%d",
    157           1.1  lonhyn                        &img->height, &img->width,
    158           1.1  lonhyn                        &img->rbits, &img->gbits, &img->bbits
    159           1.1  lonhyn                        ) != 5)
    160           1.1  lonhyn                 return -1;
    161           1.1  lonhyn 
    162           1.1  lonhyn             return 0;
    163           1.1  lonhyn         }
    164           1.1  lonhyn     }
    165           1.1  lonhyn 
    166           1.1  lonhyn     return -1;
    167           1.1  lonhyn }
    168           1.1  lonhyn 
    169           1.1  lonhyn 
    170           1.1  lonhyn int
    171           1.1  lonhyn CHRPI_getbitmap(FILE *fp, CHRPI_spec img)
    172           1.1  lonhyn {
    173           1.1  lonhyn     char line[MAX_LINE_LENGTH + 1];
    174           1.1  lonhyn     int foundtag = 0;
    175           1.1  lonhyn     char hexstr[3] = { 0, 0, 0 };
    176           1.1  lonhyn     char *p;
    177           1.1  lonhyn     int r, c;
    178           1.1  lonhyn 
    179           1.1  lonhyn 
    180           1.1  lonhyn     /* first find the BITMAP tag */
    181           1.1  lonhyn     while (fgets(line, MAX_LINE_LENGTH, fp)) {
    182           1.1  lonhyn         if (strncmp(line, BITMAP_TAG, strlen(BITMAP_TAG)) == 0) {
    183           1.1  lonhyn             foundtag++;
    184           1.1  lonhyn             break;
    185           1.1  lonhyn         }
    186           1.1  lonhyn     }
    187           1.1  lonhyn 
    188           1.1  lonhyn     if (!foundtag)
    189           1.1  lonhyn         return -1;
    190           1.1  lonhyn 
    191           1.1  lonhyn     if ((img->pixels = calloc(img->height, sizeof(chrpi_pixel *))) == NULL)
    192           1.1  lonhyn         return -1;
    193           1.1  lonhyn 
    194           1.1  lonhyn     for (r = 0; r < img->height; r++)
    195           1.1  lonhyn         if ((img->pixels[r] = CHRPI_allocrow(img->width)) == NULL)
    196           1.1  lonhyn             return -1;
    197           1.1  lonhyn 
    198           1.1  lonhyn     for (r = 0; r < img->height; r++) {
    199           1.1  lonhyn 
    200           1.1  lonhyn         /* get a row */
    201           1.1  lonhyn         if ((p = fgets(line, MAX_LINE_LENGTH, fp)) == NULL) {
    202           1.1  lonhyn             return -1;
    203           1.1  lonhyn         }
    204           1.1  lonhyn 
    205           1.1  lonhyn         /* go down the pixels and convert them */
    206           1.1  lonhyn         for (c = 0; c < img->width; c++) {
    207           1.1  lonhyn             hexstr[0] = *p++;
    208           1.1  lonhyn             hexstr[1] = *p++;
    209           1.1  lonhyn 
    210           1.1  lonhyn             img->pixels[r][c] = (chrpi_pixel)(strtoul(hexstr, NULL, 16));
    211           1.1  lonhyn         }
    212           1.1  lonhyn     }
    213           1.1  lonhyn 
    214           1.1  lonhyn     return 0;
    215           1.1  lonhyn }
    216