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, tile; int im_white, im_black, tile_white, tile_black; int x,y, error = 0; FILE *fp; char path[1024]; fputs("flag 0\n", stdout); im = gdImageCreate(150, 150); tile = gdImageCreateTrueColor(36, 36); tile_white = gdImageColorAllocate(tile,255,255,255); tile_black = gdImageColorAllocate(tile,55,0,0); im_white = gdImageColorAllocate(im,255,255,255); im_black = gdImageColorAllocate(im,0,0,0); gdImageFill(tile, 0,0, tile_white); gdImageColorTransparent(tile, tile_black); gdImageColorTransparent(im, im_black); /* create the dots pattern */ for (x=0; x<36; x+=2) { for (y=0; y<36; y+=2) { gdImageSetPixel(tile,x,y,tile_black); } } gdImageSetTile(im,tile); gdImageRectangle(im, 9,9,139,139, im_black); gdImageLine(im, 9,9,139,139, im_black); gdImageFill(im, 11,12, gdTiled); fputs("flag 1\n", stdout); gdImageFill(im, 0, 0, 0xffffff); fputs("flag 2\n", stdout); gdImageFill(im, 0, 0, 0xffffff); fputs("flag 3\n", stdout); sprintf(path, "%s/gdimagefill/bug00002_3_exp.png", GDTEST_TOP_DIR); if (!gdAssertImageEqualsToFile(path, im)) { error = 1; } /* Destroy it */ gdImageDestroy(im); return error; }
int main() { gdImagePtr im, im2; int error = 0; im = gdImageCreateTrueColor(5, 5); if (!im) { printf("can't create the src truecolor image\n"); return 1; } gdImageFilledRectangle(im, 0, 0, 49, 49, 0x00FFFFFF); gdImageColorTransparent(im, 0xFFFFFF); gdImageFilledRectangle(im, 1, 1, 4, 4, 0xFF00FF); im2 = gdImageCreateTrueColor(20, 20); if (!im2) { printf("can't create the dst truecolor image\n"); gdImageDestroy(im); return 1; } gdImageCopy(im2, im, 2, 2 , 0, 0, gdImageSX(im), gdImageSY(im)); if (!gdAssertImageEqualsToFile("gdimagecopy/bug00081_exp.png", im2)) { error = 1; printf("Reference image and destination differ\n"); } gdImageDestroy(im); gdImageDestroy(im2); return error; }
static void vrml_begin_node(GVJ_t *job) { FILE *out = job->output_file; obj_state_t *obj = job->obj; node_t *n = obj->u.n; double z = obj->z; int width, height; int transparent; fprintf(out, "# node %s\n", n->name); if (z < MinZ) MinZ = z; if (shapeOf(n) != SH_POINT) { PNGfile = nodefile(job->output_filename, n); width = (ND_lw_i(n) + ND_rw_i(n)) * Scale + 2 * NODE_PAD; height = (ND_ht_i(n) ) * Scale + 2 * NODE_PAD; im = gdImageCreate(width, height); /* make background transparent */ transparent = gdImageColorResolveAlpha(im, gdRedMax - 1, gdGreenMax, gdBlueMax, gdAlphaTransparent); gdImageColorTransparent(im, transparent); } }
static void init_gd() { SP = 0; /* must create default background color first... */ /* we have to force the background to be filled for some reason */ white = gdImageColorResolve(im, gdRedMax,gdGreenMax,gdBlueMax); gdImageFilledRectangle(im, 0, 0, im->sx-1, im->sy-1, white); black = gdImageColorResolve(im, 0, 0, 0); /* in truecolor images we don't need to allocate a color for transparent */ if (! im->trueColor) { /* transparent uses an rgb value very close to white so that formats like GIF that don't support transparency show a white background */ transparent = gdImageColorResolveAlpha(im, gdRedMax,gdGreenMax,gdBlueMax-1, gdAlphaTransparent); gdImageColorTransparent(im, transparent); } cstk[0].pencolor = black; /* set pen black*/ cstk[0].fillcolor = black; /* set fill black*/ cstk[0].fontfam = "times"; /* font family name */ cstk[0].fontopt = REGULAR; /* modifier: REGULAR, BOLD or ITALIC */ cstk[0].pen = P_SOLID; /* pen pattern style, default is solid */ cstk[0].fill = P_NONE; cstk[0].penwidth = WIDTH_NORMAL; }
int renderPixmapSymbolGD(imageObj *img, double x, double y, symbolObj *symbol, symbolStyleObj *style) { gdImagePtr ip,pp; if(!(ip = MS_IMAGE_GET_GDIMAGEPTR(img))) return MS_FAILURE; assert(symbol->pixmap_buffer && symbol->pixmap_buffer->type == MS_BUFFER_GD); pp = symbol->pixmap_buffer->data.gd_img; /* gdImageAlphaBlending(ip,1); */ /* gdImageAlphaBlending(pp,1); */ if(symbol->transparent) gdImageColorTransparent(pp,symbol->transparentcolor); if(style->scale == 1.0 && style->rotation == 0.0) { /* don't scale */ x -= .5*symbol->pixmap_buffer->width; y -= .5*symbol->pixmap_buffer->height; gdImageCopy(ip, pp, x, y, 0, 0, symbol->pixmap_buffer->width,symbol->pixmap_buffer->height); } else { int bRotated = MS_FALSE; if(style->rotation) { bRotated = MS_TRUE; pp = rotatePixmapGD(pp,style->rotation); } x -= .5*gdImageSX(pp)*style->scale; y -= .5*gdImageSY(pp)*style->scale; gdImageCopyResampled(ip, pp, x, y, 0, 0, (int)(gdImageSX(pp) * style->scale), (int)(gdImageSY(pp) * style->scale), gdImageSX(pp),gdImageSY(pp)); if(bRotated) { gdImageDestroy(pp); } } /* gdImageAlphaBlending(ip,0); */ return MS_SUCCESS; }
/* * Bueno, vamos a ver si podemos obtener la pieza * */ int tipojuego_get_tpieza_png( Tipojuego* tj, char* color, char* tpieza, int flags, void** png, int* width, int* height ){ #if GRAPH_ENABLED Tipopieza* tp = tj->tipo_piezas->data[ GETTIPOPIEZA(tj,tpieza) ]; int col = GETCOLOR(tj,color); gdImagePtr gd = graph_tpieza_get_gd( tp, col ); int size = 0; if( flags == GETPNG_PIEZA_CAPTURADA ){ gdImagePtr gd2 = gdImageCreate( gdImageSX( gd ) / 2, gdImageSY( gd ) / 2 ); int transp = gdImageColorAllocate( gd2, 0, 255, 0 ); gdImageColorTransparent( gd2, transp ); gdImageFill( gd2, 0, 0 , transp ); gdImageCopyResized( gd2, gd, 0, 0, 0, 0, gdImageSX(gd2), gdImageSY(gd2), gdImageSX(gd), gdImageSY(gd) ); if( png ) *png = gdImagePngPtr( gd2, &size ); if( width ) *width = gdImageSX( gd2 ); if( height ) *height = gdImageSY( gd2 ); gdImageDestroy( gd2 ); return size; } else { if( png ) *png = gdImagePngPtr( gd, &size ); if( width ) *width = gdImageSX( gd ); if( height ) *height = gdImageSY( gd ); return size; } #endif LOGPRINT( 2, "No compilado con el modulo GD tpieza = %s", tpieza ); return 0; }
result_t Image::set_transparent(int32_t newVal) { if (!m_image) return CHECK_ERROR(CALL_E_INVALID_CALL); gdImageColorTransparent(m_image, newVal); return 0; }
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; }
static gdImagePtr InitImage(void) { gdImagePtr im; im = gdImageCreate(150, 350); allocate_colors(im); gdImageColorTransparent (im, grey); gdImageFilledRectangle (im, 0, 0, 150, 350, grey); gdImageFilledRectangle (im, 50, 0, 150, 300, green); return im; }
int renderPolygonTiledGD(imageObj *img, shapeObj *p, imageObj *tile) { gdImagePtr ip, tp; if(!img || !p || !tile) return MS_FAILURE; if(!(ip = MS_IMAGE_GET_GDIMAGEPTR(img))) return MS_FAILURE; if(!(tp = MS_IMAGE_GET_GDIMAGEPTR(tile))) return MS_FAILURE; gdImageColorTransparent(tp,0); gdImageSetTile(ip, tp); imageFilledPolygon(ip, p, gdTiled); return MS_SUCCESS; }
JBoolean SetTransparentColor ( gdImagePtr image, const int color ) { if (color != kTransparentColorNotSet) { gdImageColorTransparent(image, color); return kJTrue; } else { return kJFalse; } }
/* int GIFFlusht0(Metafile *mf, char *filename) transparent, color 0 */ int GIFFlusht0(Metafile *mf, char *filename) { mf_cgmo *cgmo = mf->cgmo; GIFmetafile *meta = find_meta(cgmo); int status = 1; int tcolor; int i; FILE *fp = fopen(filename, "w"); if (fp != NULL && meta != 0){ status = OK; gdImageGif(meta->image, fp);/*save the image as gif */ fclose(fp); /* open the gif, change transparency and then save */ FILE *inGif; FILE *ouGif; gdImagePtr gifImg; inGif = fopen(filename,"rb"); gifImg = gdImageCreateFromGif(inGif); fclose(inGif); ouGif = fopen(filename,"w"); //(0,0,0) is for white background tcolor = gdImageColorClosest(gifImg, 255, 255, 255); for (i=0; i < 256; i+=1) { tcolor = gdImageColorClosest(gifImg, i, i, i); } gdImageColorTransparent(gifImg, tcolor); gdImageGif(gifImg, ouGif); fclose(ouGif); gdImageDestroy(gifImg); } return status; }
int main (int argc, char *argv[]) { #ifdef HAVE_LIBFREETYPE FILE *out; int transparent, green, black; gdImagePtr im; im = gdImageCreateTrueColor(100,100); black = gdImageColorResolveAlpha(im, 0, 0, 0, gdAlphaOpaque); green = gdImageColorResolveAlpha(im, 0, gdGreenMax, 0, gdAlphaOpaque); transparent = gdImageColorResolveAlpha(im, gdRedMax-1, gdGreenMax, gdBlueMax, gdAlphaTransparent); gdImageColorTransparent(im, transparent); /* Blending must be off to lay a transparent basecolor. Nothing to blend with anyway. */ gdImageAlphaBlending(im, FALSE); gdImageFill (im, im->sx/2, im->sy/2, transparent); /* Blend everything else together, especially fonts over non-transparent backgrounds */ gdImageAlphaBlending(im, TRUE); gdImageFilledRectangle (im, 30, 30, 70, 70, green); gdImageStringFT (im, NULL, black, "Times", 18, 0, 50, 50, "Hello"); gdImageSaveAlpha (im, TRUE); #ifdef HAVE_LIBPNG out = fopen ("testtr.png", "wb"); gdImagePng (im, out); fclose (out); #else fprintf(stderr, "Compiled without libpng support\n"); #endif /* HAVE_LIBPNG */ gdImageDestroy (im); return 0; #else fprintf(stderr, "Compiled without freetype support\n"); return 0; #endif /* HAVE_LIBFREETYPE */ }
result_t Image::rotate(int32_t dir) { if (dir != gd_base::_LEFT && dir != gd_base::_RIGHT) return CHECK_ERROR(CALL_E_INVALIDARG); int32_t sx = gdImageSX(m_image); int32_t sy = gdImageSY(m_image); int32_t i, j; gdImagePtr newImage; if (gdImageTrueColor(m_image)) newImage = gdImageCreateTrueColor(sy, sx); else { newImage = gdImageCreate(sy, sx); gdImagePaletteCopy(newImage, m_image); } gdImageColorTransparent(newImage, gdImageGetTransparent(m_image)); if (dir == gd_base::_LEFT) { for (i = 0; i < sx; i++) for (j = 0; j < sy; j++) gdImageSetPixel(newImage, j, sx - i - 1, gdImageGetPixel(m_image, i, j)); } else { for (i = 0; i < sx; i++) for (j = 0; j < sy; j++) gdImageSetPixel(newImage, sy - j - 1, i, gdImageGetPixel(m_image, i, j)); } setExtMemory(-1); gdImageDestroy(m_image); m_image = newImage; setExtMemory(); return 0; }
static void init2_gd(gdImagePtr im) { SP = 0; white = gdImageColorResolveAlpha(im, gdRedMax, gdGreenMax, gdBlueMax, gdAlphaOpaque); black = gdImageColorResolveAlpha(im, 0, 0, 0, gdAlphaOpaque); transparent = gdImageColorResolveAlpha(im, gdRedMax - 1, gdGreenMax, gdBlueMax, gdAlphaTransparent); gdImageColorTransparent(im, transparent); cstk[0].pencolor = black; /* set pen black */ cstk[0].fontfam = "times"; /* font family name */ cstk[0].fontopt = REGULAR; /* modifier: REGULAR, BOLD or ITALIC */ cstk[0].pen = P_SOLID; /* pen pattern style, default is solid */ cstk[0].fill = P_NONE; cstk[0].penwidth = WIDTH_NORMAL; }
result_t Image::New(int32_t width, int32_t height, obj_ptr<Image> &retVal) { if (width <= 0 || height <= 0) return CHECK_ERROR(CALL_E_INVALIDARG); obj_ptr<Image> img = new Image(); if (gdImageTrueColor(m_image)) img->m_image = gdImageCreateTrueColor(width, height); else img->m_image = gdImageCreate(width, height); gdImagePaletteCopy(img->m_image, m_image); gdImageColorTransparent(img->m_image, gdImageGetTransparent(m_image)); img->setExtMemory(); retVal = img; return 0; }
/* ** GD driver-specific image handling functions. */ imageObj *createImageGD(int width, int height, outputFormatObj *format, colorObj* bg) { imageObj *img = NULL; gdImagePtr ip; img = (imageObj *) calloc(1, sizeof (imageObj)); MS_CHECK_ALLOC(img, sizeof (imageObj), NULL); /* we're only doing PC256 for the moment */ ip = gdImageCreate(width, height); if(!format->transparent && bg && MS_VALID_COLOR(*bg)) gdImageColorAllocate(ip, bg->red, bg->green, bg->blue); /* set the background color */ else { gdImageColorAllocate(ip,117,17,91); /*random bg color (same one as picked in msResampleGDALToMap) */ gdImageColorTransparent(ip, 0); } img->img.plugin = (void *) ip; return img; }
static void make_thumb(void *conf) { int colors = 0; int transparent = -1; ngx_image_conf_t *info = conf; info->dst_im = gdImageCreateTrueColor(info->width,info->height); colors = gdImageColorsTotal(info->src_im); transparent = gdImageGetTransparent(info->src_im); if (transparent == -1) { gdImageSaveAlpha(info->src_im,1); gdImageColorTransparent(info->src_im, -1); if(colors == 0) { gdImageAlphaBlending(info->dst_im,0); gdImageSaveAlpha(info->dst_im,1); } if(colors) { gdImageTrueColorToPalette(info->dst_im,1,256); } } if(info->w_margin == 1) { info->w_im = gdImageCreateTrueColor(info->width,info->height); gdImageFilledRectangle(info->w_im, 0, 0, info->width,info->height, gdImageColorAllocate(info->w_im, 255, 255, 255)); info->dst_im = gdImageCreateTrueColor(info->max_width,info->max_height); gdImageFilledRectangle(info->dst_im, 0, 0, info->max_width,info->max_height, gdImageColorAllocate(info->dst_im, 255, 255, 255)); gdImageCopyResampled(info->w_im, info->src_im, 0, 0, info->src_x, info->src_y,info->width, info->height, info->src_w,info->src_h); gdImageCopyResampled(info->dst_im, info->w_im, info->dst_x,info->dst_y, 0, 0,info->width, info->height, info->width, info->height); gdImageDestroy(info->w_im); } else { gdImageCopyResampled(info->dst_im,info->src_im,info->dst_x,info->dst_y,info->src_x,info->src_y,info->width,info->height,info->src_w,info->src_h); } gdImageDestroy(info->src_im); }
void GDallocColorTransparent(void) { Proto proto; Proto tmp; Proto blob; gdImagePtr imgPtr; char * p; uchar t; proto = (Proto) stackAt(Cpu, 2); objectGetSlot(proto, stringToAtom("_gdp"), &blob); p = (char *) objectPointerValue(blob); memcpy(&imgPtr, p, sizeof(imgPtr)); tmp = (Proto) stackAt(Cpu, 4); t = objectIntegerValue(tmp); gdImageColorTransparent(imgPtr, t); VMReturn(Cpu, (unsigned int) proto, 4); }
static void noimage (void) { gdImagePtr im; im = gdImageCreate (150, 350); allocate_colors(im); gdImageColorTransparent (im, grey); gdImageFilledRectangle (im, 0, 0, 150, 300, grey); gdImageString (im, gdFontLarge, 0, 0, (unsigned char *)"Data not available", black); imgheader(); #ifdef SYS_IMGFMT_PNG gdImagePng (im, stdout); #else gdImageGif (im, stdout); #endif gdImageDestroy (im); }
void _transparent_bitmap_part(const void *src, int src_x, int src_y, int stride, int x, int y, int width, int height) { FILE *_image; gdImagePtr image; int pink; DEBUGF2("transparent_bitmap_part(const void *src=%s, int src_x=%d, int src_y=%d, int stride=%d, int x=%d, int y=%d, int width=%d, int height=%d\n", (char*)src, src_x, src_y, stride, x, y, width, height); _image = fopen(src, "rb"); if(_image == NULL) return; image = gdImageCreateFromBmp(_image); fclose(_image); pink = gdTrueColor(255, 0, 255); gdImageColorTransparent(image, pink); gdImageCopy(framebuffer, image, x, y, src_x, src_y, width, height); gdImageDestroy(image); }
int main() { gdImagePtr im, im2; int error = 0; im = gdImageCreateTrueColor(width, width); gdImageFilledRectangle(im, 0,0, width, width, 0xFF0000); gdImageColorTransparent(im, 0xFF0000); gdImageFilledEllipse(im, width/2, width/2, width - 20, width - 10, 0x50FFFFFF); im2 = gdImageCreateTrueColor(width, width); gdImageCopyRotated(im2, im, width / 2, width / 2, 0,0, width, width, 60); if (!gdAssertImageEqualsToFile("gdimagecopyrotated/bug00020_exp.png", im2)) { error = 1; } gdImageDestroy(im2); gdImageDestroy(im); return error; }
int main() { gdImagePtr im, tile; char exp[] = "bug00032_exp.png"; FILE *fp; tile = gdImageCreateTrueColor(10, 10); gdImageFill(tile, 0, 0, 0xFFFFFF); gdImageLine(tile, 0,0, 9,9, 0xff0000); gdImageColorTransparent(tile, 0xFFFFFF); im = gdImageCreateTrueColor(50, 50); gdImageFilledRectangle(im, 0, 0, 25, 25, 0x00FF00); gdImageSetTile(im, tile); gdImageFilledRectangle(im, 10, 10, 49, 49, gdTiled); gdAssertImageEqualsToFile(exp_img, im); /* Destroy it */ gdImageDestroy(im); gdImageDestroy(tile); return 0; }
static void writePng(char *filename, wlImages cursors, int cursorNo) { gdImagePtr output; int x, y, i; int palette[17]; int transparent; int color; FILE *file; wlImage image; image = cursors->images[cursorNo]; output = gdImageCreate(image->width, image->height); for (i = 0; i < 16; i++) { palette[i] = gdImageColorAllocate(output, wlPalette[i].red, wlPalette[i].green, wlPalette[i].blue); } transparent = gdImageColorAllocate(output, 0, 0, 0); gdImageColorTransparent(output, transparent); for (y = 0; y < image->height; y++) { for (x = 0; x < image->width; x++) { color = image->pixels[y * image->width + x]; gdImageSetPixel(output, x, y, color < 16 ? palette[color] : transparent); } } file = fopen(filename, "wb"); if (!file) { die("Unable to write PNG to %s: %s\n", filename, strerror(errno)); } gdImagePng(output, file); gdImageDestroy(output); fclose(file); }
int renderLineGD(imageObj *img, shapeObj *p, strokeStyleObj *stroke) { gdImagePtr ip; int c; gdImagePtr brush=NULL; if(!img || !p || !stroke) return MS_FAILURE; if(!(ip = MS_IMAGE_GET_GDIMAGEPTR(img))) return MS_FAILURE; SETPEN(ip, stroke->color); c = stroke->color->pen; if(stroke->patternlength > 0) { int *style; int i, j, k=0; int sc; for(i=0; i<stroke->patternlength; i++) k += MS_NINT(stroke->pattern[i]); style = (int *) malloc (k * sizeof(int)); MS_CHECK_ALLOC(style, k * sizeof(int), MS_FAILURE); sc = c; /* start with the color */ k=0; for(i=0; i<stroke->patternlength; i++) { for(j=0; j<MS_NINT(stroke->pattern[i]); j++, k++) { style[k] = sc; } sc = ((sc==c)?gdTransparent:c); } gdImageSetStyle(ip, style, k); free(style); c = gdStyled; } if(stroke->width > 1) { int brush_fc; brush = gdImageCreate(ceil(stroke->width), ceil(stroke->width)); gdImageColorAllocate(brush, gdImageRed(ip,0), gdImageGreen(ip, 0), gdImageBlue(ip, 0)); gdImageColorTransparent(brush,0); brush_fc = gdImageColorAllocate(brush, gdImageRed(ip,stroke->color->pen), gdImageGreen(ip,stroke->color->pen), gdImageBlue(ip,stroke->color->pen)); gdImageFilledEllipse(brush,MS_NINT(brush->sx/2),MS_NINT(brush->sy/2), MS_NINT(stroke->width),MS_NINT(stroke->width), brush_fc); gdImageSetBrush(ip, brush); if(stroke->patternlength > 0) { c = gdStyledBrushed; } else { c = gdBrushed; } } /* finally draw something */ imagePolyline(ip, p, c); /* clean up */ if(stroke->width>1) { gdImageDestroy(brush); } return MS_SUCCESS; }
static ngx_buf_t * ngx_http_image_resize(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx) { int sx, sy, dx, dy, ox, oy, ax, ay, size, colors, palette, transparent, sharpen, red, green, blue, t, offset_x, offset_y; u_char *out; ngx_buf_t *b; ngx_uint_t resize; gdImagePtr src, dst; ngx_pool_cleanup_t *cln; ngx_http_image_filter_conf_t *conf; src = ngx_http_image_source(r, ctx); if (src == NULL) { return NULL; } sx = gdImageSX(src); sy = gdImageSY(src); conf = ngx_http_get_module_loc_conf(r, ngx_http_image_filter_module); if (!ctx->force && ctx->angle == 0 && (ngx_uint_t) sx <= ctx->max_width && (ngx_uint_t) sy <= ctx->max_height) { gdImageDestroy(src); return ngx_http_image_asis(r, ctx); } colors = gdImageColorsTotal(src); if (colors && conf->transparency) { transparent = gdImageGetTransparent(src); if (transparent != -1) { palette = colors; red = gdImageRed(src, transparent); green = gdImageGreen(src, transparent); blue = gdImageBlue(src, transparent); goto transparent; } } palette = 0; transparent = -1; red = 0; green = 0; blue = 0; transparent: gdImageColorTransparent(src, -1); dx = sx; dy = sy; if (conf->filter == NGX_HTTP_IMAGE_RESIZE) { if ((ngx_uint_t) dx > ctx->max_width) { dy = dy * ctx->max_width / dx; dy = dy ? dy : 1; dx = ctx->max_width; } if ((ngx_uint_t) dy > ctx->max_height) { dx = dx * ctx->max_height / dy; dx = dx ? dx : 1; dy = ctx->max_height; } resize = 1; } else if (conf->filter == NGX_HTTP_IMAGE_ROTATE) { resize = 0; } else { /* NGX_HTTP_IMAGE_CROP */ resize = 0; if ((double) dx / dy < (double) ctx->max_width / ctx->max_height) { if ((ngx_uint_t) dx > ctx->max_width) { dy = dy * ctx->max_width / dx; dy = dy ? dy : 1; dx = ctx->max_width; resize = 1; } } else { if ((ngx_uint_t) dy > ctx->max_height) { dx = dx * ctx->max_height / dy; dx = dx ? dx : 1; dy = ctx->max_height; resize = 1; } } } if (resize) { dst = ngx_http_image_new(r, dx, dy, palette); if (dst == NULL) { gdImageDestroy(src); return NULL; } if (colors == 0) { gdImageSaveAlpha(dst, 1); gdImageAlphaBlending(dst, 0); } gdImageCopyResampled(dst, src, 0, 0, 0, 0, dx, dy, sx, sy); if (colors) { gdImageTrueColorToPalette(dst, 1, 256); } gdImageDestroy(src); } else { dst = src; } if (ctx->angle) { src = dst; ax = (dx % 2 == 0) ? 1 : 0; ay = (dy % 2 == 0) ? 1 : 0; switch (ctx->angle) { case 90: case 270: dst = ngx_http_image_new(r, dy, dx, palette); if (dst == NULL) { gdImageDestroy(src); return NULL; } if (ctx->angle == 90) { ox = dy / 2 + ay; oy = dx / 2 - ax; } else { ox = dy / 2 - ay; oy = dx / 2 + ax; } gdImageCopyRotated(dst, src, ox, oy, 0, 0, dx + ax, dy + ay, ctx->angle); gdImageDestroy(src); t = dx; dx = dy; dy = t; break; case 180: dst = ngx_http_image_new(r, dx, dy, palette); if (dst == NULL) { gdImageDestroy(src); return NULL; } gdImageCopyRotated(dst, src, dx / 2 - ax, dy / 2 - ay, 0, 0, dx + ax, dy + ay, ctx->angle); gdImageDestroy(src); break; } } if (conf->filter == NGX_HTTP_IMAGE_CROP) { src = dst; if ((ngx_uint_t) dx > ctx->max_width) { ox = dx - ctx->max_width; } else { ox = 0; } if ((ngx_uint_t) dy > ctx->max_height) { oy = dy - ctx->max_height; } else { oy = 0; } if (ox || oy) { dst = ngx_http_image_new(r, dx - ox, dy - oy, colors); if (dst == NULL) { gdImageDestroy(src); return NULL; } offset_x = ngx_http_image_filter_get_value(r, conf->oxcv, conf->offset_x); offset_y = ngx_http_image_filter_get_value(r, conf->oycv, conf->offset_y); if (offset_x == NGX_HTTP_IMAGE_OFFSET_LEFT) { ox = 0; } else if (offset_x == NGX_HTTP_IMAGE_OFFSET_CENTER) { ox /= 2; } if (offset_y == NGX_HTTP_IMAGE_OFFSET_TOP) { oy = 0; } else if (offset_y == NGX_HTTP_IMAGE_OFFSET_CENTER) { oy /= 2; } ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "image crop: %d x %d @ %d x %d", dx, dy, ox, oy); if (colors == 0) { gdImageSaveAlpha(dst, 1); gdImageAlphaBlending(dst, 0); } gdImageCopy(dst, src, 0, 0, ox, oy, dx - ox, dy - oy); if (colors) { gdImageTrueColorToPalette(dst, 1, 256); } gdImageDestroy(src); } } if (transparent != -1 && colors) { gdImageColorTransparent(dst, gdImageColorExact(dst, red, green, blue)); } sharpen = ngx_http_image_filter_get_value(r, conf->shcv, conf->sharpen); if (sharpen > 0) { gdImageSharpen(dst, sharpen); } out = ngx_http_image_out(r, ctx->type, dst, &size); ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "image: %d x %d %d", sx, sy, colors); gdImageDestroy(dst); ngx_pfree(r->pool, ctx->image); if (out == NULL) { return NULL; } cln = ngx_pool_cleanup_add(r->pool, 0); if (cln == NULL) { gdFree(out); return NULL; } b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); if (b == NULL) { gdFree(out); return NULL; } cln->handler = ngx_http_image_cleanup; cln->data = out; b->pos = out; b->last = out + size; b->memory = 1; b->last_buf = 1; ngx_http_image_length(r, b); return b; }
// Generates the count image from the count string, returns true if all ok bool CreateImage(const char *pcCount, const char *pcFont, int iDigits) { // Set the spare value int iCountLen = strlen(pcCount); if (iDigits < iCountLen) iDigits = iCountLen; int iSpare = 0; if (iDigits > iCountLen) iSpare = iDigits - iCountLen; // Loop vars char pcDigitName[STR_SIZE] = ""; gdImagePtr pImg = NULL; FILE *pPic = NULL; // Load the zero digit image sprintf(pcDigitName, COUNTER_ZERO, pcFont); pPic = fopen(pcDigitName, "rb+"); if (pPic == NULL) return false; pImg = gdImageCreateFromGif(pPic); fclose(pPic); if (pImg == NULL) return false; // Create the output image gdImagePtr pOutImg = NULL; pOutImg = gdImageCreate(iDigits * pImg->sx, pImg->sy); if (pOutImg == NULL) { gdImageDestroy(pImg); return false; } // Copy the palette info from one to the other... int iColTotal = gdImageColorsTotal(pImg); for (int i = 0; i < iColTotal; i++) { gdImageColorAllocate(pOutImg, gdImageRed(pImg, i), gdImageGreen(pImg, i), gdImageBlue(pImg, i)); } int iTransCol = gdImageGetTransparent(pImg); if (iTransCol >= 0) { gdImageColorTransparent(pOutImg, iTransCol); gdImageFill(pOutImg, 0, 0, iTransCol); } // Loop through each leading zero int iPos = 0; while (iSpare-- > 0) { // paste in the image gdImageCopy(pOutImg, pImg, iPos, 0, 0, 0, pImg->sx, pImg->sy); iPos += pImg->sx; } // Delete the zero image gdImageDestroy(pImg); // Loop through each counter character const char *pcPos = pcCount; while (*pcPos != '\0') { sprintf(pcDigitName, COUNTER_PIC, pcFont, *pcPos); // Load the image pPic = fopen(pcDigitName, "rb+"); if (pPic == NULL) { gdImageDestroy(pOutImg); return false; } pImg = gdImageCreateFromGif(pPic); if (pImg == NULL) { gdImageDestroy(pOutImg); return false; } // Paste in the image gdImageCopy(pOutImg, pImg, iPos, 0, 0, 0, pImg->sx, pImg->sy); iPos += pImg->sx; // Delete the image gdImageDestroy(pImg); // Update the position counter pcPos++; } // Write out the output image if (g_oCGI.Debug()) { char pcGIFName[STR_SIZE] = COUNTER_ROOT; strcat(pcGIFName, "out.gif"); FILE *pGIFFile = NULL; pGIFFile = fopen(pcGIFName, "wb"); if (pGIFFile) { gdImageGif(pOutImg, pGIFFile); fclose(pGIFFile); } } else { gdImageInterlace(pOutImg, 1); #ifdef WIN32 _setmode(_fileno(stdout), _O_BINARY); #endif printf("Content-type: image/gif\n\n"); gdImageGif(pOutImg, stdout); } // Destroy the image gdImageDestroy(pOutImg); return true; } // CreateImage
bool wfg_generateImage(char* audioFileName, char* pictureFileName, WFGO* options) { int width = options->width; int height = options->height; int seconds; long framesPerLine; // Initial audio part SF_INFO sfinfo; memset(&sfinfo, 0, sizeof(sfinfo)); SNDFILE *sfile; if(audioFileName != NULL) { sfile = sf_open(audioFileName, SFM_READ, &sfinfo); } else { sfile = sf_open_fd(STDIN_FILENO, SFM_READ, &sfinfo, 0); } if(sfile == NULL) { lastErrorMessage = "Could not open input file!"; return false; } if(audioFileName == NULL) { seconds = options->trackLength; framesPerLine = ((long) seconds * sfinfo.samplerate) / width; } else { seconds = sfinfo.frames / sfinfo.samplerate; framesPerLine = (long) sfinfo.frames / width; } long samplesPerLine = framesPerLine * sfinfo.channels; // although one could think, that these values are flexible, the loops // below only work in these configurations (1/all or all/1) int channelsPerDrawing = 1; int drawnChannels = sfinfo.channels; if(options->mixChannels) { channelsPerDrawing = sfinfo.channels; drawnChannels = 1; } float * buffer = malloc(sizeof(float) * samplesPerLine); // malloc fail if(buffer == NULL) { lastErrorMessage = "Could not allocate memory!"; sf_close(sfile); return false; } // Allocate Image gdImagePtr im = gdImageCreate(width,height); if(im == NULL) { lastErrorMessage = "Could not allocate image!"; free(buffer); sf_close(sfile); return false; } // calculate how large one drawing should be int drawHeight = height; // leave space for the timeline if(options->drawTimeline) drawHeight -= 13; // subtract spacing drawHeight = drawHeight - ((drawnChannels - 1) * options->channelSpacing); // divide by drawnChannels drawHeight = drawHeight / drawnChannels; // background color int bgColor = gdImageColorAllocate(im,WFG_UNPACK_RGB(options->bgColor)); if(options->transparentBg) { gdImageColorTransparent(im,bgColor); } int rmsColor = gdImageColorAllocate(im, WFG_UNPACK_RGB(options->rmsColor)); int peakColor = gdImageColorAllocate(im, WFG_UNPACK_RGB(options->peakColor)); // too many nested loops ... for(int i = 0; i < width; i++) { if(sf_read_float(sfile, buffer, samplesPerLine) != samplesPerLine) { if(audioFileName == NULL) { memset(buffer, 0, samplesPerLine); } else { lastErrorMessage = "Could not read samples from audio file!"; sf_close(sfile); free(buffer); gdImageDestroy(im); return false; } } int drawOffset = 0; for(int d = 0; d < drawnChannels; d++) { double val = 0.0; float peakP = 0.0; float peakM = 0.0; for(long e = 0; e < framesPerLine; e++) { for(int f = 0; f < channelsPerDrawing; f++) { float smpl = buffer[e * drawnChannels + d]; val = val + (smpl*smpl); if(peakM > smpl) peakM = smpl; if(peakP < smpl) peakP = smpl; } } val = val / (float) (framesPerLine * channelsPerDrawing); val = sqrt(val); double ddrawHeight = drawHeight; int peakPP = drawHeight/2 - round(peakP * (ddrawHeight/2.0)); int peakMP = drawHeight/2 + round(peakM * -1.0 * (ddrawHeight/2.0)); // avoid rounding errors when peak is very small // if(peakP > 0.001 || peakM < -0.001) gdImageLine(im, i,peakPP + drawOffset,i,peakMP + drawOffset, peakColor); int rmsSize; rmsSize = val * (double) (drawHeight/2); gdImageLine(im, i,drawHeight/2 - rmsSize + drawOffset,i,drawHeight/2 + rmsSize + drawOffset, rmsColor); drawOffset += drawHeight + options->channelSpacing; } } sf_close(sfile); free(buffer); if(options->drawTimeline) drawTimeline(im, options, seconds); // write out file FILE* file; if(pictureFileName) { file = fopen(pictureFileName,"wb"); } else { file = stdout; } if(file == NULL) { lastErrorMessage = "Could not open output file!"; gdImageDestroy(im); } gdImagePng(im,file); fclose(file); gdImageDestroy(im); return true; }
int main (void) { #ifdef HAVE_LIBPNG /* Input and output files */ FILE *in; FILE *out; /* Input and output images */ gdImagePtr im_in = 0, im_out = 0; /* Brush image */ gdImagePtr brush; /* Color indexes */ int white; int blue; int red; int green; /* Points for polygon */ gdPoint points[3]; int i; /* Create output image, in true color. */ im_out = gdImageCreateTrueColor (256 + 384, 384); /* 2.0.2: first color allocated would automatically be background in a palette based image. Since this is a truecolor image, with an automatic background of black, we must fill it explicitly. */ white = gdImageColorAllocate (im_out, 255, 255, 255); gdImageFilledRectangle (im_out, 0, 0, gdImageSX (im_out), gdImageSY (im_out), white); /* Set transparent color. */ gdImageColorTransparent (im_out, white); /* Try to load demoin.png and paste part of it into the output image. */ in = fopen ("demoin.png", "rb"); if (!in) { fprintf (stderr, "Can't load source image; this demo\n"); fprintf (stderr, "is much more impressive if demoin.png\n"); fprintf (stderr, "is available.\n"); im_in = 0; } else { int a; im_in = gdImageCreateFromPng (in); fclose (in); /* Now copy, and magnify as we do so */ gdImageCopyResampled (im_out, im_in, 32, 32, 0, 0, 192, 192, 255, 255); /* Now display variously rotated space shuttles in a circle of our own */ for (a = 0; (a < 360); a += 45) { int cx = cos (a * .0174532925) * 128; int cy = -sin (a * .0174532925) * 128; gdImageCopyRotated (im_out, im_in, 256 + 192 + cx, 192 + cy, 0, 0, gdImageSX (im_in), gdImageSY (im_in), a); } } red = gdImageColorAllocate (im_out, 255, 0, 0); green = gdImageColorAllocate (im_out, 0, 255, 0); blue = gdImageColorAllocate (im_out, 0, 0, 255); /* Fat Rectangle */ gdImageSetThickness (im_out, 4); gdImageLine (im_out, 16, 16, 240, 16, green); gdImageLine (im_out, 240, 16, 240, 240, green); gdImageLine (im_out, 240, 240, 16, 240, green); gdImageLine (im_out, 16, 240, 16, 16, green); gdImageSetThickness (im_out, 1); /* Circle */ gdImageArc (im_out, 128, 128, 60, 20, 0, 720, blue); /* Arc */ gdImageArc (im_out, 128, 128, 40, 40, 90, 270, blue); /* Flood fill: doesn't do much on a continuously variable tone jpeg original. */ gdImageFill (im_out, 8, 8, blue); /* Polygon */ points[0].x = 64; points[0].y = 0; points[1].x = 0; points[1].y = 128; points[2].x = 128; points[2].y = 128; gdImageFilledPolygon (im_out, points, 3, green); /* 2.0.12: Antialiased Polygon */ gdImageSetAntiAliased (im_out, green); for (i = 0; (i < 3); i++) { points[i].x += 128; } gdImageFilledPolygon (im_out, points, 3, gdAntiAliased); /* Brush. A fairly wild example also involving a line style! */ if (im_in) { int style[8]; brush = gdImageCreateTrueColor (16, 16); gdImageCopyResized (brush, im_in, 0, 0, 0, 0, gdImageSX (brush), gdImageSY (brush), gdImageSX (im_in), gdImageSY (im_in)); gdImageSetBrush (im_out, brush); /* With a style, so they won't overprint each other. Normally, they would, yielding a fat-brush effect. */ style[0] = 0; style[1] = 0; style[2] = 0; style[3] = 0; style[4] = 0; style[5] = 0; style[6] = 0; style[7] = 1; gdImageSetStyle (im_out, style, 8); /* Draw the styled, brushed line */ gdImageLine (im_out, 0, 255, 255, 0, gdStyledBrushed); } /* Text (non-truetype; see gdtestft for a freetype demo) */ gdImageString (im_out, gdFontGiant, 32, 32, (unsigned char *) "hi", red); gdImageStringUp (im_out, gdFontSmall, 64, 64, (unsigned char *) "hi", red); /* Random antialiased lines; coordinates all over the image, but the output will respect a small clipping rectangle */ gdImageSetClip(im_out, 0, gdImageSY(im_out) - 100, 100, gdImageSY(im_out)); /* Fixed seed for reproducibility of results */ srand(100); for (i = 0; (i < 100); i++) { int x1 = rand() % gdImageSX(im_out); int y1 = rand() % gdImageSY(im_out); int x2 = rand() % gdImageSX(im_out); int y2 = rand() % gdImageSY(im_out); gdImageSetAntiAliased(im_out, white); gdImageLine (im_out, x1, y1, x2, y2, gdAntiAliased); } /* Make output image interlaced (progressive, in the case of JPEG) */ gdImageInterlace (im_out, 1); out = fopen ("demoout.png", "wb"); /* Write PNG */ gdImagePng (im_out, out); fclose (out); /* 2.0.12: also write a paletteized version */ out = fopen ("demooutp.png", "wb"); gdImageTrueColorToPalette (im_out, 0, 256); gdImagePng (im_out, out); fclose (out); gdImageDestroy (im_out); if (im_in) { gdImageDestroy (im_in); } #else fprintf (stderr, "No PNG library support.\n"); #endif /* HAVE_LIBPNG */ return 0; }