Adonthell
0.4
|
00001 /* 00002 $Id: pnm.cc,v 1.5 2002/02/20 19:02:26 ksterker Exp $ 00003 00004 Copyright (C) 1999 The Adonthell Project 00005 Part of the Adonthell Project http://adonthell.linuxgames.com 00006 00007 This program is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License. 00009 This program is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY. 00011 00012 See the COPYING file for more details. 00013 */ 00014 00015 00016 /** 00017 * @file pnm.cc 00018 * @author Alexandre Courbot <alexandrecourbot@linuxgames.com> 00019 * 00020 * @brief Defines the pnm static class. 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 /* Getting height and length */ 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 /* Going to next line */ 00067 pnm_gotonextline (file); 00068 /* Reading the image */ 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 // Private methods. 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 }