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 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
|
// ==========================================================
// Photoshop Loader
//
// Design and implementation by
// - Floris van den Berg (flvdberg@wxs.nl)
// - Thorsten Radde (support@IdealSoftware.com)
//
// Based on public domain code created and
// published by Thatcher Ulrich (ulrich@world.std.com)
//
// This file is part of FreeImage 3
//
// COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
// THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
// OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
// CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
// THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
// SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
// PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
// THIS DISCLAIMER.
//
// Use at your own risk!
// ==========================================================
#include "FreeImage.h"
// ==========================================================
// Bitmap pointer access macros
// ==========================================================
#define BP_START(DIB, WIDTH, HEIGHT) \
int xxxscanline = 0; \
RGBQUAD *xxxbits = (RGBQUAD *)FreeImage_GetScanLine(DIB, HEIGHT - 1 - xxxscanline); \
RGBQUAD *xxxp = xxxbits;
#define BP_NEXT(DIB, WIDTH, HEIGHT) \
xxxp++; \
if (xxxp - xxxbits == WIDTH) { \
xxxscanline++; \
xxxbits = (RGBQUAD *)FreeImage_GetScanLine(DIB, HEIGHT - 1 - xxxscanline); \
xxxp = xxxbits; \
}
#define BP_SETVALUE(VALUE, OFFSET) \
((BYTE *)xxxp)[OFFSET] = (BYTE)VALUE;
// ==========================================================
// Internal functions
// ==========================================================
static unsigned
Read8(FreeImageIO *io, fi_handle handle) {
unsigned char i = 0;
io->read_proc(&i, 1, 1, handle);
return i;
}
static unsigned
Read16(FreeImageIO *io, fi_handle handle) {
// reads a two-byte big-endian integer from the given file and returns its value.
// assumes unsigned.
unsigned hi = Read8(io, handle);
unsigned lo = Read8(io, handle);
return lo + (hi << 8);
}
static unsigned
Read32(FreeImageIO *io, fi_handle handle) {
// reads a four-byte big-endian integer from the given file and returns its value.
// assumes unsigned.
unsigned b3 = Read8(io, handle);
unsigned b2 = Read8(io, handle);
unsigned b1 = Read8(io, handle);
unsigned b0 = Read8(io, handle);
return (b3 << 24) + (b2 << 16) + (b1 << 8) + b0;
}
// ----------------------------------------------------------
static void
ScanForResolution(float* hres, float* vres, FreeImageIO *io, fi_handle handle, int byte_count) {
// scans through the next byte_count bytes of the file, looking for an
// image resource block encoding the image's resolution. Returns the resolution(s),
// if found, in the pointed-to floats. Units are in pixels/meter.
while (byte_count) {
// read the image resource block header.
if (Read32(io, handle) != 0x3842494D /* "8BIM" */)
throw "image resource block has unknown signature";
int id = Read16(io, handle);
// skip the name.
int name_length = Read8(io, handle) | 1; // name_length must be odd, so that total including size byte is even.
io->seek_proc(handle, name_length, SEEK_CUR);
// get the size of the data block.
int data_size = Read32(io, handle);
if ((data_size & 1) == 1)
data_size++; // block size must be even.
// account for header size.
byte_count -= 11 + name_length;
// if this block is a resolution info structure, then get the resolution.
if (id == 0x03ED) {
int junk;
int hres_fixed = Read32(io, handle);
junk = Read16(io, handle); // display units of hres.
junk = Read16(io, handle); // display units of width.
int vres_fixed = Read32(io, handle);
junk = Read16(io, handle); // display units of vres.
junk = Read16(io, handle); // display units of height.
byte_count -= data_size;
data_size -= 16;
// skip any extra bytes at the end of this block...
if (data_size > 0)
io->seek_proc(handle, data_size, SEEK_CUR);
// need to convert resolution figures from fixed point, pixels/inch
// to floating point, pixels/meter.
*hres = hres_fixed * ((float)39.4 / (float)65536.0);
*vres = vres_fixed * ((float)39.4 / (float)65536.0);
} else {
// skip the rest of this block.
io->seek_proc(handle, data_size, SEEK_CUR);
byte_count -= data_size;
}
}
}
// ----------------------------------------------------------
static FIBITMAP *
LoadPSDRGB(FreeImageIO *io, fi_handle handle, int width, int height, int channel_count) {
// skip the mode data. (it's the palette for indexed color; other info for other modes.)
long area = width * height;
// skip the reserved data
int reserved_data_count = Read32(io, handle);
if (reserved_data_count)
io->seek_proc(handle, reserved_data_count, SEEK_CUR);
// find out if the data is compressed
// 0: no compressiod
// 1: RLE compressed
unsigned compression = Read16(io, handle);
if ((compression > 1) || (compression < 0))
return NULL;
// some formatting information about the channels
const struct ChannelInfo {
int ofs, deflt;
} Channel[4] = {
{ FI_RGBA_RED, 0 }, // red
{ FI_RGBA_GREEN, 0 }, // green
{ FI_RGBA_BLUE, 0 }, // blue
{ FI_RGBA_ALPHA, 255 } // alpha
};
// Create the destination bitmap
FIBITMAP *dib = FreeImage_Allocate(width, height, 32, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK);
// finally, read the image data.
if (compression) {
// the RLE-compressed data is preceeded by a 2-byte data count for each row in the data
io->seek_proc(handle, height * channel_count * 2, SEEK_CUR);
// read the RLE data by channel
for (int channel = 0; channel < 4; channel++) {
const ChannelInfo &c = Channel[channel];
BP_START(dib, width, height)
if (channel >= channel_count) {
// fill this channel with default data.
for (int i = 0; i < area; i++) {
BP_SETVALUE(c.deflt, c.ofs);
BP_NEXT(dib, width, height)
}
} else {
// read the RLE data.
int count = 0;
while (count < area) {
int len = Read8(io, handle);
if (len == 128) {
// nop
} else if (len < 128) {
// copy next len + 1 bytes literally.
len++;
// check for buffer overrun
if ((count + len) > area) {
len = area - count;
}
count += len;
while (len) {
BP_SETVALUE(Read8(io, handle), c.ofs);
BP_NEXT(dib, width, height)
len--;
}
} else if (len > 128) {
// next -len + 1 bytes in the dest are replicated from next source byte.
// (interpret len as a negative 8-bit int.)
len ^= 0x0FF;
len += 2;
// check for buffer overrun
if ((count + len) > area) {
len = area - count;
}
count += len;
unsigned val = Read8(io, handle);
while (len) {
BP_SETVALUE(val, c.ofs);
BP_NEXT(dib, width, height)
len--;
}
}
}
}
}
} else {
// we're at the raw image data. it's each channel in order (Red, Green, Blue, Alpha, ...)
// where each channel consists of an 8-bit value for each pixel in the image.
for (int channel = 0; channel < 4; channel++) {
const ChannelInfo &c = Channel[channel];
BP_START(dib, width, height)
if (channel >= channel_count) {
// fill this channel with default data.
for (int i = 0; i < area; i++) {
BP_SETVALUE(c.deflt, c.ofs);
BP_NEXT(dib, width, height)
}
} else {
// read the data
for (int i = 0; i < area; i++) {
BP_SETVALUE(Read8(io, handle), c.ofs);
BP_NEXT(dib, width, height)
}
}
}
}
return dib;
}
// ==========================================================
// Plugin Interface
// ==========================================================
static int s_format_id;
// ==========================================================
// Plugin Implementation
// ==========================================================
static const char * DLL_CALLCONV
Format() {
return "PSD";
}
static const char * DLL_CALLCONV
Description() {
return "Adobe Photoshop";
}
static const char * DLL_CALLCONV
Extension() {
return "psd";
}
static const char * DLL_CALLCONV
MimeType() {
return "image/freeimage-psd";
}
static BOOL DLL_CALLCONV
Validate(FreeImageIO *io, fi_handle handle) {
return (Read32(io, handle) == 0x38425053);
}
static BOOL DLL_CALLCONV
SupportsExportDepth(int depth) {
return FALSE;
}
static BOOL DLL_CALLCONV
SupportsExportType(FREE_IMAGE_TYPE type) {
return FALSE;
}
// ----------------------------------------------------------
static FIBITMAP * DLL_CALLCONV
Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) {
try {
if (Read32(io, handle) == 0x38425053) {
if (Read16(io, handle) == 1) {
// 6 reserved bytes.
Read32(io, handle);
Read16(io, handle);
// Read the number of channels (R, G, B, A, etc).
unsigned channel_count = Read16(io, handle);
if (channel_count >= 0 && channel_count <= 16) {
unsigned height = Read32(io, handle);
unsigned width = Read32(io, handle);
if (Read16(io, handle) == 8) {
unsigned mode = Read16(io, handle);
// Get Resolution Information
float hres = 2835, vres = 2835; // 72 dpi
int mode_data_count = Read32(io, handle);
if (mode_data_count)
io->seek_proc(handle, mode_data_count, SEEK_CUR);
int resource_data_count = Read32(io, handle);
ScanForResolution(&hres, &vres, io, handle, resource_data_count);
// Valid options are:
// 0: Bitmap (not implemented)
// 1: Grayscale (not implemented)
// 2: Indexed color (not implemented)
// 3: RGB color
// 4: CMYK color (not implemented)
// 7: Multichannel (not implemented)
// 8: Duotone (not implemented)
// 9: Lab color (not implemented)
FIBITMAP *pBitmap = NULL;
switch (mode) {
case 3 :
pBitmap = LoadPSDRGB(io, handle, width, height, channel_count);
break;
default :
throw "color mode not supported";
}
if (pBitmap) {
FreeImage_SetDotsPerMeterX(pBitmap, (LONG)hres);
FreeImage_SetDotsPerMeterY(pBitmap, (LONG)vres);
}
return pBitmap;
}
}
}
}
} catch(const char *message) {
FreeImage_OutputMessageProc(s_format_id, message);
}
return NULL;
}
// ==========================================================
// Init
// ==========================================================
void DLL_CALLCONV
InitPSD(Plugin *plugin, int format_id) {
s_format_id = format_id;
plugin->format_proc = Format;
plugin->description_proc = Description;
plugin->extension_proc = Extension;
plugin->regexpr_proc = NULL;
plugin->open_proc = NULL;
plugin->close_proc = NULL;
plugin->pagecount_proc = NULL;
plugin->pagecapability_proc = NULL;
plugin->load_proc = Load;
plugin->save_proc = NULL;
plugin->validate_proc = Validate;
plugin->mime_proc = MimeType;
plugin->supports_export_bpp_proc = SupportsExportDepth;
plugin->supports_export_type_proc = SupportsExportType;
plugin->supports_icc_profiles_proc = NULL; // not implemented yet;
}
|