00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "pnm.h"
00027 #include <stdlib.h>
00028 #include <string.h>
00029
00030 using namespace std;
00031
00032
00033 void *pnm::get (SDL_RWops * file, u_int16 * length, u_int16 * height)
00034 {
00035 void *image;
00036 char sign[10];
00037 u_int16 l, h;
00038 u_int32 i = 0;
00039
00040 SDL_RWread (file, sign, 1, 2);
00041 if ((sign[0] != 'P') || (sign[1] != '6'))
00042 {
00043 printf ("Invalid format.\n");
00044 return (NULL);
00045 }
00046 pnm_gotonextline (file);
00047
00048 while (pnm_checkforcomment (file));
00049 do
00050 {
00051 SDL_RWread (file, &sign[i], 1, 1);
00052 i++;
00053 }
00054 while (sign[i - 1] != ' ');
00055 sign[i - 1] = 0;
00056 l = atoi (sign);
00057 i = 0;
00058 do
00059 {
00060 SDL_RWread (file, &sign[i], 1, 1);
00061 i++;
00062 }
00063 while (sign[i - 1] != '\n');
00064 sign[i - 1] = 0;
00065 h = atoi (sign);
00066
00067 pnm_gotonextline (file);
00068
00069 image = calloc (l * h, 3);
00070 SDL_RWread (file, image, 1, l * h * 3);
00071 if (length)
00072 *length = l;
00073 if (height)
00074 *height = h;
00075 return (image);
00076 }
00077
00078 void pnm::put (SDL_RWops * file, void *image, u_int16 length, u_int16 height)
00079 {
00080 char s[30];
00081
00082 sprintf (s, "P6\n%d %d\n255\n", length, height);
00083 SDL_RWwrite (file, s, sizeof (char), strlen (s));
00084
00085 SDL_RWwrite (file, image, 1, length * height * 3);
00086 }
00087
00088
00089
00090
00091
00092
00093
00094
00095 void pnm::pnm_gotonextline (SDL_RWops * file)
00096 {
00097 char buff;
00098
00099 do
00100 {
00101 SDL_RWread (file, &buff, 1, 1);
00102 }
00103 while (buff != '\n');
00104 }
00105
00106 int pnm::pnm_checkforcomment (SDL_RWops * file)
00107 {
00108 char buff;
00109
00110 SDL_RWread (file, &buff, 1, 1);
00111 if (buff == '#')
00112 {
00113 pnm_gotonextline (file);
00114 return (1);
00115 }
00116 else
00117 {
00118 SDL_RWseek (file, -1, SEEK_CUR);
00119 return (0);
00120 }
00121 }