Gets a rectangle from within another image, INCLUDING the alpha channel \bug in wxWidgets, wxImage::GetSubImage should do this itself.
| 352 | /// Gets a rectangle from within another image, INCLUDING the alpha channel |
| 353 | /// \bug in wxWidgets, wxImage::GetSubImage should do this itself. |
| 354 | wxImage GetSubImageWithAlpha( const wxImage & Src, const wxRect &rect ) |
| 355 | { |
| 356 | //First part of this code is lifted from wxImage::GetSubImage() source code. |
| 357 | wxImage image; |
| 358 | |
| 359 | wxCHECK_MSG( Src.Ok(), image, wxT("invalid image") ); |
| 360 | |
| 361 | wxCHECK_MSG( (rect.GetLeft()>=0) && (rect.GetTop()>=0) && ( |
| 362 | rect.GetRight()<=Src.GetWidth()) && (rect.GetBottom()<=Src.GetHeight()), |
| 363 | image, wxT("invalid subimage size") ); |
| 364 | |
| 365 | int subwidth=rect.GetWidth(); |
| 366 | const int subheight=rect.GetHeight(); |
| 367 | |
| 368 | image.Create( subwidth, subheight, false ); |
| 369 | |
| 370 | unsigned char *subdata = image.GetData(), *data=Src.GetData(); |
| 371 | |
| 372 | wxCHECK_MSG( subdata, image, wxT("unable to create image") ); |
| 373 | |
| 374 | // JKC: Quick hack - don't deal with masks - need to understand macro M_IMGDATA first. |
| 375 | // if (Src.M_IMGDATA->m_hasMask) |
| 376 | // image.SetMaskColour( Src.M_IMGDATA->m_maskRed, Src.M_IMGDATA->m_maskGreen, Src.M_IMGDATA->m_maskBlue ); |
| 377 | |
| 378 | int subleft=3*rect.GetLeft(); |
| 379 | int width=3*Src.GetWidth(); |
| 380 | subwidth*=3; |
| 381 | |
| 382 | data+=rect.GetTop()*width+subleft; |
| 383 | |
| 384 | for (long j = 0; j < subheight; ++j) |
| 385 | { |
| 386 | memcpy( subdata, data, subwidth); |
| 387 | subdata+=subwidth; |
| 388 | data+=width; |
| 389 | } |
| 390 | |
| 391 | image.InitAlpha(); |
| 392 | if( !Src.HasAlpha() ) |
| 393 | return image; |
| 394 | // OK, so we've copied the RGB data. |
| 395 | // Now do the Alpha channel. |
| 396 | //wxASSERT( Src.HasAlpha() ); |
| 397 | |
| 398 | subleft/=3; |
| 399 | width/=3; |
| 400 | subwidth/=3; |
| 401 | |
| 402 | data =Src.GetAlpha(); |
| 403 | subdata =image.GetAlpha(); |
| 404 | |
| 405 | data+=rect.GetTop()*width+subleft; |
| 406 | |
| 407 | for (long j = 0; j < subheight; ++j) |
| 408 | { |
| 409 | memcpy( subdata, data, subwidth); |
| 410 | subdata+=subwidth; |
| 411 | data+=width; |