1 /* 2 Copyright (c) 2005-2021 Intel Corporation 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 /* 18 The original source for this example is 19 Copyright (c) 1994-2008 John E. Stone 20 All rights reserved. 21 22 Redistribution and use in source and binary forms, with or without 23 modification, are permitted provided that the following conditions 24 are met: 25 1. Redistributions of source code must retain the above copyright 26 notice, this list of conditions and the following disclaimer. 27 2. Redistributions in binary form must reproduce the above copyright 28 notice, this list of conditions and the following disclaimer in the 29 documentation and/or other materials provided with the distribution. 30 3. The name of the author may not be used to endorse or promote products 31 derived from this software without specific prior written permission. 32 33 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 34 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 35 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 36 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 37 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 38 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 39 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 40 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 41 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 42 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 43 SUCH DAMAGE. 44 */ 45 46 /* 47 * ppm.cpp - This file deals with PPM format image files (reading/writing) 48 */ 49 50 /* For our puposes, we're interested only in the 3 byte per pixel 24 bit 51 truecolor sort of file.. Probably won't implement any decent checking 52 at this point, probably choke on things like the # comments.. */ 53 54 // Try preventing lots of GCC warnings about ignored results of fscanf etc. 55 #ifdef __GNUC__ 56 #pragma GCC diagnostic ignored "-Wunused-result" 57 #endif 58 59 #include <cstdio> 60 61 #include "machine.hpp" 62 #include "types.hpp" 63 #include "util.hpp" 64 #include "imageio.hpp" /* error codes etc */ 65 #include "ppm.hpp" 66 67 static int getint(FILE *dfile) { 68 char ch[200]; 69 int i; 70 int num; 71 72 num = 0; 73 while (num == 0) { 74 fscanf(dfile, "%s", ch); 75 while (ch[0] == '#') { 76 fgets(ch, 200, dfile); 77 } 78 num = sscanf(ch, "%d", &i); 79 } 80 return i; 81 } 82 83 int readppm(char *name, int *xres, int *yres, unsigned char **imgdata) { 84 char data[200]; 85 FILE *ifp; 86 int i; 87 std::size_t bytesread; 88 int datasize; 89 90 ifp = fopen(name, "r"); 91 if (ifp == nullptr) { 92 return IMAGEBADFILE; /* couldn't open the file */ 93 } 94 fscanf(ifp, "%s", data); 95 96 if (strcmp(data, "P6")) { 97 fclose(ifp); 98 return IMAGEUNSUP; /* not a format we support */ 99 } 100 101 *xres = getint(ifp); 102 *yres = getint(ifp); 103 i = getint(ifp); /* eat the maxval number */ 104 fread(&i, 1, 1, ifp); /* eat the newline */ 105 datasize = 3 * (*xres) * (*yres); 106 107 *imgdata = (unsigned char *)rt_getmem(datasize); 108 109 bytesread = fread(*imgdata, 1, datasize, ifp); 110 111 fclose(ifp); 112 113 if (bytesread != datasize) 114 return IMAGEREADERR; 115 116 return IMAGENOERR; 117 } 118