Pastes one image into another including the alpha channel. Differs from OverlayImage in that: Happens in place to existing background image. Pastes image on top; no blending with existing background is done.
| 295 | /// Happens in place to existing background image. |
| 296 | /// Pastes image on top; no blending with existing background is done. |
| 297 | void PasteSubImage( wxImage * background, wxImage * foreground, int xoff, int yoff ) |
| 298 | { |
| 299 | |
| 300 | unsigned char *bg = background->GetData(); |
| 301 | unsigned char *fg = foreground->GetData(); |
| 302 | unsigned char *bgAlpha = background->HasAlpha() ? background->GetAlpha() : NULL; |
| 303 | unsigned char *fgAlpha = foreground->HasAlpha() ? foreground->GetAlpha() : NULL; |
| 304 | // For testing... Set as if no alpha in foreground.... |
| 305 | // fgAlpha = NULL; |
| 306 | |
| 307 | int bgWidth = background->GetWidth(); |
| 308 | int bgHeight = background->GetHeight(); |
| 309 | int fgWidth = foreground->GetWidth(); |
| 310 | int fgHeight = foreground->GetHeight(); |
| 311 | |
| 312 | int wCutoff = fgWidth; |
| 313 | int hCutoff = fgHeight; |
| 314 | |
| 315 | // If the masked foreground + offset is bigger than the background, masking |
| 316 | // should only occur within these bounds of the foreground image |
| 317 | wCutoff = (bgWidth - xoff > wCutoff) ? wCutoff : bgWidth - xoff; |
| 318 | hCutoff = (bgHeight - yoff > hCutoff) ? hCutoff : bgHeight - yoff; |
| 319 | |
| 320 | // Go through the foreground image bit by bit and place it on to the |
| 321 | // background, at an offset of xoff,yoff. |
| 322 | // Don't go beyond the size of the background image, |
| 323 | // or the foreground image. |
| 324 | int y; |
| 325 | unsigned char *bkp; |
| 326 | unsigned char *fgp; |
| 327 | unsigned char *bgAlphap; |
| 328 | unsigned char *fgAlphap; |
| 329 | for (y = 0; y < hCutoff; y++) { |
| 330 | // RGB bytes |
| 331 | bkp = bg + 3 * ((y + yoff) * bgWidth + xoff); |
| 332 | fgp = fg + 3 * ( y * fgWidth); |
| 333 | memcpy( bkp, fgp, 3 * wCutoff ); |
| 334 | |
| 335 | // Alpha bytes. |
| 336 | if( bgAlpha ) |
| 337 | { |
| 338 | bgAlphap = bgAlpha + ((y+yoff) * bgWidth + xoff ); |
| 339 | if( fgAlpha ) |
| 340 | { |
| 341 | fgAlphap = fgAlpha + (y * fgWidth ); |
| 342 | memcpy( bgAlphap, fgAlphap, wCutoff ); |
| 343 | } |
| 344 | else |
| 345 | { |
| 346 | memset( bgAlphap, 255, wCutoff ); |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | /// Gets a rectangle from within another image, INCLUDING the alpha channel |
| 353 | /// \bug in wxWidgets, wxImage::GetSubImage should do this itself. |
no test coverage detected