static void compare_image( const surface& src , const surface& dst , const std::string message) { BOOST_REQUIRE_MESSAGE( src->w == dst->w , message << "source width »" << src->w << "« destination width »" << dst->w << "«.\n" ); BOOST_REQUIRE_MESSAGE( src->h == dst->h , message << "source height »" << src->w << "« destination heigth »" << dst->w << "«.\n" ); const_surface_lock src_lock(src); const_surface_lock dst_lock(dst); const Uint32* src_pixels = src_lock.pixels(); const Uint32* dst_pixels = dst_lock.pixels(); const unsigned pixels = src->w * src->h; unsigned matches = 0; for(unsigned i = 0; i < pixels; ++i, ++src_pixels, ++dst_pixels) { matches += (*src_pixels == *dst_pixels); } BOOST_CHECK_MESSAGE( pixels == matches , message << "of the " << pixels << " pixels in the image " << matches << " match.\n" ); }
//on pixels where mask is not white, overwrite dest with src. Mask and dest are //translated to the position (x,y) on dest. //All surfaces are supposed to be neutral surfaces, mask and src are supposed //to be of identical size. void masked_overwrite_surface(surface dest, surface src, surface mask, int x, int y) { surface_lock dest_lock(dest); surface_lock src_lock(src); surface_lock mask_lock(mask); Uint32* dest_beg = dest_lock.pixels(); Uint32* src_beg = src_lock.pixels(); Uint32* mask_beg = mask_lock.pixels(); size_t small_shift_before; size_t small_shift_after; size_t dest_shift; size_t src_width = src->w; size_t src_height = src->h; if(x < 0) { small_shift_before = -x; if (src_width < small_shift_before) return; src_width -= small_shift_before; x = 0; } else { small_shift_before = 0; } if(x + src_width <= unsigned(dest->w)) { small_shift_after = 0; } else { small_shift_after = src_width - (dest->w - x); src_width = dest->w - x; } if(y >= 0) { dest_beg += dest->w * y + x; } else { src_beg += (-y) * src->w; mask_beg += (-y) * mask->w; dest_beg += x; if (src_height < static_cast<size_t>(-y)) return; src_height += y; y = 0; } if(y + src_height > unsigned(dest->h)) { src_height = dest->h - y; } dest_shift = dest->w - src_width; for(size_t j = 0; j < src_height; ++j) { src_beg += small_shift_before; mask_beg += small_shift_before; for(size_t i = 0; i < src_width; ++i) { //Assuming here alpha mask is 0xff000000 if((*mask_beg & 0x00ffffff) != 0x00ffffff) { *dest_beg = *src_beg; } ++dest_beg; ++src_beg; ++mask_beg; } src_beg += small_shift_after; mask_beg += small_shift_after; dest_beg += dest_shift; } }