File: pcx.cc

package info (click to toggle)
exactimage 1.2.1-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,048 kB
  • sloc: cpp: 35,940; ansic: 1,952; xml: 1,447; makefile: 338; perl: 138; sh: 110; python: 45; php: 37; ruby: 12
file content (315 lines) | stat: -rw-r--r-- 7,965 bytes parent folder | download | duplicates (5)
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
/*
 * C++ PCX library.
 * Copyright (C) 2008 - 2016 René Rebe, ExactCODE GmbH Germany
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2. A copy of the GNU General
 * Public License can be found in the file LICENSE.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANT-
 * ABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 *
 * Alternatively, commercial licensing options are available from the
 * copyright holder ExactCODE GmbH Germany.
 *
 */

#include <iostream>

#include <string.h>
#include <ctype.h>

#include "pcx.hh"

#include "Colorspace.hh"
#include "Endianess.hh"
#include "stdint.h"

using Exact::EndianessConverter;
using Exact::LittleEndianTraits;

#ifdef _MSC_VER
#pragma pack(push, 1)
#endif

typedef struct
{
  uint8_t Manufacturer;
  uint8_t Version;
  uint8_t Encoding;
  uint8_t BitsPerPixel;
  
  EndianessConverter<uint16_t,LittleEndianTraits> WindowXmin;
  EndianessConverter<uint16_t,LittleEndianTraits> WindowYmin;
  
  EndianessConverter<uint16_t,LittleEndianTraits> WindowXmax;
  EndianessConverter<uint16_t,LittleEndianTraits> WindowYmax;
  
  EndianessConverter<uint16_t,LittleEndianTraits> HDpi;
  EndianessConverter<uint16_t,LittleEndianTraits> VDpi;
  
  uint8_t Colormap[16][3];
  uint8_t Reserved;
  
  uint8_t NPlanes;
  
  EndianessConverter<uint16_t,LittleEndianTraits> BytesPerLine;
  // 0: udef, 1: color, 2: grayscale
  EndianessConverter<uint16_t,LittleEndianTraits> PaletteInfo;

  EndianessConverter<uint16_t,LittleEndianTraits> HScreenSize;
  EndianessConverter<uint16_t,LittleEndianTraits> VScreenSize;

  uint8_t Filler[54];
}
#ifdef __GNUC__
__attribute__((packed))
#endif
PCXHeader;

#ifdef _MSC_VER
#pragma pack(pop)
#endif

int PCXCodec::readImage(std::istream* stream,
			 Image& image, const std::string& decompress)
{
  if (stream->peek() != 0x0a)
    return false;
  stream->get();
  if ((unsigned)stream->peek() > 5) {
    stream->unget();
    return false;
  }
  stream->unget();
  
  PCXHeader header;
  if (!stream->read((char*)&header, sizeof(header)))
    return false;

  switch (header.BitsPerPixel)
   {
   case 1:
   case 8:
   case 16:
   case 24:
   case 32:
     break;
   default:
     std::cerr << "PCX invalid bit-depth: " << header.BitsPerPixel << std::endl;
     goto no_pcx;
   };

  switch (header.NPlanes)
   {
   case 1:
   case 3:
   case 4:
     break;
   default:
     std::cerr << "PCX invalid plane count: " << header.NPlanes << std::endl;
     goto no_pcx;
   };
  
  image.bps = header.BitsPerPixel;
  image.spp = header.NPlanes;
  if (image.spp == 4 && image.bps == 1)
    image.spp = 1, image.bps = 4;
  image.setResolution(header.HDpi, header.VDpi);
  
  image.resize(header.WindowXmax - header.WindowXmin + 1,
	       header.WindowYmax - header.WindowYmin + 1);
  
  //std::cerr << image.w << "x" << image.h << std::endl;
  //std::cerr << "BytesPerLine: " << (int)header.BytesPerLine << std::endl;
  /*std::cerr << "Version: " << (int)header.Version
	    << ", PaletteInfo: " << header.PaletteInfo
	    << ", BitsPerPixel: " << (int)header.BitsPerPixel
	    << ", NPlanes: " << (int)header.NPlanes
	    << ", Encoding: " << (int)header.Encoding << std::endl;*/
  
  // TODO: more buffer checks
  {
    const bool plane_packing = header.NPlanes > 1;
    uint8_t* scanline = (plane_packing ?
			 new uint8_t[header.BytesPerLine * header.NPlanes] :
			 image.getRawData());
    for (int y = 0; y < image.h; ++y)
      {
	for (int i = 0; i < header.BytesPerLine * header.NPlanes;)
	  {
	    uint8_t n = 1, v = stream->get();
	    if ((header.Encoding == 1) && (v & 0xC0) == 0xC0) {
	      n = v ^ 0xC0;
	      v = stream->get();
	    }
	    
	    while (n-- > 0 && i < header.BytesPerLine * header.NPlanes)
	      {
		scanline[i++] = v;
	      }
	  }
	
	if (plane_packing) // re-pack planes
	  {
            const unsigned bits = header.BitsPerPixel;
	    const unsigned planes = header.NPlanes;
	    uint8_t* dst = image.getRawData() + image.stride() * y;
	    
	    // TODO: share all bit packers in common code
	    struct BitPacker {
	      uint8_t* ptr;
	      int p;
	      const uint8_t bits, mask;
	      const bool msb_first;
	      
	      BitPacker(uint8_t* _ptr, int _bits, bool _msb_first = true)
		: ptr(_ptr), p(_msb_first ? 8 - _bits : 0),
		  bits(_bits), mask((1 << _bits) - 1), msb_first(_msb_first)
	      {
	      }
	      
	      void push(uint8_t v)
	      {
		if (!msb_first) {
		  *ptr = (*ptr & ~(mask << p)) | ((v & mask) << p);
		  p += bits;
		  if (p >= 8)
		    p = 0, ++ptr;
		} else {
		  *ptr = (*ptr & ~(mask << p)) | ((v & mask) << p);
		  p -= bits;
		  if (p < 0)
		    p = 8 - bits, ++ptr;
		}
	      }
	    };
	    BitPacker packer(dst, bits == 8 ? bits : bits * planes);
	    
	    for (int i = 0; i < image.w; ++i)
	      {
		uint8_t v = 0, mask = (1 << bits) - 1;
		unsigned b = 0;
		for (unsigned p = 0; p < planes; ++p)
		  {
		    uint8_t* src = scanline + p * header.BytesPerLine;
		    int idx = i * bits / 8;
		    int bit = i * bits % 8;
		    bit = 8 - bits - bit;
		    v |= ((src[idx] >> bit) & mask) << b;
                    b += bits;
		    if (b >= (bits == 8 ? bits : bits * planes)) {
		      packer.push(v);
		      b = v = 0;
		    }
		  }
	      }
	  }
	else // in-memory write
	  {
	    scanline += image.stride();
	  }
      }
    if (plane_packing)
      delete[] scanline;
  }

  if (image.spp == 1)
  {
    uint16_t rmap[256], gmap[256], bmap[256];
    
    const uint8_t colormap_magic = stream->get();
    if (colormap_magic == 0x0c) {
      //std::cerr << "reading colormap" << std::endl;
      for (int i = 0; i < 256; ++i)
	{
	  uint8_t entry[3];
	  stream->read((char*)entry, sizeof(entry));
	  rmap[i] = entry[0] * 0xffff / 0xff;
	  gmap[i] = entry[1] * 0xffff / 0xff;
	  bmap[i] = entry[2] * 0xffff / 0xff;
	}
      if (!stream->good()) {
	std::cerr << "error reading PCX colormap" << std::endl;
	return false;
      }
      
      colorspace_de_palette (image, 256, rmap, gmap, bmap);
    }
    else if (header.PaletteInfo == 1 || header.PaletteInfo == 2)
      {
	const int ncolors = 1 << image.bps;
	for (int i = 0; i < ncolors; ++i)
	  {
	    rmap[i] = header.Colormap[i][0] * 0xffff / 0xff;
	    gmap[i] = header.Colormap[i][1] * 0xffff / 0xff;
	    bmap[i] = header.Colormap[i][2] * 0xffff / 0xff;
	  }
	colorspace_de_palette(image, ncolors, rmap, gmap, bmap);
      }
  }
  
  return true;
  
no_pcx:
  stream->seekg(0);
  return false;
}

bool PCXCodec::writeImage(std::ostream* stream, Image& image, int quality,
			  const std::string& compress)
{
  PCXHeader header;
  header.Manufacturer = 10;
  header.Version = 5; // TODO: save older versions if not RGB
  
  header.Encoding = 0; // 1: RLE
  header.NPlanes = image.spp;
  header.BytesPerLine = image.stride() / image.spp;
  header.BitsPerPixel = image.bps;
  header.PaletteInfo = 0;
  
  switch (header.BitsPerPixel)
    {
    case 1:
    case 8:
    case 16:
    case 24:
    case 32:
     break;
    default:
      std::cerr << "unsupported PCX bit-depth" << std::endl;
      return false;
   };
  
  header.HDpi = image.resolutionX();
  header.VDpi = image.resolutionY();
  header.WindowXmin = 0;
  header.WindowYmin = 0;
  header.WindowXmax = image.width() - 1;
  header.WindowYmax = image.height() - 1;
  
  stream->write((char*)&header, sizeof(header));
  
  // write "un"compressed image data
  // TODO: RLE compress
  for (int y = 0; y < image.h; ++y)
    {
      for (int plane = 0; plane < image.spp; ++plane)
	{
	  uint8_t* data = image.getRawData() + image.stride() * y + plane;
	  for (int x = 0; x < image.w; ++x)
	    {
	      stream->write((char*)data, 1);
	      data += image.spp;
	    }
	}
    }
  
  return true;
}

PCXCodec pcx_codec;