📄 drawingtidbits.cpp
字号:
src[destIndex + 0] = mix.blue; } } delete[] p; } } } return status;}// convert_bitmapstatus_tconvert_bitmap( BBitmap* inBitmap, BBitmap* outBitmap ){ status_t status = B_BAD_VALUE; // see that we got valid bitmaps if ( inBitmap && inBitmap->IsValid() && outBitmap && outBitmap->IsValid() ) { status = B_MISMATCHED_VALUES; // see that bitmaps are compatible and that we support the conversion if ( inBitmap->Bounds().Width() <= outBitmap->Bounds().Width() && inBitmap->Bounds().Height() <= outBitmap->Bounds().Height() && ( outBitmap->ColorSpace() == B_RGB32 || outBitmap->ColorSpace() == B_RGBA32) ) { int32 width = inBitmap->Bounds().IntegerWidth() + 1; int32 height = inBitmap->Bounds().IntegerHeight() + 1; int32 srcBpr = inBitmap->BytesPerRow(); int32 dstBpr = outBitmap->BytesPerRow(); uint8* srcBits = (uint8*)inBitmap->Bits(); uint8* dstBits = (uint8*)outBitmap->Bits(); switch (inBitmap->ColorSpace()) { case B_YCbCr422: // Y0[7:0] Cb0[7:0] Y1[7:0] Cr0[7:0] // Y2[7:0] Cb2[7:0] Y3[7:0] Cr2[7:0] for ( int32 y = 0; y < height; y++ ) { for ( int32 x = 0; x < width; x += 2 ) { int32 srcOffset = x * 2; int32 dstOffset = x * 4; ycbcr_to_rgb( srcBits[srcOffset + 0], srcBits[srcOffset + 1], srcBits[srcOffset + 3], dstBits[dstOffset + 2], dstBits[dstOffset + 1], dstBits[dstOffset + 0] ); ycbcr_to_rgb( srcBits[srcOffset + 2], srcBits[srcOffset + 1], srcBits[srcOffset + 3], dstBits[dstOffset + 6], dstBits[dstOffset + 5], dstBits[dstOffset + 4] ); // take care of alpha dstBits[x * 4 + 3] = 255; dstBits[x * 4 + 7] = 255; } srcBits += srcBpr; dstBits += dstBpr; } status = B_OK; break; case B_YCbCr420: // Non-interlaced only! // Cb0 Y0 Y1 Cb2 Y2 Y3 on even scan lines ... // Cr0 Y0 Y1 Cr2 Y2 Y3 on odd scan lines status = B_ERROR; break; case B_YUV422: // U0[7:0] Y0[7:0] V0[7:0] Y1[7:0] // U2[7:0] Y2[7:0] V2[7:0] Y3[7:0] status = B_ERROR; break; case B_RGB32: case B_RGBA32: memcpy( dstBits, srcBits, inBitmap->BitsLength() ); status = B_OK; break; case B_RGB16: // G[2:0],B[4:0] R[4:0],G[5:3] for ( int32 y = 0; y < height; y ++ ) { for ( int32 x = 0; x < width; x++ ) { int32 srcOffset = x * 2; int32 dstOffset = x * 4; uint8 blue = srcBits[srcOffset + 0] & 0x1f; uint8 green = ( srcBits[srcOffset + 0] >> 5 ) | ( ( srcBits[srcOffset + 1] & 0x07 ) << 3 ); uint8 red = srcBits[srcOffset + 1] & 0xf8; // homogeneously scale each component to 8 bit dstBits[dstOffset + 0] = (blue << 3) | (blue >> 2); dstBits[dstOffset + 1] = (green << 2) | (green >> 4); dstBits[dstOffset + 2] = red | (red >> 5); } srcBits += srcBpr; dstBits += dstBpr; } status = B_OK; break; default: status = B_MISMATCHED_VALUES; break; } if ( status == B_OK ) { if ( width < outBitmap->Bounds().IntegerWidth() + 1 || height < outBitmap->Bounds().IntegerHeight() + 1 ) { scale_bitmap( outBitmap, width, height ); } } } } return status;}// clip_floatinline uint8clip_float(float value){ if (value < 0) value = 0; if (value > 255) value = 255; return (uint8)value;}// dim_bitmapstatus_tdim_bitmap(BBitmap* bitmap, rgb_color center, float dimLevel){ status_t status = B_BAD_VALUE; if (bitmap && bitmap->IsValid()) { switch (bitmap->ColorSpace()) { case B_CMAP8: { BScreen screen(B_MAIN_SCREEN_ID); if (screen.IsValid()) { // iterate over each pixel, get the respective // color from the screen object, find the distance // to the "center" color and shorten the distance // by "dimLevel" int32 length = bitmap->BitsLength(); uint8* bits = (uint8*)bitmap->Bits(); for (int32 i = 0; i < length; i++) { // preserve transparent pixels if (bits[i] != B_TRANSPARENT_MAGIC_CMAP8) { // get color for this index rgb_color c = screen.ColorForIndex(bits[i]); // red float dist = (c.red - center.red) * dimLevel; c.red = clip_float(center.red + dist); // green dist = (c.green - center.green) * dimLevel; c.green = clip_float(center.green + dist); // blue dist = (c.blue - center.blue) * dimLevel; c.blue = clip_float(center.blue + dist); // write correct index of the dimmed color // back into bitmap (and hope the match is close...) bits[i] = screen.IndexForColor(c); } } status = B_OK; } break; } case B_RGB32: case B_RGBA32: { // iterate over each color component, find the distance // to the "center" color and shorten the distance // by "dimLevel" uint8* bits = (uint8*)bitmap->Bits(); int32 bpr = bitmap->BytesPerRow(); int32 pixels = bitmap->Bounds().IntegerWidth() + 1; int32 lines = bitmap->Bounds().IntegerHeight() + 1; // iterate over color components for (int32 y = 0; y < lines; y++) { for (int32 x = 0; x < pixels; x++) { int32 offset = 4 * x; // four bytes per pixel // blue float dist = (bits[offset + 0] - center.blue) * dimLevel; bits[offset + 0] = clip_float(center.blue + dist); // green dist = (bits[offset + 1] - center.green) * dimLevel; bits[offset + 1] = clip_float(center.green + dist); // red dist = (bits[offset + 2] - center.red) * dimLevel; bits[offset + 2] = clip_float(center.red + dist); // ignore alpha channel } // next line bits += bpr; } status = B_OK; break; } default: status = B_ERROR; break; } } return status;}// dimmed_color_cmap8rgb_colordimmed_color_cmap8(rgb_color color, rgb_color center, float dimLevel){ BScreen screen(B_MAIN_SCREEN_ID); if (screen.IsValid()) { // red float dist = (color.red - center.red) * dimLevel; color.red = clip_float(center.red + dist); // green dist = (color.green - center.green) * dimLevel; color.green = clip_float(center.green + dist); // blue dist = (color.blue - center.blue) * dimLevel; color.blue = clip_float(center.blue + dist); // get color index for dimmed color int32 index = screen.IndexForColor(color); // put color at index (closest match in palette // to dimmed result) into returned color color = screen.ColorForIndex(index); } return color;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -