pnm.c
Go to the documentation of this file.
1 /* Copyright (C) 2013-2016, The Regents of The University of Michigan.
2 All rights reserved.
3 
4 This software was developed in the APRIL Robotics Lab under the
5 direction of Edwin Olson, ebolson@umich.edu. This software may be
6 available under alternative licensing terms; contact the address above.
7 
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10 
11 1. Redistributions of source code must retain the above copyright notice, this
12  list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 The views and conclusions contained in the software and documentation are those
29 of the authors and should not be interpreted as representing official policies,
30 either expressed or implied, of the Regents of The University of Michigan.
31 */
32 
33 #include <assert.h>
34 #include <stdint.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 
38 #include "pnm.h"
39 
40 pnm_t *pnm_create_from_file(const char *path)
41 {
42  FILE *f = fopen(path, "rb");
43  if (f == NULL)
44  return NULL;
45 
46  pnm_t *pnm = calloc(1, sizeof(pnm_t));
47  pnm->format = -1;
48 
49  char tmp[1024];
50  int nparams = 0; // will be 3 when we're all done.
51  int params[3];
52 
53  while (nparams < 3 && !(pnm->format == PNM_FORMAT_BINARY && nparams == 2)) {
54  if (fgets(tmp, sizeof(tmp), f) == NULL)
55  goto error;
56 
57  // skip comments
58  if (tmp[0]=='#')
59  continue;
60 
61  char *p = tmp;
62 
63  if (pnm->format == -1 && tmp[0]=='P') {
64  pnm->format = tmp[1]-'0';
65  assert(pnm->format == PNM_FORMAT_GRAY || pnm->format == PNM_FORMAT_RGB || pnm->format == PNM_FORMAT_BINARY);
66  p = &tmp[2];
67  }
68 
69  // pull integers out of this line until there are no more.
70  while (nparams < 3 && *p!=0) {
71  while (*p==' ')
72  p++;
73 
74  // encounter rubbish? (End of line?)
75  if (*p < '0' || *p > '9')
76  break;
77 
78  int acc = 0;
79  while (*p >= '0' && *p <= '9') {
80  acc = acc*10 + *p - '0';
81  p++;
82  }
83 
84  params[nparams++] = acc;
85  p++;
86  }
87  }
88 
89  pnm->width = params[0];
90  pnm->height = params[1];
91  pnm->max = params[2];
92 
93  switch (pnm->format) {
94  case PNM_FORMAT_BINARY: {
95  // files in the wild sometimes simply don't set max
96  pnm->max = 1;
97 
98  pnm->buflen = pnm->height * ((pnm->width + 7) / 8);
99  pnm->buf = malloc(pnm->buflen);
100  size_t len = fread(pnm->buf, 1, pnm->buflen, f);
101  if (len != pnm->buflen)
102  goto error;
103 
104  fclose(f);
105  return pnm;
106  }
107 
108  case PNM_FORMAT_GRAY: {
109  if (pnm->max == 255)
110  pnm->buflen = pnm->width * pnm->height;
111  else if (pnm->max == 65535)
112  pnm->buflen = 2 * pnm->width * pnm->height;
113  else
114  assert(0);
115 
116  pnm->buf = malloc(pnm->buflen);
117  size_t len = fread(pnm->buf, 1, pnm->buflen, f);
118  if (len != pnm->buflen)
119  goto error;
120 
121  fclose(f);
122  return pnm;
123  }
124 
125  case PNM_FORMAT_RGB: {
126  if (pnm->max == 255)
127  pnm->buflen = pnm->width * pnm->height * 3;
128  else if (pnm->max == 65535)
129  pnm->buflen = 2 * pnm->width * pnm->height * 3;
130  else
131  assert(0);
132 
133  pnm->buf = malloc(pnm->buflen);
134  size_t len = fread(pnm->buf, 1, pnm->buflen, f);
135  if (len != pnm->buflen)
136  goto error;
137  fclose(f);
138  return pnm;
139  }
140  }
141 
142 error:
143  fclose(f);
144 
145  if (pnm != NULL) {
146  free(pnm->buf);
147  free(pnm);
148  }
149 
150  return NULL;
151 }
152 
154 {
155  if (pnm == NULL)
156  return;
157 
158  free(pnm->buf);
159  free(pnm);
160 }
int height
Definition: pnm.h:51
int format
Definition: pnm.h:52
#define PNM_FORMAT_RGB
Definition: pnm.h:44
#define PNM_FORMAT_BINARY
Definition: pnm.h:42
uint8_t * buf
Definition: pnm.h:56
Definition: pnm.h:49
int width
Definition: pnm.h:51
int max
Definition: pnm.h:53
pnm_t * pnm_create_from_file(const char *path)
Definition: pnm.c:40
#define PNM_FORMAT_GRAY
Definition: pnm.h:43
uint32_t buflen
Definition: pnm.h:55
void pnm_destroy(pnm_t *pnm)
Definition: pnm.c:153


apriltags2
Author(s): Danylo Malyuta
autogenerated on Fri Oct 19 2018 04:02:32