📄 images.c
字号:
ReleaseDC(hMainWindow, hdc); if (p->mutable) { HPEN hpen; HBRUSH hbrush; hdc = getBitmapDC(&p->bitmap->bitmap); CHECK_RETURN(hpen = SelectObject(hdc, whitePen)); CHECK_RETURN(hbrush = SelectObject(hdc, whiteBrush)); Rectangle(hdc, 0, 0, width, height); SelectObject(hdc, hpen); SelectObject(hdc, hbrush); releaseBitmapDC(hdc); }}static voidcopyImagePixels(imageDstPtr self, void *sourceData){ _imageDstPtr p = (_imageDstPtr)self; myBitmapStruct *source = (myBitmapStruct *)sourceData; HDC hdc, hdcMem; hdc = GetDC(hMainWindow); hdcMem = CreateCompatibleDC(hdc); ReleaseDC(hMainWindow, hdc); SelectObject(hdcMem, p->bitmap->bitmap); DrawBitmap(hdcMem, source->bitmap, 0, 0, SRCCOPY); DeleteDC(hdcMem);}static voidcopyPixelsTransformed(imageDstPtr self, void *sourceData, int nXOriginSrc, int nYOriginSrc, int nWidth, int nHeight, int transform){ _imageDstPtr destP = (_imageDstPtr)self; myBitmapStruct *source = (myBitmapStruct *)sourceData; HDC hdc, hdcMem; HDC hdcMemDest; int imgLen; unsigned char *srcBits = NULL, *destBits = NULL, *destBitsPtr = NULL, *srcBitsPtr = NULL; int srcX; int srcY; int xStart; int yStart; int xIncr; int yIncr; int destX; int destY; int yCounter; int xCounter; int t_width; int t_height; /*scan length of the source image*/ int imageWidth; /*number of rows of the source image*/ int imageHeight; int srcIndex; int destIndex; int srcImgLen; int nXOriginDest = 0; int nYOriginDest = 0; /* --- Win32 specific setup */ hdc = GetDC(hMainWindow); hdcMemDest = CreateCompatibleDC(hdc); ReleaseDC(hMainWindow, hdc); SelectObject(hdcMemDest, destP->bitmap->bitmap); destP->bitmap->prop = HAS_ALPHA; /* --- */ imageWidth = source->width; imageHeight = source->height; /* width * height * 4 gives us the size of a 32 bpp image */ imgLen = nWidth * nHeight << 2; srcImgLen = imageWidth * imageHeight << 2; if(transform & TRANSFORM_INVERTED_AXES) { t_width = nHeight; t_height = nWidth; } else { t_width = nWidth; t_height = nHeight; } srcBits = source->image; destBits = destP->bitmap->image; if (transform & TRANSFORM_Y_FLIP) { yStart = nHeight-1; yIncr = -1; } else { yStart = 0; yIncr = +1; } if (transform & TRANSFORM_X_FLIP) { xStart = nWidth-1; xIncr = -1; } else { xStart = 0; xIncr = +1; } srcBitsPtr = srcBits; destBitsPtr = destBits; /* increment srcX,Y regular. increment destX,Y according to transform. this makes handling of mask and alpha values easier */ for (srcY = nYOriginSrc, destY = yStart, yCounter = 0; yCounter < nHeight; srcY++, destY+=yIncr, yCounter++) { /* in the current implementation we have source bitmap dimension as the width of the image and the height of the region destination bitmap is of the dimensions of the region */ for (srcX = nXOriginSrc, destX = xStart, xCounter = 0; xCounter < nWidth; srcX++, destX+=xIncr, xCounter++) { int alpha = 0; if ( transform & TRANSFORM_INVERTED_AXES ) { destIndex = ( ( (destX) * t_width) + (destY) ); } else { destIndex = ( ( (destY) * t_width) + (destX) ); } destBitsPtr = destBits + (destIndex * 4) ; srcIndex = (((srcY) * imageWidth) + (srcX)); srcBitsPtr = srcBits + (srcIndex * 4); /* copy the pixel that is pointed to */ *((int *)destBitsPtr) = *((int *)srcBitsPtr); } /*for x*/ } /* for y */ /* --- Delete unused GDI objects. */ DeleteDC(hdcMemDest);}static signed char *matrixRow(int row){ static signed char cmat[4][4] = { {-24, -6, 3, 21}, { 9, 15, -18, -12}, { -3, -21, 18, 0}, { 12, 6, -9, -15} }; static signed char matrix4[4][4] = { {-8, -2, 1, 7}, { 3, 5, -6, -4}, {-1, -7, 6, 0}, { 4, 2, -3, -5} }; static signed char matrix2[4][4] = { {-56, -14, 7, 49}, { 21, 35, -42, -28}, { -7, -49, 42, 0}, { 28, 14, -21, -35} }; static signed char matrix1[4][4] = { {-88, -22, 11, 77}, { 33, 55, -66, -44}, {-11, -77, 66, 0}, { 44, 22, -33, -55} }; if (numColors == 2) { return matrix1[row]; } else if (numColors == 4) { return matrix2[row]; } else if (numColors == 16) { return matrix4[row]; } return cmat[row];}static voidblendPixel(int *r, int *g, int *b, int *alpha, int off) {#if USE_ALPHA_BLEND if (*alpha < TRANS_THRESHOLD) { *alpha = 0; }#if TRANS_BINARY_THRESHOLD else { *alpha = 0xff; }#endif *r = (*r * *alpha) / 0xff; *g = (*g * *alpha) / 0xff; *b = (*b * *alpha) / 0xff;#else /* * if we're not using alphablending then dither the alpha * values so we can approximate a blending effect */ if ((*alpha != 0x00) && (*alpha != 0xff)) { *alpha += off * 3; if (*alpha < TRANS_THRESHOLD) { *alpha = 0; } else { *alpha = 0xff; } }#endif}static voidbuildMask(myBitmapStruct *bitmap, int alpha, int x, int y){ /* the mask must be 32-bit aligned for PlgBlt */ int width = bitmap->width + (32 - (bitmap->width & 31)); if (bitmap->mask == NULL) { BITMAPINFO bi; HDC hdc = GetDC(hMainWindow); bi.bmiHeader.biSize = sizeof(bi.bmiHeader); bi.bmiHeader.biWidth = width; bi.bmiHeader.biHeight = -bitmap->height; bi.bmiHeader.biPlanes = 1; bi.bmiHeader.biCompression = BI_RGB; bi.bmiHeader.biSizeImage = 0; bi.bmiHeader.biBitCount = 1; /* only need one bit for the mask */ bi.bmiHeader.biXPelsPerMeter = 0; bi.bmiHeader.biYPelsPerMeter = 0; bi.bmiHeader.biClrUsed = 0; bi.bmiHeader.biClrImportant = 0; bitmap->mask = CreateDIBSection(hdc, &bi, DIB_RGB_COLORS, &bitmap->imageMask, NULL, 0); if (bitmap->imageMask != NULL) { memset(bitmap->imageMask, 0x00, width * bitmap->height); } ReleaseDC(hMainWindow, hdc); } if (bitmap->mask != NULL) { /* * build the image mask */ int offset = (y * width + x) >> 3; *(bitmap->imageMask + offset) |= ((alpha & 1) << (7 - (x & 7))); }}static void setARGBPixels(imageDstPtr self, int** imageBuf, int bufLen, int width, int height, jboolean useAlpha){ int x; int y; int offset; int pixel; int color; int alpha; HDC hdc; HDC hdcMem; unsigned char* pel; _imageDstPtr p = (_imageDstPtr)self; if ((p->bitmap == NULL) || (p->mutable)){ fprintf(stderr, "setARGBPixel error\n"); return; } hdc = GetDC(hMainWindow); hdcMem = CreateCompatibleDC(hdc); ReleaseDC(hMainWindow, hdc); SelectObject(hdcMem, p->bitmap->bitmap); if (useAlpha) { p->bitmap->prop = HAS_ALPHA; } for (y = 0; y < height; y++){ for (x = 0; x < width; x++){ offset = (y * width) + x; pixel = (*imageBuf)[offset]; alpha = (pixel >> 24) & 0xff; color = makeImagePixel(pixel & 0xffffff); /* pixel 0xAARRGGBB encoded */ pel = p->bitmap->image + ((y * p->bitmap->width + x) << 2); /* * DIBs use bgr triples not rgb */ *pel++ = GetBValue(color); *pel++ = GetGValue(color); *pel++ = GetRValue(color); if (useAlpha) { buildMask(p->bitmap, alpha, x, y); *pel++ = alpha; } else { *pel++ = 255; /* fully opaque */ } } } DeleteDC(hdcMem); }static voidsendImagePixels(imageDstPtr self, int y, uchar *pixels, int pixelType){ signed char *mat = matrixRow(y & 3); int x; _imageDstPtr p = (_imageDstPtr)self; HDC hdc, hdcMem; int color, off, r, g, b, alpha; if (p->mutable) return; hdc = GetDC(hMainWindow); hdcMem = CreateCompatibleDC(hdc); ReleaseDC(hMainWindow, hdc); SelectObject(hdcMem, p->bitmap->bitmap); if ((pixelType == CT_COLOR) || (pixelType == (CT_COLOR | CT_ALPHA))) { /* rgb(a) */ for (x = 0; x < p->bitmap->width; ++x) { unsigned char *pel; off = mat[x & 3]; r = pixels[0]; g = pixels[1]; b = pixels[2]; alpha = 0xff; /* assume full opacity */ if (pixelType & CT_ALPHA) { alpha = pixels[3]; p->bitmap->prop = HAS_ALPHA; pixels++; } else if (p->hasTransMap) { /* single transparent color */ alpha = pixels[3]; p->bitmap->prop = HAS_ALPHA; pixels++; } pixels += 3; r += off; g += off; b += off; if (r < 0) r = 0; else if (r > 0xff) r = 0xff; if (g < 0) g = 0; else if (g > 0xff) g = 0xff; if (b < 0) b = 0; else if (b > 0xff) b = 0xff; if (p->bitmap->prop == HAS_ALPHA) { blendPixel(&r, &g, &b, &alpha, off); } #if USE_ALPHA_BLEND if (p->bitmap->prop == HAS_MASK) { buildMask(p->bitmap, alpha, x, y); } #else if (p->bitmap->prop == HAS_MASK || p->bitmap->prop == HAS_ALPHA) { buildMask(p->bitmap, alpha, x, y); }#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -