Adonthell  0.4
pnm.cc
Go to the documentation of this file.
1 /*
2  Copyright (C) 1999 The Adonthell Project
3  Part of the Adonthell Project <http://adonthell.nongnu.org>
4 
5  Adonthell is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  Adonthell is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with Adonthell. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 
20 /**
21  * @file pnm.cc
22  * @author Alexandre Courbot <alexandrecourbot@linuxgames.com>
23  *
24  * @brief Defines the pnm static class.
25  *
26  *
27  */
28 
29 
30 #include "pnm.h"
31 #include <cstdlib>
32 #include <cstdio>
33 #include <cstring>
34 
35 void *pnm::get (SDL_RWops * file, u_int16 * length, u_int16 * height)
36 {
37  void *image;
38  char sign[10];
39  u_int16 l, h;
40  u_int32 i = 0;
41 
42  SDL_RWread (file, sign, 1, 2);
43  if ((sign[0] != 'P') || (sign[1] != '6'))
44  {
45  printf ("Invalid format.\n");
46  return (NULL);
47  }
48  pnm_gotonextline (file);
49  /* Getting height and length */
50  while (pnm_checkforcomment (file));
51  do
52  {
53  SDL_RWread (file, &sign[i], 1, 1);
54  i++;
55  }
56  while (sign[i - 1] != ' ');
57  sign[i - 1] = 0;
58  l = atoi (sign);
59  i = 0;
60  do
61  {
62  SDL_RWread (file, &sign[i], 1, 1);
63  i++;
64  }
65  while (sign[i - 1] != '\n');
66  sign[i - 1] = 0;
67  h = atoi (sign);
68  /* Going to next line */
69  pnm_gotonextline (file);
70  /* Reading the image */
71  image = calloc (l * h, 3);
72  SDL_RWread (file, image, 1, l * h * 3);
73  if (length)
74  *length = l;
75  if (height)
76  *height = h;
77  return (image);
78 }
79 
80 void pnm::put (SDL_RWops * file, void *image, u_int16 length, u_int16 height)
81 {
82  char s[30];
83 
84  sprintf (s, "P6\n%d %d\n255\n", length, height);
85  SDL_RWwrite (file, s, sizeof (char), strlen (s));
86 
87  SDL_RWwrite (file, image, 1, length * height * 3);
88 }
89 
90 
91 
92 
93 // Private methods.
94 
95 
96 
97 void pnm::pnm_gotonextline (SDL_RWops * file)
98 {
99  char buff;
100 
101  do
102  {
103  SDL_RWread (file, &buff, 1, 1);
104  }
105  while (buff != '\n');
106 }
107 
108 int pnm::pnm_checkforcomment (SDL_RWops * file)
109 {
110  char buff;
111 
112  SDL_RWread (file, &buff, 1, 1);
113  if (buff == '#')
114  {
115  pnm_gotonextline (file);
116  return (1);
117  }
118  else
119  {
120  SDL_RWseek (file, -1, SEEK_CUR);
121  return (0);
122  }
123 }
u_int32
#define u_int32
32 bits long unsigned integer
Definition: types.h:41
pnm::put
static void put(SDL_RWops *file, void *image, u_int16 length, u_int16 height)
Saves a PNM image into an opened file.
Definition: pnm.cc:80
pnm.h
Declares the pnm static class.
pnm::get
static void * get(SDL_RWops *file, u_int16 *length, u_int16 *height)
Reads a PNM image from an opened file.
Definition: pnm.cc:35
image
Image manipulation class.
Definition: image.h:45
u_int16
#define u_int16
16 bits long unsigned integer
Definition: types.h:38