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


apriltag
Author(s): Edwin Olson , Max Krogius
autogenerated on Mon Jun 26 2023 02:26:12