int main() { gdImagePtr im; int bordercolor, color; im = gdImageCreateTrueColor(100, 100); gdImageAlphaBlending(im, 1); gdImageSaveAlpha(im, 1); bordercolor = gdImageColorAllocateAlpha(im, 0, 0, 0, 2); color = gdImageColorAllocateAlpha(im, 0, 0, 0, 1); gdImageFillToBorder(im, 5, 5, bordercolor, color); color = gdImageGetPixel(im, 5, 5); gdImageDestroy(im); if (gdTestAssert(color==0x1000000)) { return 0; } else { printf("c: %X, expected %X\n", color, 0x1000000); return -1; } }
gdImagePtr rs_gdImageThickLineBrush(int line_weight, RS_Color& color) { if (line_weight % 2 == 1) line_weight += 1; int sx = line_weight; int sy = line_weight; gdImagePtr brush = gdImageCreateTrueColor(sx, sy); int transparent = gdImageColorAllocateAlpha(brush, 0, 0, 0, 127); gdImageAlphaBlending(brush, 0); gdImageFilledRectangle(brush, 0, 0, sx, sy, transparent); //compute fractional alpha value for antialiasing effect //each pixel should be hit by line_weight / 2 number of circles //so the alpha is computed as 255 / line_weight * 2 RS_Color falpha = color; falpha.alpha() = falpha.alpha() / line_weight * 2 + 1; falpha.alpha() = (falpha.alpha() < 255)? falpha.alpha() : 255; //outer transparent circle -- for antialiased effect rs_gdImageCircleForBrush(brush, sx/2, sy/2, line_weight / 2, falpha); gdImageAlphaBlending(brush, 1); //inner non-transparent circle rs_gdImageCircleForBrush(brush, sx/2, sy/2, (line_weight - 2) / 2, color); return brush; }
int main() { gdImagePtr im; int transparent, color; im = gdImageCreateTrueColor(100, 100); transparent = gdImageColorAllocateAlpha(im, 255, 255, 255, 80); gdImageFilledRectangle(im, 0,0, 99,99, transparent); color = gdImageColorAllocateAlpha(im, 0, 255, 0, 100); gdImageFilledArc(im, 49,49, 99,99, 0,360, color, gdPie); gdAssertImageEqualsToFile("gdimagefilledarc/php_bug43828_exp.png", im); gdImageDestroy(im); return gdNumFailures(); }
int CFunctions::imgColorAllocateAlpha ( lua_State* luaVM ) { // bool/int imageColorAllocateAlpha( userdata im, int red, int green, int blue, int alpha) if ( luaVM ) { if ( lua_type ( luaVM, 1 ) == LUA_TLIGHTUSERDATA && lua_type ( luaVM, 2 ) == LUA_TNUMBER && lua_type ( luaVM, 3 ) == LUA_TNUMBER && lua_type ( luaVM, 4 ) == LUA_TNUMBER && lua_type ( luaVM, 5 ) == LUA_TNUMBER ) { gdImagePtr im = mImgManager->GetImage( lua_touserdata( luaVM, 1 ) ); if( im != NULL ) { lua_pushnumber( luaVM, gdImageColorAllocateAlpha( im, (int)lua_tonumber(luaVM, 2), (int)lua_tonumber(luaVM, 3), (int)lua_tonumber(luaVM, 4), (int)lua_tonumber(luaVM, 5) ) ); } else { lua_pushboolean( luaVM, false ); } return 1; } else { pModuleManager->DebugPrintf( luaVM, "Incorrect parameters in: imageColorAllocate" ); lua_pushboolean( luaVM, false ); } return 1; } return 0; }
int main() { gdImagePtr im; int error = 0; int c, c1, c2, c3, c4, color, i; im = gdImageCreateTrueColor(5, 5); c = gdImageColorExact(im, 255, 0, 255); c2 = gdImageColorExactAlpha(im, 255, 0, 255, 100); gdImageDestroy(im); if (gdTestAssert(c == 0xFF00FF) != 1) { error = -1; } if (gdTestAssert(c2 == 0x64FF00FF) != 1) { error = -1; } im = gdImageCreate(5, 5); c1 = gdImageColorAllocate(im, 255, 0, 255); c2 = gdImageColorAllocate(im, 255, 200, 0); c3 = gdImageColorAllocateAlpha(im, 255, 0, 255, 100); c1 = gdImageColorExact(im, 255, 0, 255); c2 = gdImageColorExact(im, 255, 200, 0); c3 = gdImageColorExactAlpha(im, 255, 0, 255, 100); c4 = gdImageColorExactAlpha(im, 255, 34, 255, 100); if (gdTestAssert(c1 == 0) != 1) { error = -1; } if (gdTestAssert(c2 == 1) != 1) { error = -1; } if (gdTestAssert(c3 == 2) != 1) { error = -1; } if (gdTestAssert(c4 == -1) != 1) { error = -1; } color = gdTrueColorAlpha(gdImageRed(im, c1), gdImageGreen(im, c1), gdImageBlue(im, c1), 0); if (gdTestAssert(color == 0xFF00FF) != 1) { error = -1; } color = gdTrueColorAlpha(gdImageRed(im, c2), gdImageGreen(im, c2), gdImageBlue(im, c2), 0); if (gdTestAssert(color == 0xFFC800) != 1) { error = -1; } color = gdTrueColorAlpha(gdImageRed(im, c3), gdImageGreen(im, c3), gdImageBlue(im, c3), 0); if (gdTestAssert(color == 0xFF00FF) != 1) { error = -1; } gdImageDestroy(im); return error; }
int GdImageRenderer::createColor(const RGBA& color) { if (color.hasAlpha()) { return gdImageColorAllocateAlpha(image_, color.red, color.green, color.blue, 127 - (color.alpha / 2)); } else { return gdImageColorAllocate(image_, color.red, color.green, color.blue); } }
gdImagePtr rotatePixmapGD(gdImagePtr img, double angle_rad) { gdImagePtr rimg = NULL; double cos_a, sin_a; double x1 = 0.0, y1 = 0.0; /* destination rectangle */ double x2 = 0.0, y2 = 0.0; double x3 = 0.0, y3 = 0.0; double x4 = 0.0, y4 = 0.0; long minx, miny, maxx, maxy; int width=0, height=0; /* int color; */ sin_a = sin(angle_rad); cos_a = cos(angle_rad); /* compute distination rectangle (x1,y1 is known) */ x1 = 0 ; y1 = 0 ; x2 = img->sy * sin_a; y2 = -img->sy * cos_a; x3 = (img->sx * cos_a) + (img->sy * sin_a); y3 = (img->sx * sin_a) - (img->sy * cos_a); x4 = (img->sx * cos_a); y4 = (img->sx * sin_a); minx = (long) MS_MIN(x1,MS_MIN(x2,MS_MIN(x3,x4))); miny = (long) MS_MIN(y1,MS_MIN(y2,MS_MIN(y3,y4))); maxx = (long) MS_MAX(x1,MS_MAX(x2,MS_MAX(x3,x4))); maxy = (long) MS_MAX(y1,MS_MAX(y2,MS_MAX(y3,y4))); width = (int)ceil(maxx-minx); height = (int)ceil(maxy-miny); /* create the new image based on the computed width/height */ if (gdImageTrueColor(img)) { rimg = gdImageCreateTrueColor(width, height); gdImageAlphaBlending(rimg, 0); gdImageFilledRectangle(rimg, 0, 0, width, height, gdImageColorAllocateAlpha(rimg, 0, 0, 0, gdAlphaTransparent)); } else { int tc = gdImageGetTransparent(img); rimg = gdImageCreate(width, height); if(tc != -1) gdImageColorTransparent(rimg, gdImageColorAllocate(rimg, gdImageRed(img, tc), gdImageGreen(img, tc), gdImageBlue(img, tc))); } if(!rimg) { msSetError(MS_GDERR,"failed to create rotated pixmap","rotatePixmapGD()"); return NULL; } gdImageCopyRotated (rimg, img, width*0.5, height*0.5, 0, 0, gdImageSX(img), gdImageSY(img), angle_rad*MS_RAD_TO_DEG); return rimg; }
result_t Image::colorAllocateAlpha(int32_t red, int32_t green, int32_t blue, double alpha, int32_t &retVal) { if (!m_image) return CHECK_ERROR(CALL_E_INVALID_CALL); if (red < 0 || red > 255 || green < 0 || green > 255 || blue < 0 || blue > 255 || alpha < 0 || alpha > 1) return CHECK_ERROR(CALL_E_INVALIDARG); retVal = gdImageColorAllocateAlpha(m_image, red, green, blue, doubleToInt(alpha * 127)); return 0; }
gdImagePtr blendTransparency(gdImagePtr image, int height) { gdImageAlphaBlending(image, 0); int w = TARGET_WIDTH; int h = height; int h1 = h / 6; int h2 = 5 * h1; int l1 = w / 6; int l2 = 5 * 11; int row, col; for (row = 0; row < h; row++) { for (col = 0; col < w; col++) { int aval = 40; if ((row < h1) || (row > h2)) aval = 80; else if ((col < l1) || (col > l2)) aval = 80; int c = gdImageGetPixel(image, col, row); int r = gdImageRed(image, c); int g = gdImageGreen(image, c); int b = gdImageBlue(image, c); int c1 = gdImageColorAllocateAlpha(image, r, g, b, aval); gdImageSetPixel(image, col, row, c1); } } gdImageSaveAlpha(image, 1); return image; }
int main() { gdImagePtr dst_tc, src; int c1; src = gdImageCreate(5,5); gdImageAlphaBlending(src, 0); gdImageColorAllocate(src, 255,255,255); /* allocate white for background color */ c1 = gdImageColorAllocateAlpha(src, 255,0,0,70); gdImageFilledRectangle(src, 0,0, 4,4, c1); dst_tc = gdImageCreateTrueColor(5,5); gdImageAlphaBlending(dst_tc, 0); gdImageCopy(dst_tc, src, 0,0, 0,0, gdImageSX(src), gdImageSY(src)); /* CuAssertImageEquals(tc, src, dst_tc); */ /* Destroy it */ gdImageDestroy(dst_tc); gdImageDestroy(src); return 0; }
void gdDrawImage(HDC hdc, RECT *rc) { HDC mem_dc; BITMAPINFO bmp_info; void* bits; HBITMAP bmp, temp; gdImagePtr im; int width, height, stride; int white, black, blue, red; char *s = "Hello world!"; gdFontPtr lfont, gfont; width = rc->right - rc->left; height = rc->bottom - rc->top; bmp_info = gdCreateBmp(width, height); // Create memory device context mem_dc = CreateCompatibleDC(hdc); if (!mem_dc) { MessageBox(NULL, "Can't create a compatible DC!", "Error!", MB_ICONEXCLAMATION | MB_OK); return; } // bits points to a shared buffer of pixels bits = NULL; bmp = CreateDIBSection(mem_dc, &bmp_info, DIB_RGB_COLORS, (void**)&bits, 0, 0); // Selecting the object before doing anything allows you to use libgd // together with native Windows GDI. temp = (HBITMAP)SelectObject(mem_dc, bmp); /*stride = ((width * 1 + 3) >> 2) << 2;*/ // always uses 32bit in BMPINFO stride = width; im = NULL; // Attach shared buffer of pixels to GD image // Negative stride places 0,0 in upper-left corner im = gdImageTrueColorAttachBuffer((int*)bits, width, height, -stride); if (!im) { MessageBox(NULL, "GD image creation failed!", "Error!", MB_ICONEXCLAMATION | MB_OK); return; } // Start of GD drawing white = gdImageColorAllocate(im, 255, 255, 255); black = gdImageColorAllocate(im, 0, 0, 0); blue = gdImageColorAllocate(im, 0, 0, 255); // Allocate the color red, 50% transparent. red = gdImageColorAllocateAlpha(im, 255, 0, 0, 64); // Erase background with white color gdImageFilledRectangle(im, 0, 0, width, height, 0xFF0000); lfont = gdFontGetLarge(); gfont = gdFontGetGiant(); // Draw a dashed line from the upper left corner to the lower right corner. gdImageFilledRectangle(im, 25, 25, 100, 100, blue); gdImageChar(im, gfont, 35, 35, 'Q', white); gdImageFilledRectangle(im, 50, 50, 75, 175, red); gdImageLine(im, 0, 0, 150, 150, black); gdImageString(im, gdFontGetLarge(), im->sx / 2 - (strlen(s) * lfont->w / 2), im->sy / 2 - lfont->h / 2, (unsigned char*)s, black); // Copy drawing from memory context (shared bitmap buffer) to screen DC. BitBlt(hdc, rc->left, rc->top, width, height, mem_dc, 0, 0, SRCCOPY); // Free gdImageDetachBuffer(im); SelectObject(mem_dc, temp); DeleteObject(bmp); DeleteObject(mem_dc); }
int ConvertColor(gdImagePtr i, RS_Color& c) { return gdImageColorAllocateAlpha(i, c.red(), c.green(), c.blue(), 127 - c.alpha()/2 ); }
ngx_int_t ngx_http_small_light_gd_process(ngx_http_request_t *r, ngx_http_small_light_ctx_t *ctx) { ngx_http_small_light_gd_ctx_t *ictx; ngx_http_small_light_image_size_t sz; gdImagePtr src, dst, canvas; ngx_int_t colors, transparent, palette, red, green, blue; ngx_int_t ax, ay, ox, oy, type; int iw, ih, radius, ccolor, bcolor; char *sharpen, *of; u_char *out; int size; double q; ictx = (ngx_http_small_light_gd_ctx_t *)ctx->ictx; src = ngx_http_small_light_gd_src(ictx); if (src == NULL) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "failed to get image source %s:%d", __FUNCTION__, __LINE__); return NGX_ERROR; } /* adjust image size */ iw = gdImageSX(src); ih = gdImageSY(src); ngx_http_small_light_calc_image_size(r, ctx, &sz, iw, ih); colors = gdImageColorsTotal(src); palette = 0; transparent = -1; red = 0; green = 0; blue = 0; if (colors) { transparent = gdImageGetTransparent(src); if (transparent != -1) { palette = colors; red = gdImageRed(src, transparent); green = gdImageGreen(src, transparent); blue = gdImageBlue(src, transparent); } } gdImageColorTransparent(src, -1); /* rotate. */ if (sz.angle) { dst = src; ax = (iw % 2 == 0) ? 1 : 0; ay = (ih % 2 == 0) ? 1 : 0; switch (sz.angle) { case 90: case 270: dst = ngx_http_small_light_gd_new(ih, iw, palette); if (dst == NULL) { gdImageDestroy(src); return NGX_ERROR; } if (sz.angle == 90) { ox = ih / 2 - ay; oy = iw / 2 + ax; } else { ox = ih / 2 + ay; oy = iw / 2 - ax; } gdImageCopyRotated(dst, src, ox, oy, 0, 0, iw, ih, -sz.angle); gdImageDestroy(src); break; case 180: dst = ngx_http_small_light_gd_new(iw, ih, palette); if (dst == NULL) { gdImageDestroy(src); return NGX_ERROR; } gdImageCopyRotated(dst, src, iw / 2 - ax, ih / 2 - ay, 0, 0, iw + ax, ih + ay, sz.angle); gdImageDestroy(src); break; default: ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "image not rotated. 'angle'(%ui) must be 90 or 180 or 270. %s:%d", sz.angle, __FUNCTION__, __LINE__); break; } src = dst; } /* calc size. */ iw = gdImageSX(src); ih = gdImageSY(src); ngx_http_small_light_calc_image_size(r, ctx, &sz, iw, ih); /* pass through. */ if (sz.pt_flg != 0) { gdImageDestroy(src); ctx->of = ctx->inf; return NGX_OK; } /* crop, scale. */ if (sz.scale_flg != 0) { dst = ngx_http_small_light_gd_new(sz.dw, sz.dh, palette); if (dst == NULL) { gdImageDestroy(src); return NGX_ERROR; } if (colors == 0) { gdImageSaveAlpha(dst, 1); gdImageAlphaBlending(dst, 0); } gdImageCopyResampled(dst, src, 0, 0, sz.sx, sz.sy, sz.dw, sz.dh, sz.sw, sz.sh); if (colors) { gdImageTrueColorToPalette(dst, 1, 256); } gdImageDestroy(src); } else { dst = src; } if (transparent != -1 && colors) { gdImageColorTransparent(dst, gdImageColorExact(dst, red, green, blue)); } /* effects. */ sharpen = NGX_HTTP_SMALL_LIGHT_PARAM_GET_LIT(&ctx->hash, "sharpen"); if (sharpen != NULL) { radius = ngx_http_small_light_parse_int(sharpen); if (radius > 0) { gdImageSharpen(dst, radius); } } /* interlace */ gdImageInterlace(dst, 1); /* create canvas then draw image to the canvas. */ if (sz.cw > 0.0 && sz.ch > 0.0) { canvas = gdImageCreateTrueColor(sz.cw, sz.ch); if (canvas == NULL) { gdImageDestroy(dst); return NGX_ERROR; } ccolor = gdImageColorAllocateAlpha(canvas, sz.cc.r, sz.cc.g, sz.cc.b, sz.cc.a); gdImageFilledRectangle(canvas, 0, 0, sz.cw, sz.ch, ccolor); gdImageCopy(canvas, dst, sz.dx, sz.dy, 0, 0, sz.dw, sz.dh); gdImageDestroy(dst); dst = canvas; } /* border. */ if (sz.bw > 0.0 || sz.bh > 0.0) { bcolor = gdImageColorAllocateAlpha(dst, sz.bc.r, sz.bc.g, sz.bc.b, sz.bc.a); if (sz.cw > 0.0 && sz.ch > 0.0) { gdImageFilledRectangle(dst, 0, 0, sz.cw, sz.bh, bcolor); gdImageFilledRectangle(dst, 0, 0, sz.bw, sz.ch, bcolor); gdImageFilledRectangle(dst, 0, sz.ch - sz.bh, sz.cw - 1, sz.ch - 1, bcolor); gdImageFilledRectangle(dst, sz.cw - sz.bw, 0, sz.cw - 1, sz.ch - 1, bcolor); } else { gdImageFilledRectangle(dst, 0, 0, sz.dw, sz.bh, bcolor); gdImageFilledRectangle(dst, 0, 0, sz.bw, sz.dh, bcolor); gdImageFilledRectangle(dst, 0, sz.dh - sz.bh, sz.dw - 1, sz.dh - 1, bcolor); gdImageFilledRectangle(dst, sz.dw - sz.bw, 0, sz.dw - 1, sz.dh - 1, bcolor); } } of = NGX_HTTP_SMALL_LIGHT_PARAM_GET_LIT(&ctx->hash, "of"); if (ngx_strlen(of) > 0) { type = ngx_http_small_light_type(of); if (type == NGX_HTTP_SMALL_LIGHT_IMAGE_NONE) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "of is invalid(%s) %s:%d", of, __FUNCTION__, __LINE__); } else if (type == NGX_HTTP_SMALL_LIGHT_IMAGE_WEBP) { #ifdef NGX_HTTP_SMALL_LIGHT_GD_WEBP_ENABLED ictx->type = type; #else ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "WebP is not supported %s:%d", __FUNCTION__, __LINE__); #endif } else { ictx->type = type; } } ctx->of = ngx_http_small_light_image_types[ictx->type - 1]; q = ngx_http_small_light_parse_double(NGX_HTTP_SMALL_LIGHT_PARAM_GET_LIT(&ctx->hash, "q")); if (q == 0) { q = 100; } out = ngx_http_small_light_gd_out(dst, ictx->type, (int *)&size, q); gdImageDestroy(dst); if (out == NULL) { return NGX_ERROR; } /* get small_lighted image as binary. */ ctx->content = out; ctx->content_length = size; ngx_pfree(r->pool, ctx->content_orig); ictx->complete = 1; return NGX_OK; }
static int createImage (MULTICHART_ID id) { int i; if (id->height < (10 * gdFontMediumBold->h)) { return -1; } if (id->im) { gdImageDestroy (id->im); } id->im = gdImageCreate (id->width, id->height); if (id->im == NULL) { return -1; } // ... allocate our colors id->bgcolor = gdImageColorAllocateAlpha (id->im, GLC_RED(id->bgcolor), GLC_GREEN(id->bgcolor), GLC_BLUE(id->bgcolor), GLC_ALPHA(id->bgcolor)); if (id->isTransparent) { gdImageColorTransparent (id->im, id->bgcolor); id->chartcolor = id->bgcolor; id->titleBGcolor = id->bgcolor; } else { id->chartcolor = gdImageColorAllocateAlpha (id->im, GLC_RED(id->chartcolor), GLC_GREEN(id->chartcolor), GLC_BLUE(id->chartcolor), GLC_ALPHA(id->chartcolor)); id->titleBGcolor = gdImageColorAllocateAlpha (id->im, GLC_RED(id->titleBGcolor), GLC_GREEN(id->titleBGcolor), GLC_BLUE(id->titleBGcolor), GLC_ALPHA(id->titleBGcolor)); } id->gridcolor = gdImageColorAllocateAlpha (id->im, GLC_RED(id->gridcolor), GLC_GREEN(id->gridcolor), GLC_BLUE(id->gridcolor), GLC_ALPHA(id->gridcolor)); id->titleFGcolor = gdImageColorAllocateAlpha (id->im, GLC_RED(id->titleFGcolor), GLC_GREEN(id->titleFGcolor), GLC_BLUE(id->titleFGcolor), GLC_ALPHA(id->titleFGcolor)); id->textcolor = gdImageColorAllocateAlpha (id->im, GLC_RED(id->textcolor), GLC_GREEN(id->textcolor), GLC_BLUE(id->textcolor), GLC_ALPHA(id->textcolor)); for (i = 0; i < id->numdatasets; i ++) { id->dataset[i].lineColor = gdImageColorAllocateAlpha (id->im, GLC_RED(id->dataset[i].lineColor), GLC_GREEN(id->dataset[i].lineColor), GLC_BLUE(id->dataset[i].lineColor), GLC_ALPHA(id->dataset[i].lineColor)); } return 0; }