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