File: fxppmio.cpp

package info (click to toggle)
gogglesmm 1.2.5-6
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 16,812 kB
  • sloc: cpp: 231,960; ansic: 893; xml: 222; makefile: 33
file content (234 lines) | stat: -rw-r--r-- 6,962 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/********************************************************************************
*                                                                               *
*                          P P M   I n p u t / O u t p u t                      *
*                                                                               *
*********************************************************************************
* Copyright (C) 2003,2022 by Jeroen van der Zijp.   All Rights Reserved.        *
*********************************************************************************
* This library is free software; you can redistribute it and/or modify          *
* it under the terms of the GNU Lesser General Public License as published by   *
* the Free Software Foundation; either version 3 of the License, or             *
* (at your option) any later version.                                           *
*                                                                               *
* This library is distributed in the hope that it will be useful,               *
* but WITHOUT ANY WARRANTY; without even the implied warranty of                *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                 *
* GNU Lesser General Public License for more details.                           *
*                                                                               *
* You should have received a copy of the GNU Lesser General Public License      *
* along with this program.  If not, see <http://www.gnu.org/licenses/>          *
********************************************************************************/
#include "xincs.h"
#include "fxver.h"
#include "fxdefs.h"
#include "fxmath.h"
#include "FXArray.h"
#include "FXHash.h"
#include "FXElement.h"
#include "FXStream.h"



/*
  Notes:
  - Definitely a 'no-frills' format.
  - Certainly not optimized for speed; but it works.
  - No support for values greater than 255.
*/

using namespace FX;

/*******************************************************************************/

namespace FX {


#ifndef FXLOADPPM
extern FXAPI FXbool fxcheckPPM(FXStream& store);
extern FXAPI FXbool fxloadPPM(FXStream& store,FXColor*& data,FXint& width,FXint& height);
extern FXAPI FXbool fxsavePPM(FXStream& store,const FXColor *data,FXint width,FXint height);
#endif

// Furnish our own version
extern FXAPI FXint __snprintf(FXchar* string,FXint length,const FXchar* format,...);


// Read one integer
static FXint getint(FXStream& store){
  FXint num=0;
  FXuchar c;
  while(!store.eof()){
    store >> c;
    if('0'<=c && c<='9') break;
    if(c=='#'){
      while(!store.eof()){
        store >> c;
        if(c=='\n') break;
        }
      }
    }
  while(!store.eof()){
    num=num*10+c-'0';
    store >> c;
    if(c<'0' || c>'9') break;
    }
  return num;
  }


// Check if stream contains a PPM
FXbool fxcheckPPM(FXStream& store){
  FXuchar signature[2];
  store.load(signature,2);
  store.position(-2,FXFromCurrent);
  return signature[0]=='P' && '1'<=signature[1] && signature[1]<='6';
  }


// Load image from stream
FXbool fxloadPPM(FXStream& store,FXColor*& data,FXint& width,FXint& height){
  FXint npixels,i,j,maxvalue=1;
  FXuchar *pp;
  FXuchar magic,format,byte;

  // Null out
  data=nullptr;
  width=0;
  height=0;

  // Check magic byte
  store >> magic;
  if(magic=='P'){

    // Check format
    // "P1" = Ascii bitmap,
    // "P2" = Ascii greymap,
    // "P3" = Ascii pixmap,
    // "P4" = Raw bitmap,
    // "P5" = Raw greymap,
    // "P6" = Raw pixmap
    store >> format;
    if('1'<=format && format<='6'){

      // Get size
      width=getint(store);
      height=getint(store);

      // Sanity check
      if(0<width && 0<height){
        npixels=width*height;

        // Get maximum value
        if(format!='1' && format!='4'){
          maxvalue=getint(store);
          if(maxvalue<=0 || maxvalue>=256) return false;
          }

        FXTRACE((100,"fxloadPPM: width=%d height=%d type=%c \n",width,height,format));

        // Allocate buffer
        if(callocElms(data,npixels)){

          // Read it
          pp=(FXuchar*)data;
          switch(format){
            case '1':   // Ascii bitmap
              for(i=0; i<height; i++){
                for(j=0; j<width; j++,pp+=4){
                  byte=getint(store);
                  pp[2] = byte?255:0;
                  pp[1] = pp[2];
                  pp[0] = pp[2];
                  pp[3] = 255;
                  }
                }
              break;
            case '2':   // Ascii greymap
              for(i=0; i<height; i++){
                for(j=0; j<width; j++,pp+=4){
                  pp[2] = getint(store);
                  pp[1] = pp[2];
                  pp[0] = pp[2];
                  pp[3] = 255;
                  }
                }
              break;
            case '3':   // Ascii pixmap
              for(i=0; i<height; i++){
                for(j=0; j<width; j++,pp+=4){
                  pp[2] = getint(store);
                  pp[1] = getint(store);
                  pp[0] = getint(store);
                  pp[3] = 255;
                  }
                }
              break;
            case '4':   // Binary bitmap
              for(i=0; i<height; i++){
                for(j=0; j<width; j++,byte<<=1,pp+=4){
                  if((j&7)==0){ store >> byte; }
                  pp[2] = (byte&0x80)?255:0;
                  pp[1] = pp[2];
                  pp[0] = pp[2];
                  pp[3] = 255;
                  }
                }
              break;
            case '5':   // Binary greymap
              for(i=0; i<height; i++){
                for(j=0; j<width; j++,pp+=4){
                  store >> pp[2];
                  pp[1] =  pp[2];
                  pp[0] =  pp[2];
                  pp[3] =  255;
                  }
                }
              break;
            case '6':   // Binary pixmap
              for(i=0; i<height; i++){
                for(j=0; j<width; j++,pp+=4){
                  store >> pp[2];
                  store >> pp[1];
                  store >> pp[0];
                  pp[3] =  255;
                  }
                }
              break;
            }
          return true;
          }
        }
      }
    }
  return false;
  }


/*******************************************************************************/


// Save a bmp file to a stream
FXbool fxsavePPM(FXStream& store,const FXColor *data,FXint width,FXint height){
  if(data && 0<width && 0<height){
    const FXuchar *pp=(const FXuchar*)data;
    FXint i,j,nsize;
    FXchar size[32];

    // Save header
    nsize=__snprintf(size,sizeof(size),"P6\n%d %d\n255\n",width,height);
    store.save(size,nsize);

    // 24-bit/pixel
    for(i=0; i<height; i++){
      for(j=0; j<width; j++,pp+=4){
        store << pp[2];
        store << pp[1];
        store << pp[0];
        }
      }
    return true;
    }
  return false;
  }

}