-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReaderWriterPCX.cpp
370 lines (317 loc) · 10.5 KB
/
ReaderWriterPCX.cpp
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
* osgdb_pcx is plugin for OpenSceneGraph engine
* Copyright (C) 2015 by darkprof
*
* 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 2.1 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
#include <osg/Image>
#include <osg/Notify>
#include <osg/Image>
#include <osg/GL>
#include <osg/Endian>
#include <osgDB/Registry>
#include <osgDB/FileNameUtils>
#include <osgDB/FileUtils>
#include <vector>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "ReaderWriterPCX.hpp"
#include "../global.hpp"
//#include "pcx.hpp"
static unsigned char* pcx_load(
std::istream& fin,
int& width_ret,
int& height_ret,
int& numComponents_ret)
{
shared_ptr(PCXHEADER) pcx (new PCXHEADER);
uint8_t tmp, count;
bool isHavePalette = true;
bool isStandartPalette = false;
bool isHave16Colors = false;
uint MaxNumberOfColors, index;
shared_ptr(COLORS[]) colorPalette;// (new uint8_t(768));
//shared_ptr(std::vector<unsigned char>) srcBuffer;
//shared_ptr()
//bool swap;
// actual file size
fin.seekg(0, std::ios::end);
size_t actFileSize = fin.tellg();
fin.seekg(0, std::ios::beg);
if (actFileSize < 128) {
OSG_WARN << "Invalid PCX file size\n";
return 0;
}
fin.read((char*) &pcx->Identifier, sizeof(pcx->Identifier));
if (pcx->Identifier != 0x0a) {
OSG_WARN << "Invalid PCX Identifier\n";
return 0;
}
fin.read((char*) &pcx->Version, sizeof(pcx->Version));
if ((pcx->Version == 1) || (pcx->Version > 5)) {
OSG_WARN << "Invalid PCX Version\n";
return 0;
}
fin.read((char*) &pcx->Encoding, sizeof(pcx->Encoding));
if ((pcx->Encoding != 0) && (pcx->Encoding != 1)) {
OSG_WARN << "Invalid PCX Encoding\n";
return 0;
}
fin.read((char*) &pcx->BitsPerPixel, sizeof(pcx->BitsPerPixel));
if ((pcx->BitsPerPixel != 8) &&
(pcx->BitsPerPixel != 1) &&
(pcx->BitsPerPixel != 2) &&
(pcx->BitsPerPixel != 4)) {
OSG_WARN << "Invalid PCX BitsPerPixel\n";
return 0;
}
/*
* Image size
*/
fin.read((char*) &pcx->XStart, sizeof(pcx->XStart));
fin.read((char*) &pcx->YStart, sizeof(pcx->YStart));
fin.read((char*) &pcx->XEnd, sizeof(pcx->XEnd));
fin.read((char*) &pcx->YEnd, sizeof(pcx->YEnd));
fin.read((char*) &pcx->HorzRes, sizeof(pcx->HorzRes));
fin.read((char*) &pcx->VertRes, sizeof(pcx->VertRes));
width_ret = pcx->XEnd - pcx->XStart + 1; /* Width of image in pixels */
height_ret = pcx->YEnd - pcx->YStart + 1; /* Length of image in scan lines */
if ((width_ret <= 0) || (height_ret <= 0)) {
OSG_WARN << "Invalid PCX image size\n";
return 0;
}
fin.read((char*) &pcx->Palette, sizeof(pcx->Palette));
fin.read((char*) &pcx->Reserved1, sizeof(pcx->Reserved1));
fin.read((char*) &pcx->NumBitPlanes, sizeof(pcx->NumBitPlanes));
fin.read((char*) &pcx->BytesPerLine, sizeof(pcx->BytesPerLine));
fin.read((char*) &pcx->PaletteType, sizeof(pcx->PaletteType));
fin.read((char*) &pcx->HorzScreenSize, sizeof(pcx->HorzScreenSize));
fin.read((char*) &pcx->VertScreenSize, sizeof(pcx->VertScreenSize));
fin.read((char*) &pcx->Reserved2, sizeof(pcx->Reserved2));
/*
* Settings
*/
isHavePalette = (pcx->BitsPerPixel * pcx->NumBitPlanes) < 24;
MaxNumberOfColors = (1L << (pcx->BitsPerPixel * pcx->NumBitPlanes));
isStandartPalette = (MaxNumberOfColors < 16);
isHave16Colors = (MaxNumberOfColors == 16);
/*
* color palette
*/
// 2, 4, 8 colors FIX
if (isStandartPalette) {
OSG_WARN << "Sorry, PCX image palittre 2, 4 or 8 colors not support\n";
return 0;
}
// 16 colors FIX
if (isStandartPalette) {
OSG_WARN << "Sorry, PCX image palittre 16 colors not support\n";
return 0;
}
// if 256 colors read palette from end file
if (MaxNumberOfColors == 256) {
fin.seekg(-769, std::ios::end);
fin.read((char*) &tmp, sizeof(tmp));
if (tmp != 0x0c) {
OSG_WARN << "Invalid PCX image 256 color palette\n";
return 0;
}
numComponents_ret = sizeof(COLORS);
//colorPalette->resize (PCX_RGB * MaxNumberOfColors);
colorPalette.reset( new COLORS [MaxNumberOfColors] );
fin.read((char*) colorPalette.get(), sizeof(COLORS) * MaxNumberOfColors);
}
/*
* image data
*/
//const unsigned int imageBytesPerRow = dib.width * imageBytesPerPixel;
const unsigned int imageBufferSize = width_ret * height_ret;
//srcSize = 8 * pcx->NumBitPlanes; // work only if equalent or above 256 colors
//srcBuffer->resize (srcSize);
COLORS* imageBuffer = new COLORS[imageBufferSize];
/*
** Decode a PCX scan line.
**
** In this example the size of Buffer[] and the value of BufferSize
** is equal to the scan line length. Data is read from the FILE
** stream fpIn and written to Buffer[].
*/
fin.seekg(sizeof (PCXHEADER), std::ios::beg);
index = 0; count = 0;
// #### pcx->Encoding, if ##################################################
if (pcx->Encoding) {
if (!isHavePalette) {
OSG_WARN << "Sorry, packed PCX image with no palette not support\n";
return 0;
}
for (int h = height_ret - 1; h >= 0; --h) {
for (int w = 0; w < width_ret; ++w) {
if(!count) {
if(!fin.read((char*) &tmp, sizeof(tmp))) {
OSG_WARN << "file truncated\n";
return 0;
}
if( (tmp & 0xc0) == 0xc0) {
count = tmp & 0x3f;
if(!fin.read((char*) &tmp, sizeof(tmp))) {
OSG_WARN << "file truncated\n";
return 0;
}
} else {
count = 1;
}
}
index = h * width_ret + w;
imageBuffer[index].red = colorPalette[tmp].red;
imageBuffer[index].green = colorPalette[tmp].green;
imageBuffer[index].blue = colorPalette[tmp].blue;
--count;
}
}
// ##### pcx->Encoding, else ###############################################
} else {
// if no palette
if (!isHavePalette) {
//while (index < imageBufferSize) {
for (int h = height_ret - 1; h >= 0; --h) {
for (int w = 0; w < width_ret; ++w) {
index = h * width_ret + w;
if ( !fin.read((char*) &imageBuffer[index], sizeof(COLORS)) ) {
OSG_WARN << "file truncated\n";
return 0;
}
}
}
} else {
for (int h = height_ret - 1; h >= 0; --h) {
for (int w = 0; w < width_ret; ++w) {
if ( !fin.read((char*) &tmp, sizeof(tmp)) ) {
OSG_WARN << "file truncated\n";
return 0;
}
index = h * width_ret + w;
imageBuffer[index].red = colorPalette[tmp].red;
imageBuffer[index].green = colorPalette[tmp].green;
imageBuffer[index].blue = colorPalette[tmp].blue;
}
}
}
} // #### pcx->Encoding, end if ############################################
return (uint8_t*)imageBuffer;
}
static bool pcx_save(const osg::Image& img, std::ostream& fout)
{
}
ReaderWriterPCX::ReaderWriterPCX()
{
supportsExtension("pcx","PCX Image format");
}
const char* ReaderWriterPCX::className() const
{
return "PCX Image Reader";
}
osgDB::ReaderWriter::ReadResult ReaderWriterPCX::readObject(
std::istream& fin, const Options* options) const
{
return readImage(fin, options);
}
osgDB::ReaderWriter::ReadResult ReaderWriterPCX::readObject(
const std::string& file, const Options* options) const
{
return readImage(file, options);
}
osgDB::ReaderWriter::ReadResult ReaderWriterPCX::readImage(
std::istream& fin, const Options* options) const
{
return readPCXStream(fin);
}
osgDB::ReaderWriter::ReadResult ReaderWriterPCX::readImage(
const std::string& file, const Options* options) const
{
std::string ext = osgDB::getLowerCaseFileExtension(file);
if (!acceptsExtension(ext))
return osgDB::ReaderWriter::ReadResult::FILE_NOT_HANDLED;
std::string fileName = osgDB::findDataFile(file, options);
if (fileName.empty())
return osgDB::ReaderWriter::ReadResult::FILE_NOT_FOUND;
osgDB::ifstream istream(fileName.c_str(), std::ios::in | std::ios::binary);
if(!istream)
return osgDB::ReaderWriter::ReadResult::FILE_NOT_HANDLED;
osgDB::ReaderWriter::ReadResult rr = readPCXStream(istream);
if(rr.validImage())
rr.getImage()->setFileName(file);
return rr;
}
osgDB::ReaderWriter::WriteResult ReaderWriterPCX::writeImage(
const osg::Image& image, std::ostream& fout, const Options* options) const
{
if (pcx_save(image, fout))
return osgDB::ReaderWriter::WriteResult::FILE_SAVED;
else
return osgDB::ReaderWriter::WriteResult::ERROR_IN_WRITING_FILE;
}
osgDB::ReaderWriter::WriteResult ReaderWriterPCX::writeImage(
const osg::Image& img,
const std::string& fileName, const Options* options) const
{
std::string ext = osgDB::getFileExtension(fileName);
if (!acceptsExtension(ext))
return osgDB::ReaderWriter::WriteResult::FILE_NOT_HANDLED;
osgDB::ofstream fout(fileName.c_str(), std::ios::out | std::ios::binary);
if (!fout)
return osgDB::ReaderWriter::WriteResult::ERROR_IN_WRITING_FILE;
return writeImage(img, fout, options);
}
osgDB::ReaderWriter::ReadResult ReaderWriterPCX::readPCXStream(
std::istream& fin)
{
int s, t;
int internalFormat;
unsigned char *imageData = pcx_load(fin, s, t, internalFormat);
if (imageData == 0)
return osgDB::ReaderWriter::ReadResult::ERROR_IN_READING_FILE;
unsigned int pixelFormat;
switch (internalFormat) {
case 1:
pixelFormat = GL_LUMINANCE;
break;
case 2:
pixelFormat = GL_LUMINANCE_ALPHA;
break;
case 3:
pixelFormat = GL_RGB;
break;
default:
pixelFormat = GL_RGBA;
break;
}
osg::Image* img = new osg::Image;
img->setImage(
s,
t,
1,
internalFormat,
pixelFormat,
GL_UNSIGNED_BYTE,
imageData,
osg::Image::USE_NEW_DELETE);
return img;
}
// now register with Registry to instantiate the above
// reader/writer.
REGISTER_OSGPLUGIN(pcx, ReaderWriterPCX)