Added HangmanGame class, but does not work yet
Dependencies: SDFileSystem app epson mbed msp430 pl tests
pnm-utils.c@0:fa7450a43b99, 2017-12-04 (annotated)
- Committer:
- group-FlexBook
- Date:
- Mon Dec 04 09:32:20 2017 +0000
- Revision:
- 0:fa7450a43b99
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
group-FlexBook | 0:fa7450a43b99 | 1 | /* |
group-FlexBook | 0:fa7450a43b99 | 2 | Plastic Logic EPD project on MSP430 |
group-FlexBook | 0:fa7450a43b99 | 3 | |
group-FlexBook | 0:fa7450a43b99 | 4 | Copyright (C) 2013, 2014 Plastic Logic Limited |
group-FlexBook | 0:fa7450a43b99 | 5 | |
group-FlexBook | 0:fa7450a43b99 | 6 | This program is free software: you can redistribute it and/or modify |
group-FlexBook | 0:fa7450a43b99 | 7 | it under the terms of the GNU General Public License as published by |
group-FlexBook | 0:fa7450a43b99 | 8 | the Free Software Foundation, either version 3 of the License, or |
group-FlexBook | 0:fa7450a43b99 | 9 | (at your option) any later version. |
group-FlexBook | 0:fa7450a43b99 | 10 | |
group-FlexBook | 0:fa7450a43b99 | 11 | This program is distributed in the hope that it will be useful, |
group-FlexBook | 0:fa7450a43b99 | 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
group-FlexBook | 0:fa7450a43b99 | 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
group-FlexBook | 0:fa7450a43b99 | 14 | GNU General Public License for more details. |
group-FlexBook | 0:fa7450a43b99 | 15 | |
group-FlexBook | 0:fa7450a43b99 | 16 | You should have received a copy of the GNU General Public License |
group-FlexBook | 0:fa7450a43b99 | 17 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
group-FlexBook | 0:fa7450a43b99 | 18 | */ |
group-FlexBook | 0:fa7450a43b99 | 19 | /* |
group-FlexBook | 0:fa7450a43b99 | 20 | * pnm-utils.c -- Utilities for dealing with PNM format graphics files |
group-FlexBook | 0:fa7450a43b99 | 21 | * |
group-FlexBook | 0:fa7450a43b99 | 22 | * Authors: |
group-FlexBook | 0:fa7450a43b99 | 23 | * Nick Terry <nick.terry@plasticlogic.com> |
group-FlexBook | 0:fa7450a43b99 | 24 | * Guillaume Tucker <guillaume.tucker@plasticlogic.com> |
group-FlexBook | 0:fa7450a43b99 | 25 | * |
group-FlexBook | 0:fa7450a43b99 | 26 | * Utilities for dealing with PNM format graphics files |
group-FlexBook | 0:fa7450a43b99 | 27 | * We are only interested in PGM and PBM |
group-FlexBook | 0:fa7450a43b99 | 28 | * |
group-FlexBook | 0:fa7450a43b99 | 29 | * Details of the format can be found at: |
group-FlexBook | 0:fa7450a43b99 | 30 | * http://en.wikipedia.org/wiki/Netpbm_format |
group-FlexBook | 0:fa7450a43b99 | 31 | * However, this seems to indicate that the header format is more free format |
group-FlexBook | 0:fa7450a43b99 | 32 | * http://paulbourke.net/dataformats/ppm/ |
group-FlexBook | 0:fa7450a43b99 | 33 | * Lines terminated with cr and/or lf or just white space |
group-FlexBook | 0:fa7450a43b99 | 34 | * Header example 1 |
group-FlexBook | 0:fa7450a43b99 | 35 | * P6 1024 788 255 |
group-FlexBook | 0:fa7450a43b99 | 36 | * Header example 2 |
group-FlexBook | 0:fa7450a43b99 | 37 | * P6 |
group-FlexBook | 0:fa7450a43b99 | 38 | * 1024 788 |
group-FlexBook | 0:fa7450a43b99 | 39 | * # A comment |
group-FlexBook | 0:fa7450a43b99 | 40 | * 255 |
group-FlexBook | 0:fa7450a43b99 | 41 | * Header example 3 |
group-FlexBook | 0:fa7450a43b99 | 42 | * P3 |
group-FlexBook | 0:fa7450a43b99 | 43 | * 1024 # the image width |
group-FlexBook | 0:fa7450a43b99 | 44 | * 788 # the image height |
group-FlexBook | 0:fa7450a43b99 | 45 | * # A comment |
group-FlexBook | 0:fa7450a43b99 | 46 | * 1023 |
group-FlexBook | 0:fa7450a43b99 | 47 | * |
group-FlexBook | 0:fa7450a43b99 | 48 | * |
group-FlexBook | 0:fa7450a43b99 | 49 | * P4 |
group-FlexBook | 0:fa7450a43b99 | 50 | * # Comment string |
group-FlexBook | 0:fa7450a43b99 | 51 | * 24 7 |
group-FlexBook | 0:fa7450a43b99 | 52 | * |
group-FlexBook | 0:fa7450a43b99 | 53 | * P5 |
group-FlexBook | 0:fa7450a43b99 | 54 | * # Comment string |
group-FlexBook | 0:fa7450a43b99 | 55 | * 24 7 |
group-FlexBook | 0:fa7450a43b99 | 56 | * 15 |
group-FlexBook | 0:fa7450a43b99 | 57 | * |
group-FlexBook | 0:fa7450a43b99 | 58 | */ |
group-FlexBook | 0:fa7450a43b99 | 59 | |
group-FlexBook | 0:fa7450a43b99 | 60 | #include <stdlib.h> |
group-FlexBook | 0:fa7450a43b99 | 61 | #include "assert.h" |
group-FlexBook | 0:fa7450a43b99 | 62 | #include "ChaN/ff.h" |
group-FlexBook | 0:fa7450a43b99 | 63 | #include "pnm-utils.h" |
group-FlexBook | 0:fa7450a43b99 | 64 | |
group-FlexBook | 0:fa7450a43b99 | 65 | int pnm_read_int32(FIL *pnm_file, int32_t *value) |
group-FlexBook | 0:fa7450a43b99 | 66 | { |
group-FlexBook | 0:fa7450a43b99 | 67 | UINT count; |
group-FlexBook | 0:fa7450a43b99 | 68 | char ch; |
group-FlexBook | 0:fa7450a43b99 | 69 | int digits = 0; |
group-FlexBook | 0:fa7450a43b99 | 70 | int in_comment = 0; |
group-FlexBook | 0:fa7450a43b99 | 71 | int found = 0; |
group-FlexBook | 0:fa7450a43b99 | 72 | int32_t val = 0; |
group-FlexBook | 0:fa7450a43b99 | 73 | |
group-FlexBook | 0:fa7450a43b99 | 74 | assert(pnm_file != NULL); |
group-FlexBook | 0:fa7450a43b99 | 75 | assert(value != NULL); |
group-FlexBook | 0:fa7450a43b99 | 76 | |
group-FlexBook | 0:fa7450a43b99 | 77 | while (!found && |
group-FlexBook | 0:fa7450a43b99 | 78 | (f_read(pnm_file,&ch,1,&count) == FR_OK) && count == 1) |
group-FlexBook | 0:fa7450a43b99 | 79 | { |
group-FlexBook | 0:fa7450a43b99 | 80 | switch (ch) |
group-FlexBook | 0:fa7450a43b99 | 81 | { |
group-FlexBook | 0:fa7450a43b99 | 82 | case '#': |
group-FlexBook | 0:fa7450a43b99 | 83 | in_comment = 1; |
group-FlexBook | 0:fa7450a43b99 | 84 | break; |
group-FlexBook | 0:fa7450a43b99 | 85 | case ' ': |
group-FlexBook | 0:fa7450a43b99 | 86 | case '\t': |
group-FlexBook | 0:fa7450a43b99 | 87 | case '\r': |
group-FlexBook | 0:fa7450a43b99 | 88 | case '\n': |
group-FlexBook | 0:fa7450a43b99 | 89 | if (!in_comment) |
group-FlexBook | 0:fa7450a43b99 | 90 | { |
group-FlexBook | 0:fa7450a43b99 | 91 | if (digits) { |
group-FlexBook | 0:fa7450a43b99 | 92 | found = 1; |
group-FlexBook | 0:fa7450a43b99 | 93 | } |
group-FlexBook | 0:fa7450a43b99 | 94 | } |
group-FlexBook | 0:fa7450a43b99 | 95 | if (ch == '\r' || ch == '\n') |
group-FlexBook | 0:fa7450a43b99 | 96 | in_comment = 0; |
group-FlexBook | 0:fa7450a43b99 | 97 | break; |
group-FlexBook | 0:fa7450a43b99 | 98 | case '0': case '1': case '2': case '3': case '4': |
group-FlexBook | 0:fa7450a43b99 | 99 | case '5': case '6': case '7': case '8': case '9': |
group-FlexBook | 0:fa7450a43b99 | 100 | if (!in_comment) { |
group-FlexBook | 0:fa7450a43b99 | 101 | val = val * 10 + (ch - '0'); |
group-FlexBook | 0:fa7450a43b99 | 102 | digits++; |
group-FlexBook | 0:fa7450a43b99 | 103 | |
group-FlexBook | 0:fa7450a43b99 | 104 | if (f_eof(pnm_file)) { |
group-FlexBook | 0:fa7450a43b99 | 105 | found = 1; |
group-FlexBook | 0:fa7450a43b99 | 106 | } |
group-FlexBook | 0:fa7450a43b99 | 107 | } |
group-FlexBook | 0:fa7450a43b99 | 108 | break; |
group-FlexBook | 0:fa7450a43b99 | 109 | default: |
group-FlexBook | 0:fa7450a43b99 | 110 | break; |
group-FlexBook | 0:fa7450a43b99 | 111 | } |
group-FlexBook | 0:fa7450a43b99 | 112 | } |
group-FlexBook | 0:fa7450a43b99 | 113 | |
group-FlexBook | 0:fa7450a43b99 | 114 | if (!found) |
group-FlexBook | 0:fa7450a43b99 | 115 | return -1; |
group-FlexBook | 0:fa7450a43b99 | 116 | |
group-FlexBook | 0:fa7450a43b99 | 117 | *value = val; |
group-FlexBook | 0:fa7450a43b99 | 118 | |
group-FlexBook | 0:fa7450a43b99 | 119 | return 0; |
group-FlexBook | 0:fa7450a43b99 | 120 | } |
group-FlexBook | 0:fa7450a43b99 | 121 | |
group-FlexBook | 0:fa7450a43b99 | 122 | int pnm_read_header(FIL *pnm_file, struct pnm_header *hdr) |
group-FlexBook | 0:fa7450a43b99 | 123 | { |
group-FlexBook | 0:fa7450a43b99 | 124 | char buffer[2]; |
group-FlexBook | 0:fa7450a43b99 | 125 | UINT count; |
group-FlexBook | 0:fa7450a43b99 | 126 | |
group-FlexBook | 0:fa7450a43b99 | 127 | assert(pnm_file); |
group-FlexBook | 0:fa7450a43b99 | 128 | assert(hdr); |
group-FlexBook | 0:fa7450a43b99 | 129 | |
group-FlexBook | 0:fa7450a43b99 | 130 | hdr->type = PNM_UNKNOWN; |
group-FlexBook | 0:fa7450a43b99 | 131 | |
group-FlexBook | 0:fa7450a43b99 | 132 | if (f_read(pnm_file,buffer,2,&count) != FR_OK) |
group-FlexBook | 0:fa7450a43b99 | 133 | goto read_error; |
group-FlexBook | 0:fa7450a43b99 | 134 | |
group-FlexBook | 0:fa7450a43b99 | 135 | if (buffer[0] != 'P') |
group-FlexBook | 0:fa7450a43b99 | 136 | goto format_error; |
group-FlexBook | 0:fa7450a43b99 | 137 | |
group-FlexBook | 0:fa7450a43b99 | 138 | if (buffer[1] == '4') |
group-FlexBook | 0:fa7450a43b99 | 139 | hdr->type = PNM_BITMAP; |
group-FlexBook | 0:fa7450a43b99 | 140 | else if (buffer[1] == '5') |
group-FlexBook | 0:fa7450a43b99 | 141 | hdr->type = PNM_GREYSCALE; |
group-FlexBook | 0:fa7450a43b99 | 142 | else |
group-FlexBook | 0:fa7450a43b99 | 143 | goto format_error; |
group-FlexBook | 0:fa7450a43b99 | 144 | |
group-FlexBook | 0:fa7450a43b99 | 145 | hdr->max_gray = 1; |
group-FlexBook | 0:fa7450a43b99 | 146 | hdr->width = pnm_read_int(pnm_file); |
group-FlexBook | 0:fa7450a43b99 | 147 | hdr->height = pnm_read_int(pnm_file); |
group-FlexBook | 0:fa7450a43b99 | 148 | |
group-FlexBook | 0:fa7450a43b99 | 149 | if (hdr->type == PNM_GREYSCALE) { |
group-FlexBook | 0:fa7450a43b99 | 150 | hdr->max_gray = pnm_read_int(pnm_file); |
group-FlexBook | 0:fa7450a43b99 | 151 | } |
group-FlexBook | 0:fa7450a43b99 | 152 | // check to see if any of the data items were not read correctly |
group-FlexBook | 0:fa7450a43b99 | 153 | if (hdr->width <= 0 || hdr->height <= 0 || hdr->max_gray <= 0) |
group-FlexBook | 0:fa7450a43b99 | 154 | goto format_error; |
group-FlexBook | 0:fa7450a43b99 | 155 | |
group-FlexBook | 0:fa7450a43b99 | 156 | // read pointer is now positioned at start of image data |
group-FlexBook | 0:fa7450a43b99 | 157 | return 0; |
group-FlexBook | 0:fa7450a43b99 | 158 | |
group-FlexBook | 0:fa7450a43b99 | 159 | format_error: |
group-FlexBook | 0:fa7450a43b99 | 160 | read_error: |
group-FlexBook | 0:fa7450a43b99 | 161 | return -1; |
group-FlexBook | 0:fa7450a43b99 | 162 | } |