| 1 |
//======================================================================== |
| 2 |
// |
| 3 |
// SplashBitmap.cc |
| 4 |
// |
| 5 |
// Copyright 2003-2013 Glyph & Cog, LLC |
| 6 |
// |
| 7 |
//======================================================================== |
| 8 |
|
| 9 |
#include <aconf.h> |
| 10 |
|
| 11 |
#include <stdio.h> |
| 12 |
#include <limits.h> |
| 13 |
#include "gmem.h" |
| 14 |
#include "gmempp.h" |
| 15 |
#include "gfile.h" |
| 16 |
#include "Trace.h" |
| 17 |
#include "SplashErrorCodes.h" |
| 18 |
#include "SplashBitmap.h" |
| 19 |
|
| 20 |
//------------------------------------------------------------------------ |
| 21 |
// SplashBitmapMemCache |
| 22 |
//------------------------------------------------------------------------ |
| 23 |
|
| 24 |
#define splashBitmapMemCacheThreshold 4000000 |
| 25 |
|
| 26 |
SplashBitmapMemCache::SplashBitmapMemCache() { |
| 27 |
for (int i = 0; i < splashBitmapMemCacheSize; ++i) { |
| 28 |
cache[i].data = NULL; |
| 29 |
cache[i].height = 0; |
| 30 |
cache[i].rowSize = 0; |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
SplashBitmapMemCache::~SplashBitmapMemCache() { |
| 35 |
for (int i = 0; i < splashBitmapMemCacheSize; ++i) { |
| 36 |
if (cache[i].data) { |
| 37 |
gfree(cache[i].data); |
| 38 |
cache[i].data = NULL; |
| 39 |
cache[i].height = 0; |
| 40 |
cache[i].rowSize = 0; |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
void *SplashBitmapMemCache::alloc(int height, SplashBitmapRowSize rowSize) { |
| 46 |
// if the block is small, just allocate it |
| 47 |
if (rowSize < splashBitmapMemCacheThreshold / height) { |
| 48 |
return gmallocn64(height, rowSize); |
| 49 |
} |
| 50 |
|
| 51 |
// if a match is found in the cache, return it |
| 52 |
for (int i = splashBitmapMemCacheSize - 1; i >= 0; --i) { |
| 53 |
if (cache[i].data && |
| 54 |
cache[i].height == height && |
| 55 |
cache[i].rowSize == rowSize) { |
| 56 |
void *data = cache[i].data; |
| 57 |
for (int j = i; j < splashBitmapMemCacheSize - 1; ++j) { |
| 58 |
cache[j] = cache[j + 1]; |
| 59 |
} |
| 60 |
cache[splashBitmapMemCacheSize - 1].data = NULL; |
| 61 |
cache[splashBitmapMemCacheSize - 1].height = 0; |
| 62 |
cache[splashBitmapMemCacheSize - 1].rowSize = 0; |
| 63 |
return data; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
// if no match was found, clear the cache and allcoate the block |
| 68 |
for (int i = 0; i < splashBitmapMemCacheSize; ++i) { |
| 69 |
if (cache[i].data) { |
| 70 |
gfree(cache[i].data); |
| 71 |
cache[i].data = NULL; |
| 72 |
cache[i].height = 0; |
| 73 |
cache[i].rowSize = 0; |
| 74 |
} |
| 75 |
} |
| 76 |
return gmallocn64(height, rowSize); |
| 77 |
} |
| 78 |
|
| 79 |
void SplashBitmapMemCache::free(void *data, int height, |
| 80 |
SplashBitmapRowSize rowSize) { |
| 81 |
if (rowSize < splashBitmapMemCacheThreshold / height) { |
| 82 |
gfree(data); |
| 83 |
return; |
| 84 |
} |
| 85 |
if (cache[0].data) { |
| 86 |
gfree(cache[0].data); |
| 87 |
} |
| 88 |
for (int i = 0; i < splashBitmapMemCacheSize - 1; ++i) { |
| 89 |
cache[i] = cache[i + 1]; |
| 90 |
} |
| 91 |
cache[splashBitmapMemCacheSize - 1].data = data; |
| 92 |
cache[splashBitmapMemCacheSize - 1].height = height; |
| 93 |
cache[splashBitmapMemCacheSize - 1].rowSize = rowSize; |
| 94 |
} |
| 95 |
|
| 96 |
//------------------------------------------------------------------------ |
| 97 |
// SplashBitmap |
| 98 |
//------------------------------------------------------------------------ |
| 99 |
|
| 100 |
SplashBitmap::SplashBitmap(int widthA, int heightA, int rowPad, |
| 101 |
SplashColorMode modeA, GBool alphaA, |
| 102 |
GBool topDown, SplashBitmapMemCache *cacheA) { |
| 103 |
// NB: this code checks that rowSize fits in a signed 32-bit |
| 104 |
// integer, because some code (outside this class) makes that |
| 105 |
// assumption |
| 106 |
width = widthA; |
| 107 |
height = heightA; |
| 108 |
mode = modeA; |
| 109 |
rowSize = 0; // make gcc happy |
| 110 |
switch (mode) { |
| 111 |
case splashModeMono1: |
| 112 |
if (width <= 0) { |
| 113 |
gMemError("invalid bitmap width"); |
| 114 |
} |
| 115 |
rowSize = (width + 7) >> 3; |
| 116 |
break; |
| 117 |
case splashModeMono8: |
| 118 |
if (width <= 0) { |
| 119 |
gMemError("invalid bitmap width"); |
| 120 |
} |
| 121 |
rowSize = width; |
| 122 |
break; |
| 123 |
case splashModeRGB8: |
| 124 |
case splashModeBGR8: |
| 125 |
if (width <= 0 || width > INT_MAX / 3) { |
| 126 |
gMemError("invalid bitmap width"); |
| 127 |
} |
| 128 |
rowSize = (SplashBitmapRowSize)width * 3; |
| 129 |
break; |
| 130 |
#if SPLASH_CMYK |
| 131 |
case splashModeCMYK8: |
| 132 |
if (width <= 0 || width > INT_MAX / 4) { |
| 133 |
gMemError("invalid bitmap width"); |
| 134 |
} |
| 135 |
rowSize = (SplashBitmapRowSize)width * 4; |
| 136 |
break; |
| 137 |
#endif |
| 138 |
} |
| 139 |
rowSize += rowPad - 1; |
| 140 |
rowSize -= rowSize % rowPad; |
| 141 |
|
| 142 |
traceAlloc(this, "alloc bitmap: %d x %d x %d %s -> %lld bytes", |
| 143 |
width, height, splashColorModeNComps[mode], |
| 144 |
alphaA ? "with alpha" : "without alpha", |
| 145 |
height * rowSize + (alphaA ? height * width : 0)); |
| 146 |
|
| 147 |
cache = cacheA; |
| 148 |
if (cache) { |
| 149 |
data = (SplashColorPtr)cache->alloc(height, rowSize); |
| 150 |
} else { |
| 151 |
data = (SplashColorPtr)gmallocn64(height, rowSize); |
| 152 |
} |
| 153 |
if (!topDown) { |
| 154 |
data += (height - 1) * rowSize; |
| 155 |
rowSize = -rowSize; |
| 156 |
} |
| 157 |
if (alphaA) { |
| 158 |
alphaRowSize = width; |
| 159 |
if (cache) { |
| 160 |
alpha = (Guchar *)cache->alloc(height, alphaRowSize); |
| 161 |
} else { |
| 162 |
alpha = (Guchar *)gmallocn64(height, alphaRowSize); |
| 163 |
} |
| 164 |
} else { |
| 165 |
alphaRowSize = 0; |
| 166 |
alpha = NULL; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
SplashBitmap::~SplashBitmap() { |
| 171 |
traceFree(this, "free bitmap"); |
| 172 |
if (data && rowSize < 0) { |
| 173 |
rowSize = -rowSize; |
| 174 |
data -= (height - 1) * rowSize; |
| 175 |
} |
| 176 |
if (cache) { |
| 177 |
cache->free(data, height, rowSize); |
| 178 |
if (alpha) { |
| 179 |
cache->free(alpha, height, alphaRowSize); |
| 180 |
} |
| 181 |
} else { |
| 182 |
gfree(data); |
| 183 |
gfree(alpha); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
SplashError SplashBitmap::writePNMFile(char *fileName) { |
| 188 |
FILE *f; |
| 189 |
SplashError err; |
| 190 |
|
| 191 |
if (!(f = openFile(fileName, "wb"))) { |
| 192 |
return splashErrOpenFile; |
| 193 |
} |
| 194 |
err = writePNMFile(f); |
| 195 |
fclose(f); |
| 196 |
return err; |
| 197 |
} |
| 198 |
|
| 199 |
SplashError SplashBitmap::writePNMFile(FILE *f) { |
| 200 |
SplashColorPtr row, p; |
| 201 |
int x, y; |
| 202 |
|
| 203 |
switch (mode) { |
| 204 |
|
| 205 |
case splashModeMono1: |
| 206 |
fprintf(f, "P4\n%d %d\n", width, height); |
| 207 |
row = data; |
| 208 |
for (y = 0; y < height; ++y) { |
| 209 |
p = row; |
| 210 |
for (x = 0; x < width; x += 8) { |
| 211 |
fputc(*p ^ 0xff, f); |
| 212 |
++p; |
| 213 |
} |
| 214 |
row += rowSize; |
| 215 |
} |
| 216 |
break; |
| 217 |
|
| 218 |
case splashModeMono8: |
| 219 |
fprintf(f, "P5\n%d %d\n255\n", width, height); |
| 220 |
row = data; |
| 221 |
for (y = 0; y < height; ++y) { |
| 222 |
fwrite(row, 1, width, f); |
| 223 |
row += rowSize; |
| 224 |
} |
| 225 |
break; |
| 226 |
|
| 227 |
case splashModeRGB8: |
| 228 |
fprintf(f, "P6\n%d %d\n255\n", width, height); |
| 229 |
row = data; |
| 230 |
for (y = 0; y < height; ++y) { |
| 231 |
fwrite(row, 1, 3 * width, f); |
| 232 |
row += rowSize; |
| 233 |
} |
| 234 |
break; |
| 235 |
|
| 236 |
case splashModeBGR8: |
| 237 |
fprintf(f, "P6\n%d %d\n255\n", width, height); |
| 238 |
row = data; |
| 239 |
for (y = 0; y < height; ++y) { |
| 240 |
p = row; |
| 241 |
for (x = 0; x < width; ++x) { |
| 242 |
fputc(splashBGR8R(p), f); |
| 243 |
fputc(splashBGR8G(p), f); |
| 244 |
fputc(splashBGR8B(p), f); |
| 245 |
p += 3; |
| 246 |
} |
| 247 |
row += rowSize; |
| 248 |
} |
| 249 |
break; |
| 250 |
|
| 251 |
#if SPLASH_CMYK |
| 252 |
case splashModeCMYK8: |
| 253 |
fprintf(f, "P7\n"); |
| 254 |
fprintf(f, "WIDTH %d\n", width); |
| 255 |
fprintf(f, "HEIGHT %d\n", height); |
| 256 |
fprintf(f, "DEPTH 4\n"); |
| 257 |
fprintf(f, "MAXVAL 255\n"); |
| 258 |
fprintf(f, "TUPLTYPE CMYK\n"); |
| 259 |
fprintf(f, "ENDHDR\n"); |
| 260 |
row = data; |
| 261 |
for (y = 0; y < height; ++y) { |
| 262 |
fwrite(row, 1, 4 * width, f); |
| 263 |
row += rowSize; |
| 264 |
} |
| 265 |
break; |
| 266 |
#endif |
| 267 |
|
| 268 |
} |
| 269 |
|
| 270 |
return splashOk; |
| 271 |
} |
| 272 |
|
| 273 |
SplashError SplashBitmap::writeAlphaPGMFile(char *fileName) { |
| 274 |
FILE *f; |
| 275 |
|
| 276 |
if (!alpha) { |
| 277 |
return splashErrModeMismatch; |
| 278 |
} |
| 279 |
if (!(f = openFile(fileName, "wb"))) { |
| 280 |
return splashErrOpenFile; |
| 281 |
} |
| 282 |
fprintf(f, "P5\n%d %d\n255\n", width, height); |
| 283 |
fwrite(alpha, 1, width * height, f); |
| 284 |
fclose(f); |
| 285 |
return splashOk; |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
void SplashBitmap::getPixel(int x, int y, SplashColorPtr pixel) { |
| 290 |
SplashColorPtr p; |
| 291 |
|
| 292 |
if (y < 0 || y >= height || x < 0 || x >= width) { |
| 293 |
return; |
| 294 |
} |
| 295 |
switch (mode) { |
| 296 |
case splashModeMono1: |
| 297 |
p = &data[y * rowSize + (x >> 3)]; |
| 298 |
pixel[0] = (p[0] & (0x80 >> (x & 7))) ? 0xff : 0x00; |
| 299 |
break; |
| 300 |
case splashModeMono8: |
| 301 |
p = &data[y * rowSize + x]; |
| 302 |
pixel[0] = p[0]; |
| 303 |
break; |
| 304 |
case splashModeRGB8: |
| 305 |
p = &data[y * rowSize + 3 * x]; |
| 306 |
pixel[0] = p[0]; |
| 307 |
pixel[1] = p[1]; |
| 308 |
pixel[2] = p[2]; |
| 309 |
break; |
| 310 |
case splashModeBGR8: |
| 311 |
p = &data[y * rowSize + 3 * x]; |
| 312 |
pixel[0] = p[2]; |
| 313 |
pixel[1] = p[1]; |
| 314 |
pixel[2] = p[0]; |
| 315 |
break; |
| 316 |
#if SPLASH_CMYK |
| 317 |
case splashModeCMYK8: |
| 318 |
p = &data[y * rowSize + 4 * x]; |
| 319 |
pixel[0] = p[0]; |
| 320 |
pixel[1] = p[1]; |
| 321 |
pixel[2] = p[2]; |
| 322 |
pixel[3] = p[3]; |
| 323 |
break; |
| 324 |
#endif |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
Guchar SplashBitmap::getAlpha(int x, int y) { |
| 329 |
return alpha[y * (size_t)width + x]; |
| 330 |
} |
| 331 |
|
| 332 |
SplashColorPtr SplashBitmap::takeData() { |
| 333 |
SplashColorPtr data2; |
| 334 |
|
| 335 |
data2 = data; |
| 336 |
data = NULL; |
| 337 |
return data2; |
| 338 |
} |
| 339 |
|
| 340 |
void SplashBitmap::doNotCache() { |
| 341 |
cache = NULL; |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
//------------------------------------------------------------------------ |
| 346 |
// SplashAlphaBitmap |
| 347 |
//------------------------------------------------------------------------ |
| 348 |
|
| 349 |
SplashAlphaBitmap::SplashAlphaBitmap(int widthA, int heightA, |
| 350 |
SplashBitmapMemCache *cacheA) { |
| 351 |
width = widthA; |
| 352 |
height = heightA; |
| 353 |
alphaRowSize = width; |
| 354 |
|
| 355 |
traceAlloc(this, "alloc alpha bitmap: %d x %d -> %lld bytes", |
| 356 |
width, height, height * alphaRowSize); |
| 357 |
|
| 358 |
cache = cacheA; |
| 359 |
if (cache) { |
| 360 |
alpha = (Guchar *)cache->alloc(height, alphaRowSize); |
| 361 |
} else { |
| 362 |
alpha = (Guchar *)gmallocn64(height, alphaRowSize); |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
SplashAlphaBitmap::~SplashAlphaBitmap() { |
| 367 |
traceFree(this, "free alpha bitmap"); |
| 368 |
if (cache) { |
| 369 |
cache->free(alpha, height, alphaRowSize); |
| 370 |
} else { |
| 371 |
gfree(alpha); |
| 372 |
} |
| 373 |
} |