| 1 |
//---------------------------------------------------------------------------- |
| 2 |
// |
| 3 |
//---------------------------------------------------------------------------- |
| 4 |
// Contact: [email protected] |
| 5 |
// [email protected] |
| 6 |
//---------------------------------------------------------------------------- |
| 7 |
// |
| 8 |
// class pixel_map |
| 9 |
// |
| 10 |
//---------------------------------------------------------------------------- |
| 11 |
|
| 12 |
#include <cstring> |
| 13 |
#include <Carbon.h> |
| 14 |
#include <QuickTimeComponents.h> |
| 15 |
#include <ImageCompression.h> |
| 16 |
#include "platform/mac/agg_mac_pmap.h" |
| 17 |
#include "agg_basics.h" |
| 18 |
|
| 19 |
namespace agg |
| 20 |
{ |
| 21 |
|
| 22 |
//------------------------------------------------------------------------ |
| 23 |
pixel_map::~pixel_map() |
| 24 |
{ |
| 25 |
destroy(); |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
//------------------------------------------------------------------------ |
| 30 |
pixel_map::pixel_map() : |
| 31 |
m_pmap(0), |
| 32 |
m_buf(0), |
| 33 |
m_bpp(0), |
| 34 |
m_img_size(0) |
| 35 |
|
| 36 |
{ |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
//------------------------------------------------------------------------ |
| 41 |
void pixel_map::destroy() |
| 42 |
{ |
| 43 |
delete[] m_buf; |
| 44 |
m_buf = NULL; |
| 45 |
if (m_pmap != nil) |
| 46 |
{ |
| 47 |
DisposeGWorld(m_pmap); |
| 48 |
m_pmap = nil; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
//------------------------------------------------------------------------ |
| 54 |
void pixel_map::create(unsigned width, |
| 55 |
unsigned height, |
| 56 |
org_e org, |
| 57 |
unsigned clear_val) |
| 58 |
{ |
| 59 |
destroy(); |
| 60 |
if(width == 0) width = 1; |
| 61 |
if(height == 0) height = 1; |
| 62 |
m_bpp = org; |
| 63 |
|
| 64 |
Rect r; |
| 65 |
int row_bytes = calc_row_len (width, m_bpp); |
| 66 |
MacSetRect(&r, 0, 0, width, height); |
| 67 |
m_buf = new unsigned char[m_img_size = row_bytes * height]; |
| 68 |
// The Quicktime version for creating GWorlds is more flexible than the classical function. |
| 69 |
QTNewGWorldFromPtr (&m_pmap, m_bpp, &r, nil, nil, 0, m_buf, row_bytes); |
| 70 |
|
| 71 |
// create_gray_scale_palette(m_pmap); I didn't care about gray scale palettes so far. |
| 72 |
if(clear_val <= 255) |
| 73 |
{ |
| 74 |
std::memset(m_buf, clear_val, m_img_size); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
//------------------------------------------------------------------------ |
| 81 |
void pixel_map::clear(unsigned clear_val) |
| 82 |
{ |
| 83 |
if(m_buf) std::memset(m_buf, clear_val, m_img_size); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
//static |
| 88 |
//This function is just copied from the Win32 plattform support. |
| 89 |
//Is also seems to be appropriate for MacOS as well, but it is not |
| 90 |
//thouroughly tested so far. |
| 91 |
//------------------------------------------------------------------------ |
| 92 |
|
| 93 |
unsigned pixel_map::calc_row_len(unsigned width, unsigned bits_per_pixel) |
| 94 |
{ |
| 95 |
unsigned n = width; |
| 96 |
unsigned k; |
| 97 |
|
| 98 |
switch(bits_per_pixel) |
| 99 |
{ |
| 100 |
case 1: k = n; |
| 101 |
n = n >> 3; |
| 102 |
if(k & 7) n++; |
| 103 |
break; |
| 104 |
|
| 105 |
case 4: k = n; |
| 106 |
n = n >> 1; |
| 107 |
if(k & 3) n++; |
| 108 |
break; |
| 109 |
|
| 110 |
case 8: |
| 111 |
break; |
| 112 |
|
| 113 |
case 16: n = n << 1; |
| 114 |
break; |
| 115 |
|
| 116 |
case 24: n = (n << 1) + n; |
| 117 |
break; |
| 118 |
|
| 119 |
case 32: n = n << 2; |
| 120 |
break; |
| 121 |
|
| 122 |
default: n = 0; |
| 123 |
break; |
| 124 |
} |
| 125 |
return ((n + 3) >> 2) << 2; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
//------------------------------------------------------------------------ |
| 132 |
void pixel_map::draw(WindowRef window, const Rect *device_rect, const Rect *pmap_rect) const |
| 133 |
{ |
| 134 |
if(m_pmap == nil || m_buf == NULL) return; |
| 135 |
|
| 136 |
PixMapHandle pm = GetGWorldPixMap (m_pmap); |
| 137 |
CGrafPtr port = GetWindowPort (window); |
| 138 |
Rect dest_rect; |
| 139 |
|
| 140 |
// Again, I used the Quicktime version. |
| 141 |
// Good old 'CopyBits' does better interpolation when scaling |
| 142 |
// but does not support all pixel depths. |
| 143 |
MacSetRect (&dest_rect, 0, 0, this->width(), this->height()); |
| 144 |
ImageDescriptionHandle image_description; |
| 145 |
MakeImageDescriptionForPixMap (pm, &image_description); |
| 146 |
if (image_description != nil) |
| 147 |
{ |
| 148 |
DecompressImage (GetPixBaseAddr (pm), image_description, GetPortPixMap (port), nil, &dest_rect, ditherCopy, nil); |
| 149 |
DisposeHandle ((Handle) image_description); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
//------------------------------------------------------------------------ |
| 155 |
void pixel_map::draw(WindowRef window, int x, int y, double scale) const |
| 156 |
{ |
| 157 |
if(m_pmap == nil || m_buf == NULL) return; |
| 158 |
unsigned width = (unsigned)(this->width() * scale); |
| 159 |
unsigned height = (unsigned)(this->height() * scale); |
| 160 |
Rect rect; |
| 161 |
SetRect (&rect, x, y, x + width, y + height); |
| 162 |
draw(window, &rect); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
//------------------------------------------------------------------------ |
| 168 |
void pixel_map::blend(WindowRef window, const Rect *device_rect, const Rect *bmp_rect) const |
| 169 |
{ |
| 170 |
draw (window, device_rect, bmp_rect); // currently just mapped to drawing method |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
//------------------------------------------------------------------------ |
| 175 |
void pixel_map::blend(WindowRef window, int x, int y, double scale) const |
| 176 |
{ |
| 177 |
draw(window, x, y, scale); // currently just mapped to drawing method |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
// I let Quicktime handle image import since it supports most popular |
| 182 |
// image formats such as: |
| 183 |
// *.psd, *.bmp, *.tif, *.png, *.jpg, *.gif, *.pct, *.pcx |
| 184 |
//------------------------------------------------------------------------ |
| 185 |
bool pixel_map::load_from_qt(const char *filename) |
| 186 |
{ |
| 187 |
FSSpec fss; |
| 188 |
OSErr err; |
| 189 |
|
| 190 |
// get file specification to application directory |
| 191 |
err = HGetVol(nil, &fss.vRefNum, &fss.parID); |
| 192 |
if (err == noErr) |
| 193 |
{ |
| 194 |
CopyCStringToPascal(filename, fss.name); |
| 195 |
GraphicsImportComponent gi; |
| 196 |
err = GetGraphicsImporterForFile (&fss, &gi); |
| 197 |
if (err == noErr) |
| 198 |
{ |
| 199 |
ImageDescriptionHandle desc; |
| 200 |
GraphicsImportGetImageDescription(gi, &desc); |
| 201 |
// For simplicity, all images are currently converted to 32 bit. |
| 202 |
// create an empty pixelmap |
| 203 |
short depth = 32; |
| 204 |
create ((**desc).width, (**desc).height, (org_e)depth, 0xff); |
| 205 |
DisposeHandle ((Handle)desc); |
| 206 |
// let Quicktime draw to pixelmap |
| 207 |
GraphicsImportSetGWorld(gi, m_pmap, nil); |
| 208 |
GraphicsImportDraw(gi); |
| 209 |
// Well, this is a hack. The graphics importer sets the alpha channel of the pixelmap to 0x00 |
| 210 |
// for imported images without alpha channel but this would cause agg to draw an invisible image. |
| 211 |
// set alpha channel to 0xff |
| 212 |
unsigned char * buf = m_buf; |
| 213 |
for (unsigned int size = 0; size < m_img_size; size += 4) |
| 214 |
{ |
| 215 |
*buf = 0xff; |
| 216 |
buf += 4; |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
return err == noErr; |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
//------------------------------------------------------------------------ |
| 226 |
bool pixel_map::save_as_qt(const char *filename) const |
| 227 |
{ |
| 228 |
FSSpec fss; |
| 229 |
OSErr err; |
| 230 |
|
| 231 |
// get file specification to application directory |
| 232 |
err = HGetVol(nil, &fss.vRefNum, &fss.parID); |
| 233 |
if (err == noErr) |
| 234 |
{ |
| 235 |
GraphicsExportComponent ge; |
| 236 |
CopyCStringToPascal(filename, fss.name); |
| 237 |
// I decided to use PNG as output image file type. |
| 238 |
// There are a number of other available formats. |
| 239 |
// Should I check the file suffix to choose the image file format? |
| 240 |
err = OpenADefaultComponent(GraphicsExporterComponentType, kQTFileTypePNG, &ge); |
| 241 |
if (err == noErr) |
| 242 |
{ |
| 243 |
err = GraphicsExportSetInputGWorld(ge, m_pmap); |
| 244 |
if (err == noErr) |
| 245 |
{ |
| 246 |
err = GraphicsExportSetOutputFile (ge, &fss); |
| 247 |
if (err == noErr) |
| 248 |
{ |
| 249 |
GraphicsExportDoExport(ge, nil); |
| 250 |
} |
| 251 |
} |
| 252 |
CloseComponent(ge); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
return err == noErr; |
| 257 |
} |
| 258 |
|
| 259 |
//------------------------------------------------------------------------ |
| 260 |
unsigned char* pixel_map::buf() |
| 261 |
{ |
| 262 |
return m_buf; |
| 263 |
} |
| 264 |
|
| 265 |
//------------------------------------------------------------------------ |
| 266 |
unsigned pixel_map::width() const |
| 267 |
{ |
| 268 |
if(m_pmap == nil) return 0; |
| 269 |
PixMapHandle pm = GetGWorldPixMap (m_pmap); |
| 270 |
Rect bounds; |
| 271 |
GetPixBounds (pm, &bounds); |
| 272 |
return bounds.right - bounds.left; |
| 273 |
} |
| 274 |
|
| 275 |
//------------------------------------------------------------------------ |
| 276 |
unsigned pixel_map::height() const |
| 277 |
{ |
| 278 |
if(m_pmap == nil) return 0; |
| 279 |
PixMapHandle pm = GetGWorldPixMap (m_pmap); |
| 280 |
Rect bounds; |
| 281 |
GetPixBounds (pm, &bounds); |
| 282 |
return bounds.bottom - bounds.top; |
| 283 |
} |
| 284 |
|
| 285 |
//------------------------------------------------------------------------ |
| 286 |
int pixel_map::row_bytes() const |
| 287 |
{ |
| 288 |
if(m_pmap == nil) return 0; |
| 289 |
PixMapHandle pm = GetGWorldPixMap (m_pmap); |
| 290 |
return calc_row_len(width(), GetPixDepth(pm)); |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|