| 1 |
//======================================================================== |
| 2 |
// |
| 3 |
// SplashBitmap.h |
| 4 |
// |
| 5 |
// Copyright 2003-2013 Glyph & Cog, LLC |
| 6 |
// |
| 7 |
//======================================================================== |
| 8 |
|
| 9 |
#ifndef SPLASHBITMAP_H |
| 10 |
#define SPLASHBITMAP_H |
| 11 |
|
| 12 |
#include <aconf.h> |
| 13 |
|
| 14 |
#include <stdio.h> |
| 15 |
#include <limits.h> |
| 16 |
// older compilers won't define SIZE_MAX in stdint.h without this |
| 17 |
#ifndef __STDC_LIMIT_MACROS |
| 18 |
# define __STDC_LIMIT_MACROS 1 |
| 19 |
#endif |
| 20 |
#include <stdint.h> |
| 21 |
#include "SplashTypes.h" |
| 22 |
|
| 23 |
//------------------------------------------------------------------------ |
| 24 |
|
| 25 |
// ssize_t isn't well-defined, so define our own |
| 26 |
#if SIZE_MAX == UINT_MAX |
| 27 |
typedef int SplashBitmapRowSize; |
| 28 |
# define SplashBitmapRowSizeMax INT_MAX |
| 29 |
#else |
| 30 |
typedef long long SplashBitmapRowSize; |
| 31 |
# define SplashBitmapRowSizeMax LLONG_MAX |
| 32 |
#endif |
| 33 |
|
| 34 |
//------------------------------------------------------------------------ |
| 35 |
// SplashBitmapMemCache |
| 36 |
//------------------------------------------------------------------------ |
| 37 |
|
| 38 |
#define splashBitmapMemCacheSize 4 |
| 39 |
|
| 40 |
struct SplashBitmapMemCacheEntry { |
| 41 |
void *data; |
| 42 |
int height; |
| 43 |
SplashBitmapRowSize rowSize; |
| 44 |
}; |
| 45 |
|
| 46 |
// This holds onto a small number of freed bitmaps so they can be |
| 47 |
// reused, avoiding system overhead for (re)allocating large chunks of |
| 48 |
// memory. It's not thread-safe, so it needs to be owned by a single |
| 49 |
// thread. |
| 50 |
class SplashBitmapMemCache { |
| 51 |
public: |
| 52 |
|
| 53 |
SplashBitmapMemCache(); |
| 54 |
~SplashBitmapMemCache(); |
| 55 |
|
| 56 |
private: |
| 57 |
|
| 58 |
void *alloc(int height, SplashBitmapRowSize rowSize); |
| 59 |
void free(void *data, int height, SplashBitmapRowSize rowSize); |
| 60 |
|
| 61 |
SplashBitmapMemCacheEntry cache[splashBitmapMemCacheSize]; |
| 62 |
|
| 63 |
friend class SplashBitmap; |
| 64 |
friend class SplashAlphaBitmap; |
| 65 |
}; |
| 66 |
|
| 67 |
//------------------------------------------------------------------------ |
| 68 |
// SplashBitmap |
| 69 |
//------------------------------------------------------------------------ |
| 70 |
|
| 71 |
class SplashBitmap { |
| 72 |
public: |
| 73 |
|
| 74 |
// Create a new bitmap. It will have <widthA> x <heightA> pixels in |
| 75 |
// color mode <modeA>. Rows will be padded out to a multiple of |
| 76 |
// <rowPad> bytes. If <topDown> is false, the bitmap will be stored |
| 77 |
// upside-down, i.e., with the last row first in memory. |
| 78 |
SplashBitmap(int widthA, int heightA, int rowPad, |
| 79 |
SplashColorMode modeA, GBool alphaA, |
| 80 |
GBool topDown, SplashBitmapMemCache *cacheA); |
| 81 |
|
| 82 |
~SplashBitmap(); |
| 83 |
|
| 84 |
int getWidth() { return width; } |
| 85 |
int getHeight() { return height; } |
| 86 |
SplashBitmapRowSize getRowSize() { return rowSize; } |
| 87 |
size_t getAlphaRowSize() { return alphaRowSize; } |
| 88 |
SplashColorMode getMode() { return mode; } |
| 89 |
SplashColorPtr getDataPtr() { return data; } |
| 90 |
Guchar *getAlphaPtr() { return alpha; } |
| 91 |
|
| 92 |
SplashError writePNMFile(char *fileName); |
| 93 |
SplashError writePNMFile(FILE *f); |
| 94 |
SplashError writeAlphaPGMFile(char *fileName); |
| 95 |
|
| 96 |
void getPixel(int x, int y, SplashColorPtr pixel); |
| 97 |
Guchar getAlpha(int x, int y); |
| 98 |
|
| 99 |
// Caller takes ownership of the bitmap data. The SplashBitmap |
| 100 |
// object is no longer valid -- the next call should be to the |
| 101 |
// destructor. |
| 102 |
SplashColorPtr takeData(); |
| 103 |
|
| 104 |
void doNotCache(); |
| 105 |
|
| 106 |
private: |
| 107 |
|
| 108 |
int width, height; // size of bitmap |
| 109 |
SplashBitmapRowSize rowSize; // size of one row of data, in bytes |
| 110 |
// - negative for bottom-up bitmaps |
| 111 |
size_t alphaRowSize; // size of one row of alpha, in bytes |
| 112 |
SplashColorMode mode; // color mode |
| 113 |
SplashColorPtr data; // pointer to row zero of the color data |
| 114 |
Guchar *alpha; // pointer to row zero of the alpha data |
| 115 |
// (always top-down) |
| 116 |
|
| 117 |
SplashBitmapMemCache *cache; |
| 118 |
|
| 119 |
friend class Splash; |
| 120 |
}; |
| 121 |
|
| 122 |
|
| 123 |
//------------------------------------------------------------------------ |
| 124 |
// SplashAlphaBitmap |
| 125 |
//------------------------------------------------------------------------ |
| 126 |
|
| 127 |
class SplashAlphaBitmap { |
| 128 |
public: |
| 129 |
|
| 130 |
// Create a new alpha bitmap with <widthA> x <heightA> pixels. |
| 131 |
SplashAlphaBitmap(int widthA, int heightA, SplashBitmapMemCache *cacheA); |
| 132 |
|
| 133 |
~SplashAlphaBitmap(); |
| 134 |
|
| 135 |
int getWidth() { return width; } |
| 136 |
int getHeight() { return height; } |
| 137 |
size_t getAlphaRowSize() { return alphaRowSize; } |
| 138 |
Guchar *getAlphaPtr() { return alpha; } |
| 139 |
|
| 140 |
private: |
| 141 |
|
| 142 |
int width, height; // size of bitmap |
| 143 |
size_t alphaRowSize; // size of one row, in bytes |
| 144 |
Guchar *alpha; // pointer to the alpha data |
| 145 |
|
| 146 |
SplashBitmapMemCache *cache; |
| 147 |
|
| 148 |
friend class Splash; |
| 149 |
}; |
| 150 |
|
| 151 |
#endif |