int main() { gdImagePtr im, tile; int tile_red, tile_blue; int error = 0; char path[1024]; im = gdImageCreate(200, 150); tile = gdImageCreateTrueColor(2, 2); tile_red = gdImageColorAllocate(tile, 255, 0, 0); tile_blue = gdImageColorAllocate(tile, 0, 0, 255); gdImageSetPixel(tile, 0, 0, tile_red); gdImageSetPixel(tile, 1, 1, tile_red); gdImageSetPixel(tile, 1, 0, tile_blue); gdImageSetPixel(tile, 0, 1, tile_blue); gdImageSetTile(im, tile); gdImageFill(im, 11, 12, gdTiled); sprintf(path, "%s/gdimagefill/bug00104_1_exp.png", GDTEST_TOP_DIR); if (!gdAssertImageEqualsToFile(path, im)) { error = 1; } gdImageDestroy(im); gdImageDestroy(tile); return error; }
static void *resizeImage( const char *buf, int bufLen, int width, int height, MyData *myData, int *size ) { char *ptr; gdImagePtr dest, src; if ( myData->IMAGE_TYPE == HTTP_IMG_JPEG ) src = gdImageCreateFromJpegPtr( bufLen, (void *)buf ); else if ( myData->IMAGE_TYPE == HTTP_IMG_GIF ) src = gdImageCreateFromGifPtr( bufLen, (void *)buf ); else if ( myData->IMAGE_TYPE == HTTP_IMG_PNG ) src = gdImageCreateFromPngPtr( bufLen, (void *)buf ); else return NULL; if ( !width && !height ) return NULL; else if ( !width ) width = height * src->sx / src->sy; else if ( !height ) height = width * src->sy / src->sx; dest = gdImageCreateTrueColor( width, height ); gdImageCopyResampled( dest, src, 0, 0, 0, 0, width, height, src->sx, src->sy ); if ( myData->IMAGE_TYPE == HTTP_IMG_JPEG ) ptr = gdImageJpegPtr( dest, size, 50 ); else if ( myData->IMAGE_TYPE == HTTP_IMG_GIF ) ptr = gdImageGifPtr( dest, size ); else if ( myData->IMAGE_TYPE == HTTP_IMG_PNG ) ptr = gdImagePngPtr( dest, size ); return ptr; }
int main() { int method, i; for(method = GD_BELL; method <= GD_TRIANGLE; method++) { /* GD_WEIGHTED4 is unsupported. */ gdImagePtr im[2]; // printf("Method = %d\n", method); im[0] = gdImageCreateTrueColor(X, Y); im[1] = gdImageCreatePalette(X, Y); for (i = 0; i < 2; i++) { gdImagePtr result; // printf(" %s\n", i == 0 ? "truecolor" : "palette"); gdImageFilledRectangle(im[i], 0, 0, X-1, Y-1, gdImageColorExactAlpha(im[i], 255, 255, 255, 0)); gdImageSetInterpolationMethod(im[i], method); gdTestAssert(im[i]->interpolation_id == method); /* No getter yet. */ result = gdImageScale(im[i], NX, NY); gdTestAssert(result != NULL); gdTestAssert(result != im[i]); gdTestAssert(result->sx == NX && result->sy == NY); gdImageDestroy(result); gdImageDestroy(im[i]); }/* for */ }/* for*/ return gdNumFailures(); }/* main*/
void ImageComparisonPerform(int method, const char *f1, const char *f2) { gdImagePtr img1 = NULL, img2 = NULL, imgresult = NULL; int x, y; int match, nunmatched; img1 = ThumbCreate(f1, NULL); img2 = ThumbCreate(f2, NULL); if (!img1 || !img2) { fprintf(stderr, "ERROR: failed to create thumbnail of image\n"); goto end; } switch (method) { case IMG_CMP_ABS: case IMG_CMP_RANGE: imgresult = gdImageCreateTrueColor(THUMB_CX, THUMB_CY); if (!imgresult) { fprintf(stderr, "ERROR: failed to create result image\n"); goto end; } if (method == IMG_CMP_ABS) { printf("Average color difference: 0x%08x\n", ImgGetAbsColorDiff(img1, img2, imgresult)); } else { nunmatched = 0; for (y = 0; y != THUMB_CY; y++) { for (x = 0; x != THUMB_CX; x++) { match = ImgPixelCompareFuzzy(img1->tpixels[y][x], img2->tpixels[y][x]); if (match) { imgresult->tpixels[y][x] = 0xFFFFFF; } else { imgresult->tpixels[y][x] = 0x000000; nunmatched++; } } } } if (!ImgSavePng(outpath[0] ? outpath : "output.png", imgresult)) { fprintf(stderr, "ERROR: failed to save output image to file\n"); goto end; } break; case IMG_CMP_HISTRGB: case IMG_CMP_HISTHSV: break; case IMG_CMP_PHASH: break; default: fprintf(stderr, "ERROR: unrecognized comparison method\n"); goto end; } end: if (img1) gdImageDestroy(img1); if (img2) gdImageDestroy(img2); if (imgresult) gdImageDestroy(imgresult); }
PngSurface::~PngSurface() { gdImagePtr im; FILE* out; im = gdImageCreateTrueColor(width(), height()); if (!(out = fopen(m_fname.c_str(), "wb"))) { std::cerr << "Unable to open/create file " << m_fname << std::endl; return; } for (int h = 0; h < m_height; ++h) { for (int w = 0; w < m_width; ++w) { gdImageSetPixel(im, w, h, data[h][w]); } } gdImagePng(im, out); fclose(out); gdImageDestroy(im); for (int i = 0; i < m_height; ++i) { delete[] data[i]; data[i] = NULL; } delete[] data; data = NULL; }
gdImagePtr imagefilter_difference(gdImagePtr img1, gdImagePtr img2) { int x, y, c1, c2, r, g, b, np; int sx = img1->sx < img2->sx ? img1->sx : img2->sx; int sy = img1->sy < img2->sy ? img1->sy : img2->sy; gdImagePtr img = gdImageCreateTrueColor(sx, sy); for (y = 0; y < sy; y++) for (x = 0; x < sx; x++) { c1 = gdImageGetPixel(img1, x, y); c2 = gdImageGetPixel(img2, x, y); r = gdImageRed(img1, c1) - gdImageRed(img2, c2); g = gdImageGreen(img1, c1) - gdImageGreen(img2, c2); b = gdImageBlue(img1, c1) - gdImageBlue(img2, c2); np = gdImageColorAllocate(img, r < 0 ? -r : r, g < 0 ? -g : g, b < 0 ? -b : b ); gdImageSetPixel(img, x, y, np); } return img; }
int main() { gdImagePtr im, im2; int error = 0; im = gdImageCreateTrueColor(100, 100); if (im == NULL) { gdTestErrorMsg("gdImageCreateTruecolor failed\n"); error = 1; goto exit; } gdImageColorTransparent(im, -1); gdImageTrueColorToPalette(im, 1, 3); gdImageColorTransparent(im, 9); im2 = gdImageScale(im, 1, 65535); if (im2 == NULL) { error = 1; goto freeim; } else { gdImageDestroy(im2); } freeim: gdImageDestroy(im); exit: return error; }
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; }
static void crop(Cmd *cmd) { char *buff = cmd->data; int len = cmd->size; Gd *gd = cmd->gd; int index = 0; long width, height; int srcW, srcH, srcX, srcY, destX, destY, playX, playY; gdImagePtr destination = NULL; ei_decode_version(buff, &index, NULL); ei_decode_tuple_header(buff, &index, NULL); ei_decode_long(buff, &index, &width); ei_decode_long(buff, &index, &height); if (NULL == gd->image) { driver_failure_atom(gd->port, "null_image"); return; } srcW = gdImageSX(gd->image); srcH = gdImageSY(gd->image); destination = gdImageCreateTrueColor(width, height); if (NULL == destination) { driver_failure_posix(gd->port, ENOMEM); return; } gdImageFilledRectangle(destination, 0, 0, width, height, gdImageColorAllocate(destination, 255, 255, 255)); destX = (width - srcW) / 2; destY = (height - srcH) / 2; gdImageCopy(destination, gd->image, destX, destY, 0, 0, srcW, srcH); gdImageDestroy(gd->image); gd->image = destination; send_atom(gd->port, "ok"); }
static gdImagePtr _gd2CreateFromFile (gdIOCtxPtr in, int *sx, int *sy, int *cs, int *vers, int *fmt, int *ncx, int *ncy, t_chunk_info ** cidx) { gdImagePtr im; if (_gd2GetHeader (in, sx, sy, cs, vers, fmt, ncx, ncy, cidx) != 1) { GD2_DBG(php_gd_error("Bad GD2 header")); goto fail1; } if (gd2_truecolor(*fmt)) { im = gdImageCreateTrueColor(*sx, *sy); } else { im = gdImageCreate(*sx, *sy); } if (im == NULL) { GD2_DBG(php_gd_error("Could not create gdImage")); goto fail2; } if (!_gdGetColors(in, im, (*vers) == 2)) { GD2_DBG(php_gd_error("Could not read color palette")); goto fail3; } GD2_DBG(php_gd_error("Image palette completed: %d colours", im->colorsTotal)); return im; fail3: gdImageDestroy(im); fail2: gdFree(*cidx); fail1: return 0; }
/* saves the data stored in the image_data array of given width and height into an output .png file returns: 0 on success 1 otherwise */ int save_image_data(char *file_name, color_t *imageData, int width, int height) { gdImagePtr img = gdImageCreateTrueColor(width, height); unsigned x, y; for(y = 0; y < height; ++y) { for(x = 0; x < width; ++x) { int index = y*width + x; int gd_color = gdImageColorAllocate(img, imageData[index].r, imageData[index].g, imageData[index].b); gdImageSetPixel(img, x, y, gd_color); } } FILE *img_file = fopen(file_name, "w+"); if (img_file == NULL) { gdImageDestroy(img); perror("error opening file"); return 1; } gdImagePng(img, img_file); gdImageDestroy(img); if (fclose(img_file)) { perror("error closing file"); return 1; } return 0; }
int main() { gdImagePtr im; int white, black; char *path; im = gdImageCreateTrueColor(6, 6); white = gdImageColorAllocate(im, 255, 255, 255); black = gdImageColorAllocate(im, 0, 0, 0); gdImageFilledRectangle(im, 0,0, 5,5, white); gdImageLine(im, 4,4, 4,4, black); gdImageLine(im, 1,4, 2,4, black); gdImageLine(im, 4,1, 4,2, black); gdImageSetAntiAliased(im, black); gdImageLine(im, 1,1, 1,1, gdAntiAliased); path = gdTestFilePath2("gdimageline", "bug00315_exp.png"); gdAssertImageEqualsToFile(path, im); gdFree(path); gdImageDestroy(im); return gdNumFailures(); }
static void resize(Cmd *cmd) { char *buff = cmd->data; int len = cmd->size; Gd *gd = cmd->gd; int index = 0; unsigned long width, height, srcW, srcH; gdImagePtr destination = NULL; ei_decode_version(buff, &index, NULL); ei_decode_tuple_header(buff, &index, NULL); ei_decode_ulong(buff, &index, &width); ei_decode_ulong(buff, &index, &height); if (NULL == gd->image) { driver_failure_atom(gd->port, "null_image"); return; } srcW = gdImageSX(gd->image); srcH = gdImageSY(gd->image); destination = gdImageCreateTrueColor(width, height); if (NULL == destination) { driver_failure_posix(gd->port, ENOMEM); return; } gdImageCopyResampled(destination, gd->image, 0, 0, 0, 0, width, height, srcW, srcH); gdImageDestroy(gd->image); gd->image = destination; send_atom(gd->port, "ok"); }
gdImage *fx_scale(gdImage *src, char *options) { int w, h; gdImage *im; w = argtol(options, "x ", 0, 0, 10); h = argtol(options, "x ", 1, 0, 10); if(w < 0 || h < 0) { WARN("Invalid resolution: %s", options); return(src); } MSG("Scaling image from %ix%i -> %ix%i.", gdImageSX(src), gdImageSY(src), w, h); im = gdImageCreateTrueColor(w, h); if(!im) { WARN("Out of memory."); return(src); } gdImageCopyResampled(im, src, 0, 0, 0, 0, w, h, gdImageSX(src), gdImageSY(src)); gdImageDestroy(src); return(im); }
static int szm_do_scale_png(char *in, int in_len, uint8_t scale, char **out, int *out_len) { gdImagePtr im_in, im_out; int x, y, new_x, new_y; im_in = gdImageCreateFromPngPtr(in_len, in); if (!im_in) return -SZM_ERR_IMGFMT; x = gdImageSX(im_in); y = gdImageSY(im_in); new_x = x / scale; new_y = y / scale; if (new_x == 0) new_x = 1; if (new_y == 0) new_y = 1; im_out = gdImageCreateTrueColor(new_x, new_y); if (!im_out) { gdImageDestroy(im_in); return -SZM_ERR_IMGPROC; } gdImageCopyResized(im_out, im_in, 0, 0, 0, 0, im_out->sx, im_out->sy, im_in->sx, im_in->sy); *out = gdImagePngPtr(im_out, out_len); gdImageDestroy(im_in); gdImageDestroy(im_out); return 0; }
int main() { gdImagePtr im; int error = 0; char path[2048]; const char *file_exp = "bug00111_exp.png"; im = gdImageCreateTrueColor(10, 10); if (!im) { printf("can't get truecolor image\n"); return 1; } gdImageLine(im, 2, 2, 2, 2, 0xFFFFFF); gdImageLine(im, 5, 5, 5, 5, 0xFFFFFF); gdImageLine(im, 0, 0, 0, 0, 0xFFFFFF); sprintf(path, "%s/gdimageline/%s", GDTEST_TOP_DIR, file_exp); if (!gdAssertImageEqualsToFile(path, im)) { error = 1; printf("Reference image and destination differ\n"); } gdImageDestroy(im); return error; }
int main() { gdImagePtr im; int c1,c2,c3,c4; im = gdImageCreateTrueColor(10,10); if (!im) { return 1; } gdImageRectangle(im, 1,1, 1,1, 0x50FFFFFF); c1 = gdImageGetTrueColorPixel(im, 1, 1); c2 = gdImageGetTrueColorPixel(im, 2, 1); c3 = gdImageGetTrueColorPixel(im, 1, 2); c4 = gdImageGetTrueColorPixel(im, 2, 2); gdImageDestroy(im); if (0x005e5e5e == c1 && 0x0 == c2 && 0x0 == c3 && 0x0 == c4) { return 0; } else { return 1; } }
static gdImagePtr ngx_http_image_new(ngx_http_request_t *r, int w, int h, int colors) { gdImagePtr img; if (colors == 0) { img = gdImageCreateTrueColor(w, h); if (img == NULL) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "gdImageCreateTrueColor() failed"); return NULL; } } else { img = gdImageCreate(w, h); if (img == NULL) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "gdImageCreate() failed"); return NULL; } } return img; }
int output_gd_png(void *_array, int width, int height, int depth, const char *filename, color colors[], int numColors) { int x, y; int (*array)[height][width] = _array; int color=0; // Draw to buffer gdImagePtr im; im = gdImageCreateTrueColor(width, height); for (y=0; y<height; y++) for (x=0; x<width; x++) { if ((*array)[y][x] == -1) color=0; else color=colors[(*array)[y][x]].hex; //return_color(colors, numColors, depth, (*array)[y][x]); gdImageSetPixel(im, x, y, color); } // Write to file save_png(im, filename); gdImageDestroy(im); return 0; }
static void gd_begin_graph_to_file(graph_t* g, box bb, point pb) { char *truecolor_p; gd_begin_graph(g, bb, pb); if (Verbose) fprintf(stderr,"%s: allocating a %dK GD image\n", CmdName, ROUND( Viewport.x * Viewport.y / 1024. )); truecolor_p = agget(g,"truecolor"); /* automatically decide color mode if "truecolor" not specified */ if (truecolor_p == NULL || *truecolor_p == '\0') { truecolor_p="false"; /* indexed color maps unless ... */ if (agfindattr(g->proto->n, "shapefile")) { /* ... custom shapefiles are used */ truecolor_p = "true"; } } if (mapbool(truecolor_p)) im = gdImageCreateTrueColor(Viewport.x, Viewport.y); else im = gdImageCreate(Viewport.x, Viewport.y); init_gd(); #ifdef MYTRACE fprintf(stderr,"gd_begin_graph_to_file\n"); #endif }
int main() { gdImagePtr im; const char *exp = "bug00077_exp.png"; const int files_cnt = 4; FILE *fp; int i = 0; int error = 0; char path[1024]; im = gdImageCreateTrueColor(11, 11); gdImageFilledRectangle(im, 0, 0, 10, 10, 0xFFFFFF); gdImageSetThickness(im, 1); gdImageLine(im, 0, 10, 0, 0, 0x0); gdImageLine(im, 5, 10, 5, 0, 0x0); gdImageLine(im, 10, 5, 0, 5, 0x0); gdImageLine(im, 10, 10, 0, 10, 0x0); sprintf(path, "%s/gdimageline/%s", GDTEST_TOP_DIR, exp); if (!gdAssertImageEqualsToFile(path, im)) { error = 1; } gdImageDestroy(im); return error; }
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 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; } }
int main() { gdImagePtr im; int x, y, c; FILE *out; char path[1024]; int r=0; im = gdImageCreateTrueColor(256, 256); gdImageAlphaBlending( im, gdEffectReplace ); for (x=0; x<256; x++) { for (y=0; y<256; y++) { c = (y/2 << 24) + (x << 16) + (x << 8) + x; gdImageSetPixel(im, x, y, c ); } } gdImageAlphaBlending( im, gdEffectOverlay ); gdImageFilledRectangle(im, 0, 0, 255, 255, 0xff7f00); if (gdTrueColorGetGreen(gdImageGetPixel(im, 0, 128)) != 0x00) { r = 1; } if (gdTrueColorGetGreen(gdImageGetPixel(im, 128, 128)) != 0x80) { r = 1; } if (gdTrueColorGetGreen(gdImageGetPixel(im, 255, 128)) != 0xff) { r = 1; } gdImageDestroy(im); return r; }
static int szm_do_resize_jpg(char *in, int in_len, uint16_t new_x, uint16_t new_y, char **out, int *out_len) { int x, y; gdImagePtr im_in, im_out; im_in = gdImageCreateFromJpegPtr(in_len, in); if (!im_in) return -SZM_ERR_IMGFMT; x = gdImageSX(im_in); y = gdImageSY(im_in); if (new_x == 0) new_x = 1; if (new_y == 0) new_y = 1; im_out = gdImageCreateTrueColor(new_x, new_y); if (!im_out) { gdImageDestroy(im_in); return -SZM_ERR_IMGPROC; } gdImageCopyResized(im_out, im_in, 0, 0, 0, 0, im_out->sx, im_out->sy, im_in->sx, im_in->sy); *out = gdImageJpegPtr(im_out, out_len, -1); gdImageDestroy(im_in); gdImageDestroy(im_out); return 0; }
int main() { gdImagePtr im; FILE *fp; int cor_rad = 60; im = gdImageCreateTrueColor(400, 400); gdImageFilledRectangle(im, 0, 0, 399, 399, 0x00FFFFFF); gdImageFilledArc (im, cor_rad, 399 - cor_rad, cor_rad *2, cor_rad *2, 90, 180, 0x0, gdPie); //fp = fopen("b.gif", "wb"); fp = fopen("b.png", "wb"); if (!fp) { fprintf(stderr, "Can't open file to save image.\n"); gdImageDestroy(im); return 1; } // gdImageGif(im, fp); #ifdef HAVE_LIBPNG gdImagePng(im, fp); #else printf("No PNG support. Cannot save image.\n"); #endif fclose(fp); gdImageDestroy(im); return 0; }
Gd::Gd(int x, int y, bool trueColor) : _imagePtr(0) { if (trueColor) _imagePtr = gdImageCreateTrueColor(x, y); else _imagePtr = gdImageCreate(x, y); }
gdImage *fx_rotate(gdImage *src, char *options) { int x, y; gdImage *im; int angle = atoi(options); /* Restrict angle to 0-360 range. */ if((angle %= 360) < 0) angle += 360; /* Round to nearest right angle. */ x = (angle + 45) % 360; angle = x - (x % 90); /* Not rotating 0 degrees. */ if(angle == 0) { MSG("Not rotating 0 degrees."); return(src); } /* 180 can be done with two flips. */ if(angle == 180) { fx_flip(src, "h,v"); return(src); } MSG("Rotating image %i degrees. %ix%i -> %ix%i.", angle, gdImageSX(src), gdImageSY(src), gdImageSY(src), gdImageSX(src)); /* Create rotated image. */ im = gdImageCreateTrueColor(gdImageSY(src), gdImageSX(src)); if(!im) { WARN("Out of memory."); return(src); } for(y = 0; y < gdImageSY(src); y++) for(x = 0; x < gdImageSX(src); x++) { int c = gdImageGetPixel(src, x, y); if(angle == 90) { gdImageSetPixel(im, gdImageSX(im) - y - 1, x, c); } else { gdImageSetPixel(im, y, gdImageSY(im) - x - 1, c); } } gdImageDestroy(src); return(im); }
/* If palette is true, we convert from truecolor to palette at the end, to test gdImageTrueColorToPalette and see file size/ quality tradeoffs. */ void testDrawing (gdImagePtr im_in, double scale, int blending, int palette, char *filename) { gdImagePtr im_out; FILE *out; /* Create output image. */ im_out = gdImageCreateTrueColor ((int) (gdImageSX (im_in) * scale), (int) (gdImageSY (im_in) * scale)); /* Request alpha blending. This causes future drawing operations to perform alpha channel blending with the background, resulting in an opaque image. Without this call, pixels in the foreground color are copied literally, *including* the alpha channel value, resulting in an output image which is potentially not opaque. This flag can be set and cleared as often as desired. */ gdImageAlphaBlending (im_out, blending); /* Flood with light blue. */ gdImageFill (im_out, (int) (gdImageSX (im_in) * scale / 2), (int) (gdImageSY (im_in) * scale / 2), gdTrueColor (192, 192, 255)); /* Copy the source image. Alpha blending should result in compositing against red. With blending turned off, the browser or viewer will composite against its preferred background, or, if it does not support an alpha channel, we will see the original colors for the pixels that ought to be transparent or semitransparent. */ gdImageCopyResampled (im_out, im_in, 0, 0, 0, 0, (int) (gdImageSX (im_in) * scale), (int) (gdImageSY (im_in) * scale), gdImageSX (im_in), gdImageSY (im_in)); /* Write PNG */ out = fopen (filename, "wb"); /* If this image is the result of alpha channel blending, it will not contain an interesting alpha channel itself. Save a little file size by not saving the alpha channel. Otherwise the file would typically be slightly larger. */ gdImageSaveAlpha (im_out, !blending); /* If requested, convert from truecolor to palette. */ if (palette) { /* Dithering, 256 colors. */ gdImageTrueColorToPalette (im_out, 1, 256); } gdImagePng (im_out, out); fclose (out); gdImageDestroy (im_out); }
void hqx_scale(gdImagePtr srcIm, int factor, gdImagePtr* result) { gdImagePtr dstIm; uint32_t *srcBuffer, *dstBuffer; size_t srcSize, dstSize; uint32_t w, h; uint32_t x, y; w = gdImageSX(srcIm); h = gdImageSY(srcIm); srcSize = sizeof(uint32_t) * w * h; dstSize = sizeof(uint32_t) * factor * w * factor * h; // Unfortunately it doesn't work to simply pass the gd buffer from the // gdImage struct to hqx, and the gd documentation explicitly says // not to access member fields directly. Thus we allocate two buffers // and copy the data back and forth as a workaround. srcBuffer = (uint32_t*)emalloc(srcSize); for (y = 0; y < h; y++) { for (x = 0; x < w; x++) { srcBuffer[w*y + x] = gdImageGetPixel(srcIm, x, y); } } dstBuffer = (uint32_t*)emalloc(dstSize); if (factor == 4) { hq4x_32_rb(srcBuffer, sizeof(uint32_t)*w, dstBuffer, sizeof(uint32_t)*factor*w, w, h); } else if (factor == 3) { hq3x_32_rb(srcBuffer, sizeof(uint32_t)*w, dstBuffer, sizeof(uint32_t)*factor*w, w, h); } else if (factor == 2) { hq2x_32_rb(srcBuffer, sizeof(uint32_t)*w, dstBuffer, sizeof(uint32_t)*factor*w, w, h); } dstIm = gdImageCreateTrueColor(factor*w, factor*h); gdImageAlphaBlending(dstIm, 0); gdImageSaveAlpha(dstIm, 1); for (y = 0; y < factor*h; y++) { for (x = 0; x < factor*w; x++) { gdImageSetPixel(dstIm, x, y, dstBuffer[factor*w*y + x]); } } efree(srcBuffer); efree(dstBuffer); *result = dstIm; }