ppm.c

00001 /*
00002  * Copyright (c) 2006 Ondrej Palkovsky
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00029 #include <sys/types.h>
00030 #include <errno.h>
00031 
00032 #include "ppm.h"
00033 
00034 static void skip_whitespace(unsigned char **data)
00035 {
00036 retry:
00037         while (**data == ' ' || **data == '\t' || **data == '\n' ||
00038             **data == '\r')
00039                 (*data)++;
00040         if (**data == '#') {
00041                 while (1) {
00042                         if (**data == '\n' || **data == '\r')
00043                                 break;
00044                         (*data)++;
00045                 }
00046                 goto retry;
00047         }
00048 }
00049 
00050 static void read_num(unsigned char **data, unsigned int *num)
00051 {
00052         *num = 0;
00053         while (**data >= '0' && **data <= '9') {
00054                 *num *= 10;
00055                 *num += **data - '0';
00056                 (*data)++;
00057         }
00058 }
00059 
00060 int ppm_get_data(unsigned char *data, size_t dtsz, unsigned int *width,
00061     unsigned int *height)
00062 {
00063         /* Read magic */
00064         if (data[0] != 'P' || data[1] != '6')
00065                 return EINVAL;
00066 
00067         data+=2;
00068         skip_whitespace(&data);
00069         read_num(&data, width);
00070         skip_whitespace(&data);
00071         read_num(&data,height);
00072 
00073         return 0;
00074 }
00075 
00086 int ppm_draw(unsigned char *data, size_t datasz, unsigned int sx,
00087     unsigned int sy, unsigned int maxwidth, unsigned int maxheight,
00088     putpixel_cb_t putpixel, void *vport)
00089 {
00090         unsigned int width, height;
00091         unsigned int maxcolor;
00092         unsigned int i;
00093         unsigned int color;
00094         unsigned int coef;
00095         
00096         /* Read magic */
00097         if ((data[0] != 'P') || (data[1] != '6'))
00098                 return EINVAL;
00099         
00100         data += 2;
00101         skip_whitespace(&data);
00102         read_num(&data, &width);
00103         skip_whitespace(&data);
00104         read_num(&data, &height);
00105         skip_whitespace(&data);
00106         read_num(&data, &maxcolor);
00107         data++;
00108         
00109         if ((maxcolor == 0) || (maxcolor > 255) || (width * height > datasz))
00110                 return EINVAL;
00111         
00112         coef = 255 / maxcolor;
00113         if (coef * maxcolor > 255)
00114                 coef -= 1;
00115         
00116         for (i = 0; i < width * height; i++) {
00117                 /* Crop picture if we don't fit into region */
00118                 if (i % width > maxwidth || i / width > maxheight) {
00119                         data += 3;
00120                         continue;
00121                 }
00122                 color = ((data[0] * coef) << 16) + ((data[1] * coef) << 8) +
00123                     data[2] * coef;
00124                 
00125                 (*putpixel)(vport, sx + (i % width), sy + (i / width), color);
00126                 data += 3;
00127         }
00128         
00129         return 0;
00130 }

Generated on Thu Jun 2 07:45:49 2011 for HelenOS/USB by  doxygen 1.4.7