📄 images.c
字号:
color = makeImagePixel((r << 16) | (g << 8) | b); 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 USE_ALPHA_BLEND *pel++ = alpha;#endif } } else { /* grayscale or indexed color */ for (x = 0; x < p->bitmap->width; ++x) { unsigned char *pel; int cmapIndex = *pixels++; off = mat[x & 3]; color = p->cmap[cmapIndex]; r = ((color >> 16) & 0xff) + off; g = ((color >> 8) & 0xff) + off; b = ((color >> 0) & 0xff) + off; alpha = 0xff; /* assume full opacity */ 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; /* grayscale with alpha */ if ((pixelType & (CT_ALPHA | CT_COLOR)) == CT_ALPHA) { alpha = *pixels++; p->bitmap->prop = HAS_ALPHA; } else if (p->hasTransMap) { if ((pixelType & CT_COLOR) == 0) { /* grayscale */ alpha = *pixels++; p->bitmap->prop = HAS_ALPHA; } else { /* indexed color */ alpha = p->tmap[cmapIndex]; p->bitmap->prop = HAS_ALPHA; } } 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 color = makeImagePixel((r << 16) | (g << 8) | b); 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 USE_ALPHA_BLEND *pel++ = alpha;#endif } } DeleteDC(hdcMem); }static voidsendPackedImagePixels(imageDstPtr self, int y, uchar *pixels) { /* we specified a depth of 8, so... */ sendImagePixels(self, y, pixels, KNI_FALSE);}static void*imageDone(imageDstPtr self, int *width, int *height){ _imageDstPtr p = (_imageDstPtr)self; if (p == NULL) return NULL; if ((width != NULL) && (height != NULL)) { *width = p->bitmap->width; *height = p->bitmap->height; } return (void*)p->bitmap;}voidLCDUIdestroyNativeImage(void* imagePtr) { myBitmapStruct *imgData = (myBitmapStruct*)imagePtr; if (imgData != NULL) { if (imgData->bitmap) { DeleteObject(imgData->bitmap); } if (imgData->mask) { DeleteObject(imgData->mask); } midpFree(imagePtr); }}imageDstPtr LCDUIcreateImageDst(jboolean mutable) { _imageDstPtr p = midpMalloc(sizeof(_imageDstData)); if (p == NULL) { return NULL; } p->super.depth = 8; p->super.setColormap = setImageColormap; p->super.setTransMap = setImageTransparencyMap; p->super.setSize = setImageSize; p->super.sendPixels = sendImagePixels; p->super.sendPackedPixels = sendPackedImagePixels; p->super.copyPixels = copyImagePixels; p->super.done = imageDone; p->super.setARGBPixels = setARGBPixels; p->super.copyPixelsTransformed = copyPixelsTransformed; /* Create a bitmap structure. We use our own private data structure. * But, on some platforms, this may be an opaque data structure * managed by the platform. */ if ((p->bitmap=(myBitmapStruct*)midpMalloc(sizeof(myBitmapStruct))) == NULL) { midpFree(p); return NULL; } p->mutable = mutable; p->hasColormap = KNI_FALSE; p->hasTransMap = KNI_FALSE; return (imageDstPtr)p;}#if USE_ALPHA_BLENDBOOL customAlphaBlend(HDC hdcDest, int nXOriginDest, int nYOriginDest, HDC hdcSrc, int nXOriginSrc, int nYOriginSrc, int nWidth, int nHeight, int transform, jboolean processAlpha) { int imgLen; int scanLength; unsigned char *srcBits = NULL, *destBits = NULL, *destBitsPtr = NULL, *srcBitsPtr = NULL, *srcBitsEndPtr = NULL, *lineEndPtr = NULL; int destX, destY; int xStart; int yStart; int xIncr; int yIncr; int alpha; int t_width; int t_height; BOOL ret = KNI_FALSE; HDC srcHDC, destHDC; HBITMAP srcHBmp, destHBmp; BITMAP srcBmp; BITMAPINFO bi; HGDIOBJ oobj; destHDC = CreateCompatibleDC(hdcSrc); srcHBmp = GetCurrentObject(hdcSrc, OBJ_BITMAP); DB(GetObject(srcHBmp, sizeof(srcBmp), &srcBmp)); #if 0 fprintf(stderr, "bmType=%i\n" "bmWidth=%i\n" "bmHeight=%i\n" "bmWidthBytes=%i\n" "bmPlanes=%i\n" "bmBitsPixel=%i\n\n", srcBmp.bmType, srcBmp.bmWidth, srcBmp.bmHeight, srcBmp.bmWidthBytes, srcBmp.bmPlanes, srcBmp.bmBitsPixel);#endif imgLen = nWidth * nHeight << 2; if(transform & TRANSFORM_INVERTED_AXES) { t_width = nHeight; t_height = nWidth; } else { t_width = nWidth; t_height = nHeight; } bi.bmiHeader.biSize = sizeof(bi.bmiHeader); bi.bmiHeader.biPlanes = srcBmp.bmPlanes; bi.bmiHeader.biBitCount = 32; /* srcBmp.bmPlanes * srcBmp.bmBitsPixel;*/ bi.bmiHeader.biCompression = BI_RGB; bi.bmiHeader.biXPelsPerMeter = 0; bi.bmiHeader.biYPelsPerMeter = 0; bi.bmiHeader.biClrUsed = 0; bi.bmiHeader.biClrImportant = 0; bi.bmiHeader.biWidth = t_width; bi.bmiHeader.biHeight = -t_height; bi.bmiHeader.biSizeImage = t_width * t_height << 2; destHBmp = CreateDIBSection(destHDC, &bi, DIB_RGB_COLORS, &destBits, NULL, 0); srcBits = (unsigned char *)midpMalloc((srcBmp.bmWidth << 2) * nHeight); if ((srcBits != NULL) && (destBits != NULL)) { oobj = SelectObject(destHDC, destHBmp); /* * grab the screen contents into a bitmap * the same size as our source image * + ~510ms */ DB(BitBlt(destHDC, 0, 0, t_width, t_height, hdcDest, nXOriginDest, nYOriginDest, SRCCOPY)); bi.bmiHeader.biWidth = srcBmp.bmWidth; bi.bmiHeader.biHeight = -srcBmp.bmHeight; bi.bmiHeader.biSizeImage = srcBmp.bmWidth * srcBmp.bmWidth << 2; /* grab source bits */ DB(GetDIBits(hdcSrc, srcHBmp, srcBmp.bmHeight - nHeight - nYOriginSrc, nHeight, srcBits, &bi, DIB_RGB_COLORS));#if 1 scanLength = srcBmp.bmWidth << 2; srcBitsPtr = srcBits + (nXOriginSrc << 2); destBitsPtr = destBits; srcBitsEndPtr = srcBitsPtr + (nHeight - 1) * scanLength + (nWidth << 2); lineEndPtr = srcBitsPtr + (nWidth << 2); if (transform != 0) { if (transform & TRANSFORM_Y_FLIP) { destY = nHeight - 1; yIncr = -1; } else { destY = 0; yIncr = +1; } if (transform & TRANSFORM_X_FLIP) { xStart = nWidth - 1; xIncr = -1; } else { xStart = 0; xIncr = +1; } } while (srcBitsPtr < srcBitsEndPtr) { /** * these may not have valid values is transform == 0, * but in that case they are never used. */ destX = xStart; while (srcBitsPtr < lineEndPtr) { if (transform != 0) { destBitsPtr = destBits; if (transform & TRANSFORM_INVERTED_AXES) { destBitsPtr += ((destX * t_width) + destY) << 2; } else { destBitsPtr += ((destY * t_width) + destX) << 2; } destX += xIncr; } alpha = srcBitsPtr[3]; if (alpha == 0xff || (processAlpha == KNI_FALSE)) { /* completely opaque */ /* * this operation can be optimized * for the bus size of a device */ *((int *)destBitsPtr) = *((int *)srcBitsPtr); /* *(destBitsPtr + x + 0) = *(srcBitsPtr + x + 0); *(destBitsPtr + x + 1) = *(srcBitsPtr + x + 1); *(destBitsPtr + x + 2) = *(srcBitsPtr + x + 2); *(destBitsPtr + x + 3) = alpha; */ } else if (alpha != 0x00) { /* needs blending */ /* save on dereferencing these values everywhere */ unsigned char *d0 = destBitsPtr + 0, *d1 = destBitsPtr + 1, *d2 = destBitsPtr + 2; int c; /* this could be optimized if the alpha values could be * rounded to powers of two, then we could simply shift * by alpha. we could also be careless with the divide * and right shift by 8 */ /* doing the calculations this way is faster than the * other method which is compiler defined out below * but the results are still not very good */ c = srcBitsPtr[0] + ((*d0 * (0xff - alpha)) / 0xff); if (c > 0xff) c = 0xff; *d0 = (unsigned char)c; c = srcBitsPtr[1] + ((*d1 * (0xff - alpha)) / 0xff); if (c > 0xff) c = 0xff; *d1 = (unsigned char)c; c = srcBitsPtr[2] + ((*d2 * (0xff - alpha)) / 0xff); if (c > 0xff) c = 0xff; *d2 = (unsigned char)c; /*fprintf(stderr, "RGB(%i, %i, %i)\n", *d0, *d1, *d2);*/ #if 0 *d0 = *(srcBitsPtr + 0) + *d0 - ((*d0 * alpha) / 0xff); *d1 = *(srcBitsPtr + 1) + *d1 - ((*d1 * alpha) / 0xff); *d2 = *(srcBitsPtr + 2) + *d2 - ((*d2 * alpha) / 0xff);#endif /* this operation is unnecessary */ /* destBitsPtr[4] = alpha; */ } /* changing destBitsPtr if transform != 0 is ok since we reset * it's value at the top of the loop */ destBitsPtr += 4; srcBitsPtr += 4; } lineEndPtr += scanLength; srcBitsPtr = lineEndPtr - (nWidth << 2); destY += yIncr; } #endif bi.bmiHeader.biWidth = t_width; bi.bmiHeader.biHeight = -t_height; bi.bmiHeader.biSizeImage = t_width * t_height << 2; ret = BitBlt(hdcDest, nXOriginDest, nYOriginDest, t_width, t_height, destHDC, 0, 0, SRCCOPY); SelectObject(destHDC, oobj); /* Delete the DIB */ DeleteObject(destHBmp); } DeleteDC(destHDC); midpFree(srcBits); return ret;} #endif /* USE_ALPHA_BLEND */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -