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 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
|
#include "stdafx.h"
#include "Ppm.h"
#include "Exception.h"
#include "Dither.h"
#include <math.h>
namespace graphics {
static Bool CODECALL ppmApplicable(IStream *from) {
const char *spec[] = {
"P",
"123456",
" \n\r\t",
};
const Nat len = ARRAY_COUNT(spec);
Buffer buffer = from->peek(storm::buffer(from->engine(), len));
if (!buffer.full())
return false;
for (Nat i = 0; i < len; i++) {
bool ok = false;
for (const char *at = spec[i]; *at; at++) {
ok |= byte(*at) == buffer[i];
}
if (!ok)
return false;
}
return true;
}
static FormatOptions *CODECALL ppmCreate(ImageFormat *f) {
return new (f) PPMOptions();
}
// Note: We go with ASCII by default, since the purpose of PPM is likely to write something that
// is trivial to read somewhere else.
PPMOptions::PPMOptions() : binary(false), mode(color) {}
PPMOptions::PPMOptions(Bool binary, Mode mode) : binary(binary), mode(mode) {}
void PPMOptions::toS(StrBuf *to) const {
*to << S("PPM: { ") << (binary ? S("binary") : S("ascii")) << S(", ");
switch (mode) {
case mono1:
*to << S("monochrome, 1-bit");
break;
case mono:
*to << S("monochrome");
break;
case color:
*to << S("color");
break;
}
*to << S(" }");
}
ImageFormat *ppmFormat(Engine &e) {
const wchar *exts[] = {
S("ppm"),
S("pbm"),
null
};
return new (e) ImageFormat(S("Portable Pixel Map"), exts, &ppmApplicable, &ppmCreate);
}
// Is this a whitespace character?
static bool whitespace(Byte ch) {
switch (ch) {
case ' ':
case '\n':
case '\r':
case '\t':
return true;
default:
return false;
}
}
// Is this a numeric character?
static bool numeric(Byte ch) {
return ch >= '0' && ch <= '9';
}
struct ReadState {
IStream *src;
Buffer buffer;
Nat pos;
ReadState(IStream *src) :
src(src),
buffer(src->read(1024)),
pos(0) {}
// Get the next byte in the file.
Byte next() {
if (pos >= buffer.filled()) {
pos = 0;
buffer.filled(0);
buffer = src->read(buffer);
if (buffer.empty())
throw new (src) ImageLoadError(S("Unexpected end of stream!"));
}
return buffer[pos++];
}
// Get the next character, ignoring comments.
Byte nextText() {
Byte r;
for (r = next(); r == '#'; r = next()) {
// In a comment. Skip until newline.
while (next() != '\n')
;
}
return r;
}
// Get the next number (in ASCII) from the file.
Nat nextNum() {
// Skip whitespace.
Byte ch;
while (whitespace(ch = nextText()))
;
// Read the number.
if (!numeric(ch))
throw new (src) ImageLoadError(S("Not a number!"));
Nat result = 0;
while (numeric(ch)) {
result *= 10;
result += ch - '0';
ch = nextText();
}
return result;
}
};
class WriteState {
public:
Image *image;
OStream *out;
Buffer buffer;
WriteState(Image *image, OStream *out) :
image(image),
out(out),
buffer(storm::buffer(out->engine(), 1024)) {}
~WriteState() {
flush();
}
void put(Byte b) {
if (buffer.full())
flush();
Nat p = buffer.filled();
buffer[p] = b;
buffer.filled(p + 1);
}
void put(const Byte *buf, Nat count) {
// We assume that 'count' is always smaller than the buffer.
if (buffer.filled() + count >= buffer.count())
flush();
Nat p = buffer.filled();
memcpy(buffer.dataPtr() + p, buf, count);
buffer.filled(p + count);
}
void put(const char *buf, Nat count) {
put((const Byte *)buf, count);
}
void putNum(Nat num, Nat minWidth) {
Byte buffer[11] = " 0";
Nat pos = 9;
while (num > 0) {
buffer[pos--] = '0' + num % 10;
num = num / 10;
}
pos = min(pos, 9 - minWidth);
put(buffer + pos + 1, 9 - pos);
}
void flush() {
if (!buffer.empty())
out->write(buffer);
buffer.filled(0);
}
};
struct Header {
// Mode: We use 1, 2 and 3 regardless if binary or ascii.
Byte mode;
// Size.
Nat width, height;
};
template <class T>
Image *loadMonoPPM(ReadState &src, const Header &header, T read) {
Image *out = new (src.src) Image(header.width, header.height);
for (Nat y = 0; y < header.height; y++) {
for (Nat x = 0; x < header.width; x++) {
Float c = read.next(src);
out->set(x, y, Color(c, c, c));
}
read.flush();
}
return out;
}
template <class T>
Image *loadColorPPM(ReadState &src, const Header &header, T read) {
Image *out = new (src.src) Image(header.width, header.height);
for (Nat y = 0; y < header.height; y++) {
for (Nat x = 0; x < header.width; x++) {
Float r = read.next(src);
Float g = read.next(src);
Float b = read.next(src);
out->set(x, y, Color(r, g, b));
}
read.flush();
}
return out;
}
template <class T>
Image *loadPPM(ReadState &src, const Header &header) {
Nat maxval = 0;
switch (header.mode) {
case 1:
return loadMonoPPM(src, header, typename T::Mono());
case 2:
maxval = src.nextNum();
return loadMonoPPM(src, header, typename T::Multi(maxval));
case 3:
maxval = src.nextNum();
return loadColorPPM(src, header, typename T::Multi(maxval));
default:
return null;
}
}
struct Raw {
struct Multi {
Float maxval;
Bool multibyte;
Multi(Nat maxval) : maxval(Float(maxval)), multibyte(maxval > 255) {}
Float next(ReadState &src) const {
Nat result = src.next();
if (multibyte)
result = (result << 8) | src.next();
return Float(result) / maxval;
}
void put(WriteState &to, Byte px) {
to.put(px);
}
void put(WriteState &to, Float pxData) {
Int value = Int(pxData * 255.0f);
put(to, Byte(max(255, min(0, value))));
}
void nextLine(WriteState &to) {
// Nothing to do...
(void)to;
}
void flush() const {}
};
struct Mono {
Byte data;
Byte fill;
DitherState dither;
Mono() : data(0), fill(0) {}
Float next(ReadState &src) {
if (fill == 0) {
data = src.next();
fill = 8;
}
fill--;
return Float(~(data >> fill) & 0x1);
}
void flush() {
fill = 0;
}
void put(WriteState &to, Float pxData) {
if (fill >= 8)
nextLine(to);
data <<= 1;
data |= dither.pixelValue(pxData) ? 0 : 1;
fill++;
}
void nextLine(WriteState &to) {
if (fill) {
data <<= 8 - fill;
to.put(data);
}
data = fill = 0;
}
};
};
struct Ascii {
struct Multi {
Float maxval;
Bool outFirst;
Multi(Nat maxval) : maxval(Float(maxval)), outFirst(true) {}
Float next(ReadState &src) const {
return Float(src.nextNum()) / maxval;
}
void flush() const {}
void put(WriteState &to, Byte pxData) {
if (!outFirst)
to.put(' ');
to.putNum(pxData, 3);
outFirst = false;
}
void put(WriteState &to, Float pxData) {
Int value = Int(floorf((pxData * 255.0f) + 0.5f));
put(to, Byte(min(255, max(0, value))));
}
void nextLine(WriteState &to) {
to.put('\n');
outFirst = true;
}
};
struct Mono {
Bool outFirst;
DitherState dither;
Mono() : outFirst(true) {}
Float next(ReadState &src) const {
return Float(src.nextNum());
}
void flush() const {}
void put(WriteState &to, Float pxData) {
if (!outFirst)
to.put(' ');
to.put(dither.pixelValue(pxData) ? '0' : '1');
outFirst = false;
}
void nextLine(WriteState &to) {
to.put('\n');
outFirst = true;
}
};
};
Image *PPMOptions::load(IStream *src) {
ReadState s(src);
if (s.next() != 'P')
throw new (this) ImageLoadError(S("Not a supported PPM file."));
Header header;
header.mode = s.next();
if (header.mode >= '1' && header.mode <= '6')
header.mode -= '0';
else
return null;
if (!whitespace(s.next()))
return null;
header.width = s.nextNum();
header.height = s.nextNum();
if (header.mode > 3) {
header.mode -= 3;
this->mode = Mode(header.mode);
this->binary = true;
return loadPPM<Raw>(s, header);
} else {
this->mode = Mode(header.mode);
this->binary = false;
return loadPPM<Ascii>(s, header);
}
}
template <class T>
void saveMonoPPM(WriteState &s, T write) {
// Now we can just put numbers:
for (Nat y = 0; y < s.image->height(); y++) {
for (Nat x = 0; x < s.image->width(); x++) {
Color color = s.image->get(x, y);
Float average = fromLinear(color.toLinear().brightness());
write.put(s, average);
}
write.nextLine(s);
}
}
template <class T>
void saveColorPPM(WriteState &s, T write) {
// Now we can just put numbers for RGB:
for (Nat y = 0; y < s.image->height(); y++) {
for (Nat x = 0; x < s.image->width(); x++) {
Byte *buf = s.image->buffer(x, y);
write.put(s, buf[0]);
write.put(s, buf[1]);
write.put(s, buf[2]);
}
write.nextLine(s);
}
}
template <class T>
void savePPM(WriteState &s, PPMOptions::Mode mode) {
switch (mode) {
case PPMOptions::mono1:
saveMonoPPM(s, typename T::Mono());
break;
case PPMOptions::mono:
// Second part of the header, max value.
s.put("255\n", 4);
saveMonoPPM(s, typename T::Multi(255));
break;
case PPMOptions::color:
// Second part of the header, max value.
s.put("255\n", 4);
saveColorPPM(s, typename T::Multi(255));
break;
}
}
void PPMOptions::save(Image *image, OStream *to) {
WriteState s(image, to);
// Write the header.
Byte header[4] = "P0\n";
header[1] += Byte(mode) + (binary ? 3 : 0);
s.put(header, 3);
s.putNum(image->width(), 1);
s.put(' ');
s.putNum(image->height(), 1);
s.put('\n');
if (binary) {
savePPM<Raw>(s, mode);
} else {
savePPM<Ascii>(s, mode);
}
}
}
|