FlexBook / Mbed 2 deprecated FlexBook171204a

Dependencies:   SDFileSystem app epson mbed msp430 pl tests

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pnm-utils.c Source File

pnm-utils.c

00001 /*
00002   Plastic Logic EPD project on MSP430
00003 
00004   Copyright (C) 2013, 2014 Plastic Logic Limited
00005 
00006   This program is free software: you can redistribute it and/or modify
00007   it under the terms of the GNU General Public License as published by
00008   the Free Software Foundation, either version 3 of the License, or
00009   (at your option) any later version.
00010 
00011   This program is distributed in the hope that it will be useful,
00012   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014   GNU General Public License for more details.
00015 
00016   You should have received a copy of the GNU General Public License
00017   along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 /*
00020  * pnm-utils.c -- Utilities for dealing with PNM format graphics files
00021  *
00022  * Authors:
00023  *   Nick Terry <nick.terry@plasticlogic.com>
00024  *   Guillaume Tucker <guillaume.tucker@plasticlogic.com>
00025  *
00026  * Utilities for dealing with PNM format graphics files
00027  * We are only interested in PGM and PBM
00028  *
00029  * Details of the format can be found at:
00030  * http://en.wikipedia.org/wiki/Netpbm_format
00031  * However, this seems to indicate that the header format is more free format
00032  * http://paulbourke.net/dataformats/ppm/
00033  * Lines terminated with cr and/or lf or just white space
00034  * Header example 1
00035  * P6 1024 788 255
00036  * Header example 2
00037  * P6
00038  * 1024 788
00039  * # A comment
00040  * 255
00041  * Header example 3
00042  * P3
00043  * 1024 # the image width
00044  * 788 # the image height
00045  * # A comment
00046  * 1023
00047  *
00048  *
00049  * P4
00050  * # Comment string
00051  * 24 7
00052  *
00053  * P5
00054  * # Comment string
00055  * 24 7
00056  * 15
00057  *
00058  */
00059 
00060 #include <stdlib.h>
00061 #include "assert.h"
00062 #include "ChaN/ff.h"
00063 #include "pnm-utils.h"
00064 
00065 int pnm_read_int32(FIL *pnm_file, int32_t *value)
00066 {
00067     UINT count;
00068     char ch;
00069     int digits = 0;
00070     int in_comment = 0;
00071     int found = 0;
00072     int32_t val = 0;
00073 
00074     assert(pnm_file != NULL);
00075     assert(value != NULL);
00076 
00077     while (!found &&
00078             (f_read(pnm_file,&ch,1,&count) == FR_OK) && count == 1)
00079     {
00080         switch (ch)
00081         {
00082             case '#':
00083                 in_comment = 1;
00084                 break;
00085             case ' ':
00086             case '\t':
00087             case '\r':
00088             case '\n':
00089                 if (!in_comment)
00090                 {
00091                     if (digits) {
00092                         found = 1;
00093                     }
00094                 }
00095                 if (ch == '\r' || ch == '\n')
00096                     in_comment = 0;
00097                 break;
00098             case '0': case '1': case '2': case '3': case '4':
00099             case '5': case '6': case '7': case '8': case '9':
00100                 if (!in_comment) {
00101                     val = val * 10 + (ch - '0');
00102                     digits++;
00103 
00104                     if (f_eof(pnm_file)) {
00105                         found = 1;
00106                     }
00107                 }
00108                 break;
00109             default:
00110                 break;
00111         }
00112     }
00113 
00114     if (!found)
00115         return -1;
00116 
00117     *value = val;
00118 
00119     return 0;
00120 }
00121 
00122 int pnm_read_header(FIL *pnm_file, struct pnm_header *hdr)
00123 {
00124     char buffer[2];
00125     UINT count;
00126 
00127     assert(pnm_file);
00128     assert(hdr);
00129 
00130     hdr->type = PNM_UNKNOWN;
00131 
00132     if (f_read(pnm_file,buffer,2,&count) != FR_OK)
00133         goto read_error;
00134 
00135     if (buffer[0] != 'P')
00136         goto format_error;
00137 
00138     if (buffer[1] == '4')
00139         hdr->type = PNM_BITMAP;
00140     else if (buffer[1] == '5')
00141         hdr->type = PNM_GREYSCALE;
00142     else
00143         goto format_error;
00144 
00145     hdr->max_gray = 1;
00146     hdr->width = pnm_read_int(pnm_file);
00147     hdr->height = pnm_read_int(pnm_file);
00148 
00149     if (hdr->type == PNM_GREYSCALE) {
00150         hdr->max_gray = pnm_read_int(pnm_file);
00151     }
00152     // check to see if any of the data items were not read correctly
00153     if (hdr->width <= 0 || hdr->height <= 0 || hdr->max_gray <= 0)
00154         goto format_error;
00155 
00156     // read pointer is now positioned at start of image data
00157     return 0;
00158 
00159 format_error:
00160 read_error:
00161     return -1;
00162 }