/* {{{ zBarcodeImage zBarcodeImage:clear() Remove existing pages */ PHP_METHOD(zbarcodeimage, clear) { php_zbarcode_image_object *intern; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") == FAILURE) { return; } intern = (php_zbarcode_image_object *)zend_object_store_get_object(getThis() TSRMLS_CC); ClearMagickWand(intern->magick_wand); PHP_ZBARCODE_CHAIN_METHOD; }
/** * read frame using ImageMagick */ NftResult im_read_frame(struct Ledcat *c, size_t width, size_t height, char *buf) { #if HAVE_IMAGEMAGICK == 1 /* is there an image from a previous read? */ if(MagickHasNextImage(c->mw)) { MagickNextImage(c->mw); } else { /* end-of-stream? */ if(feof(c->file)) return FALSE; /* read file */ if(!MagickReadImageFile(c->mw, c->file)) { im_error(c->mw); return FALSE; } /* reset iterator in case we read more than one file */ //MagickResetIterator(c->mw); } /* turn possible alpha-channel black */ /*PixelWand *pw; if(!(pw = NewPixelWand())) return FALSE; PixelSetColor(pw, "black"); MagickSetImageBackgroundColor(c->mw, pw); DestroyPixelWand(pw);*/ /* get raw-buffer from imagemagick */ if(!(MagickExportImagePixels(c->mw, 0, 0, width, height, c->map, c->storage, buf))) { im_error(c->mw); return FALSE; } /* free resources */ if(!MagickHasNextImage(c->mw)) ClearMagickWand(c->mw); #endif return TRUE; }
bool img_load_screen (img_t *im, const char *path) { ClearMagickWand ((MagickWand *) im->screen); if (MagickReadImage ((MagickWand *) im->screen, path) == MagickFalse) { img_exception ((MagickWand *) im->screen); return false; } return true; }
bool img_load_banner (img_t *im, const uint8_t *data, const size_t size) { ClearMagickWand ((MagickWand *) im->banner); if (MagickReadImageBlob ((MagickWand *) im->banner, (const void *) data, size) == MagickFalse) { img_exception ((MagickWand *) im->banner); return false; } return true; }
static zend_bool _php_zbarcode_read(MagickWand *wand, char *filename, long enhance) { if (enhance & PHP_ZBARCODE_RESOLUTION) { MagickSetResolution(wand, 200, 200); } if (MagickReadImage(wand, filename) == MagickFalse) { ClearMagickWand(wand); return 0; } if (enhance & PHP_ZBARCODE_ENHANCE) { MagickEnhanceImage(wand); } if (enhance & PHP_ZBARCODE_SHARPEN) { MagickSharpenImage(wand, 0, 0.5); } return 1; }
int parseImage(void) { int x,y; MagickBooleanType status; MagickWand *magick_wand; //PixelWand *color; MagickPixelPacket pixel; PixelWand **pixels; MagickWandGenesis(); magick_wand=NewMagickWand(); status=MagickReadImage(magick_wand,"matti.png"); if (status == MagickFalse) printf("ERROR\n"); size_t width=MagickGetImageWidth(magick_wand); size_t height=MagickGetImageHeight(magick_wand); printf("X:%d, Y:%d\n",width, height); int image_width = MAX_WIDTH; int image_height = MAX_HEIGHT; if(width < MAX_WIDTH) image_width=width; if(height < MAX_HEIGHT) image_height=height; // PixelIterator* iterator = NewPixelIterator(magick_wand); //PixelWand **pixels = PixelGetNextIteratorRow(iterator,&number_wands); for (y=0; y < image_height; y++) // for (y=0; y < 1; y++) { pixels=PixelGetNextIteratorRow(iterator,&width); for (x=0; x < image_width; x++) { PixelGetMagickColor(pixels[x],&pixel); //printf("R/G/B %g/%g/%g\n",round(pixel.red),round(pixel.green),round(pixel.blue)); int real_y= image_height-y-1; if (real_y < 0) { real_y = 0; printf("ERRRORRR\n"); } else { /*if (round(pixel.red)==0&&round(pixel.green)==0&&round(pixel.blue)==0) image_in[x][real_y]=255; if (round(pixel.red)<4095) image_in[x][real_y]=255;*/ image_in[x][real_y] = 65535 - round(pixel.red); } } PixelSyncIterator(iterator); } PixelSyncIterator(iterator); iterator=DestroyPixelIterator(iterator); ClearMagickWand(magick_wand); printf("Parsed bitmap\n"); magick_wand=DestroyMagickWand(magick_wand); MagickWandTerminus(); }
int main(int argc, char *argv[]) { MagickWand *wand, *input, *output; MagickBooleanType status; printf("Add 3 sets of images after setting 'first' on empty wand\n"); printf("Result shoud be: 678 345 012\n"); MagickWandGenesis(); wand = NewMagickWand(); input = NewMagickWand(); MagickSetFirstIterator(wand); status = MagickReadImage(input, "font_0.gif" ) && MagickReadImage(input, "font_1.gif" ) && MagickReadImage(input, "font_2.gif" ); if (status == MagickFalse) ThrowWandException(input); status = MagickAddImage(wand, input); if (status == MagickFalse) ThrowWandException(wand); ClearMagickWand(input); status = MagickReadImage(input, "font_3.gif" ) && MagickReadImage(input, "font_4.gif" ) && MagickReadImage(input, "font_5.gif" ); if (status == MagickFalse) ThrowWandException(input); status = MagickAddImage(wand, input); if (status == MagickFalse) ThrowWandException(wand); ClearMagickWand(input); status = MagickReadImage(input, "font_6.gif" ) && MagickReadImage(input, "font_7.gif" ) && MagickReadImage(input, "font_8.gif" ); if (status == MagickFalse) ThrowWandException(input); status = MagickAddImage(wand, input); if (status == MagickFalse) ThrowWandException(wand); input=DestroyMagickWand(input); /* finished */ /* append all images together to create the output wand */ MagickResetIterator(wand); /* append all images */ output = MagickAppendImages(wand,MagickFalse); wand = DestroyMagickWand(wand); /* finished - could swap here */ /* Final output */ status = MagickWriteImage(output,"show:"); if (status == MagickFalse) ThrowWandException(output); output = DestroyMagickWand(output); MagickWandTerminus(); }
ngx_int_t ngx_http_small_light_imagemagick_process(ngx_http_request_t *r, ngx_http_small_light_ctx_t *ctx) { ngx_http_small_light_imagemagick_ctx_t *ictx; ngx_http_small_light_image_size_t sz; MagickBooleanType status; int rmprof_flg, progressive_flg; double iw, ih, q; char *jpeg_size_opt, *of_orig, *crop_geo, *size_geo; char *unsharp, *sharpen, *blur, *dealpha, *of; MagickWand *trans_wand, *canvas_wand; DrawingWand *border_wand; PixelWand *bg_color, *canvas_color, *border_color; GeometryInfo geo; ngx_fd_t fd; MagickWand *icon_wand; u_char *p, *embedicon, *embedicon_path; size_t embedicon_path_len, embedicon_len, sled_image_size; ngx_int_t type; status = MagickFalse; ictx = (ngx_http_small_light_imagemagick_ctx_t *)ctx->ictx; /* adjust image size */ ngx_http_small_light_calc_image_size(r, ctx, &sz, 10000.0, 10000.0); /* init */ ictx->wand = NewMagickWand(); /* prepare */ if (sz.jpeghint_flg != 0) { jpeg_size_opt = ngx_pcalloc(r->pool, 32 + 1); if (jpeg_size_opt == NULL) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "failed to allocate memory from r->pool %s:%d", __FUNCTION__, __LINE__); return NGX_ERROR; } ngx_snprintf((u_char *)jpeg_size_opt, 32 + 1, "%dx%d", (ngx_int_t)sz.dw, (ngx_int_t)sz.dh); MagickSetOption(ictx->wand, "jpeg:size", jpeg_size_opt); } /* load image. */ status = MagickReadImageBlob(ictx->wand, (void *)ictx->image, ictx->image_len); if (status == MagickFalse) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "couldn't read image %s:%d", __FUNCTION__, __LINE__); return NGX_ERROR; } /* remove all profiles */ rmprof_flg = ngx_http_small_light_parse_flag(NGX_HTTP_SMALL_LIGHT_PARAM_GET_LIT(&ctx->hash, "rmprof")); if (rmprof_flg != 0) { status = MagickProfileImage(ictx->wand, "*", NULL, 0); if (status == MagickFalse) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "couldn't profiling image %s:%d", __FUNCTION__, __LINE__); } } /* calc size. */ iw = (double)MagickGetImageWidth(ictx->wand); ih = (double)MagickGetImageHeight(ictx->wand); ngx_http_small_light_calc_image_size(r, ctx, &sz, iw, ih); /* pass through. */ if (sz.pt_flg != 0) { return NGX_OK; } of_orig = MagickGetImageFormat(ictx->wand); /* crop, scale. */ status = MagickTrue; if (sz.scale_flg != 0) { crop_geo = ngx_pcalloc(r->pool, 128 + 1); if (crop_geo == NULL) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "failed to allocate memory from r->pool %s:%d", __FUNCTION__, __LINE__); return NGX_ERROR; } size_geo = ngx_pcalloc(r->pool, 128 + 1); if (size_geo == NULL) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "failed to allocate memory from r->pool %s:%d", __FUNCTION__, __LINE__); return NGX_ERROR; } ngx_snprintf((u_char *)crop_geo, 128 + 1, "%f!x%f!+%f+%f", sz.sw, sz.sh, sz.sx, sz.sy); ngx_snprintf((u_char *)size_geo, 128 + 1, "%f!x%f!", sz.dw, sz.dh); trans_wand = MagickTransformImage(ictx->wand, crop_geo, size_geo); if (trans_wand == NULL || trans_wand == ictx->wand) { r->err_status = NGX_HTTP_INTERNAL_SERVER_ERROR; return NGX_ERROR; } DestroyMagickWand(ictx->wand); ictx->wand = trans_wand; } /* rotate */ if (sz.angle) { bg_color = NewPixelWand(); PixelSetRed(bg_color, sz.cc.r / 255.0); PixelSetGreen(bg_color, sz.cc.g / 255.0); PixelSetBlue(bg_color, sz.cc.b / 255.0); PixelSetAlpha(bg_color, sz.cc.a / 255.0); switch (sz.angle) { case 90: case 180: case 270: MagickRotateImage(ictx->wand, bg_color, sz.angle); break; } DestroyPixelWand(bg_color); } /* create canvas then draw image to the canvas. */ if (sz.cw > 0.0 && sz.ch > 0.0) { canvas_wand = NewMagickWand(); canvas_color = NewPixelWand(); PixelSetRed(canvas_color, sz.cc.r / 255.0); PixelSetGreen(canvas_color, sz.cc.g / 255.0); PixelSetBlue(canvas_color, sz.cc.b / 255.0); PixelSetAlpha(canvas_color, sz.cc.a / 255.0); status = MagickNewImage(canvas_wand, sz.cw, sz.ch, canvas_color); DestroyPixelWand(canvas_color); if (status == MagickFalse) { r->err_status = NGX_HTTP_INTERNAL_SERVER_ERROR; return NGX_ERROR; } status = MagickCompositeImage(canvas_wand, ictx->wand, AtopCompositeOp, sz.dx, sz.dy); if (status == MagickFalse) { r->err_status = NGX_HTTP_INTERNAL_SERVER_ERROR; return NGX_ERROR; } DestroyMagickWand(ictx->wand); ictx->wand = canvas_wand; } /* effects. */ unsharp = NGX_HTTP_SMALL_LIGHT_PARAM_GET_LIT(&ctx->hash, "unsharp"); if (unsharp != NULL) { ParseGeometry(unsharp, &geo); status = MagickUnsharpMaskImage(ictx->wand, geo.rho, geo.sigma, geo.xi, geo.psi); if (status == MagickFalse) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "unsharp failed %s:%d", __FUNCTION__, __LINE__); } } sharpen = NGX_HTTP_SMALL_LIGHT_PARAM_GET_LIT(&ctx->hash, "sharpen"); if (sharpen != NULL) { ParseGeometry(sharpen, &geo); status = MagickSharpenImage(ictx->wand, geo.rho, geo.sigma); if (status == MagickFalse) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "sharpen failed %s:%d", __FUNCTION__, __LINE__); } } blur = NGX_HTTP_SMALL_LIGHT_PARAM_GET_LIT(&ctx->hash, "blur"); if (blur) { ParseGeometry(blur, &geo); status = MagickBlurImage(ictx->wand, geo.rho, geo.sigma); if (status == MagickFalse) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "blur failed %s:%d", __FUNCTION__, __LINE__); } } dealpha = NGX_HTTP_SMALL_LIGHT_PARAM_GET_LIT(&ctx->hash, "dealpha"); if (dealpha != NULL) { status = MagickSetImageAlphaChannel(ictx->wand, DeactivateAlphaChannel); if (status == MagickFalse) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "dealpha failed %s:%d", __FUNCTION__, __LINE__); } } /* border. */ if (sz.bw > 0.0 || sz.bh > 0.0) { border_wand = NewDrawingWand(); border_color = NewPixelWand(); PixelSetRed(border_color, sz.bc.r / 255.0); PixelSetGreen(border_color, sz.bc.g / 255.0); PixelSetBlue(border_color, sz.bc.b / 255.0); PixelSetAlpha(border_color, sz.bc.a / 255.0); DrawSetFillColor(border_wand, border_color); DrawSetStrokeColor(border_wand, border_color); DrawSetStrokeWidth(border_wand, 1); if (sz.cw > 0.0 && sz.ch > 0.0) { DrawRectangle(border_wand, 0, 0, sz.cw - 1, sz.bh - 1); DrawRectangle(border_wand, 0, 0, sz.bw - 1, sz.ch - 1); DrawRectangle(border_wand, 0, sz.ch - sz.bh, sz.cw - 1, sz.ch - 1); DrawRectangle(border_wand, sz.cw - sz.bw, 0, sz.cw - 1, sz.ch - 1); } else { DrawRectangle(border_wand, 0, 0, sz.dw - 1, sz.bh - 1); DrawRectangle(border_wand, 0, 0, sz.bw - 1, sz.dh - 1); DrawRectangle(border_wand, 0, sz.dh - sz.bh, sz.dw - 1, sz.dh - 1); DrawRectangle(border_wand, sz.dw - sz.bw, 0, sz.dw - 1, sz.dh - 1); } MagickDrawImage(ictx->wand, border_wand); DestroyPixelWand(border_color); DestroyDrawingWand(border_wand); } /* embed icon */ embedicon = NGX_HTTP_SMALL_LIGHT_PARAM_GET_LIT(&ctx->hash, "embedicon"); if (ngx_strlen(ctx->material_dir) > 0 && ngx_strlen(embedicon) > 0) { if (ngx_strstrn((u_char *)embedicon, "/", 1 - 1)) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "invalid parameter 'embedicon':%s %s:%d", embedicon, __FUNCTION__, __LINE__); return NGX_ERROR; } icon_wand = NewMagickWand(); embedicon_len = ngx_strlen(embedicon); embedicon_path_len = ctx->material_dir->len + ngx_strlen("/") + embedicon_len; embedicon_path = ngx_palloc(r->pool, embedicon_path_len + 1); if (embedicon_path == NULL) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "failed to allocate memory from r->pool %s:%d", __FUNCTION__, __LINE__); return NGX_ERROR; } p = embedicon_path; p = ngx_cpystrn(p, ctx->material_dir->data, ctx->material_dir->len + 1); p = ngx_cpystrn(p, (u_char *)"/", 1 + 1); p = ngx_cpystrn(p, embedicon, embedicon_len + 1); if ((fd = ngx_open_file(embedicon_path, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0)) == NGX_INVALID_FILE) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "failed to open embeddedicon file:%s %s:%d", embedicon_path, __FUNCTION__, __LINE__); return NGX_ERROR; } if (ngx_close_file(fd) == NGX_FILE_ERROR) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "failed to close:%s %s:%d", embedicon_path, __FUNCTION__, __LINE__); return NGX_ERROR; } if (ngx_strstrn(embedicon_path, "..", 2 - 1)) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "invalid embeddedicon_path:%s %s:%d", embedicon_path, __FUNCTION__, __LINE__); return NGX_ERROR; } if (MagickReadImage(icon_wand, (char *)embedicon_path) == MagickFalse) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "failed to read embed icon image file:%s %s:%d", embedicon_path, __FUNCTION__, __LINE__); return NGX_ERROR; } MagickCompositeImageChannel(ictx->wand, AllChannels, icon_wand, OverCompositeOp, sz.ix, sz.iy); ClearMagickWand(icon_wand); } /* set params. */ q = ngx_http_small_light_parse_double(NGX_HTTP_SMALL_LIGHT_PARAM_GET_LIT(&ctx->hash, "q")); if (q > 0.0) { MagickSetImageCompressionQuality(ictx->wand, q); } progressive_flg = ngx_http_small_light_parse_flag(NGX_HTTP_SMALL_LIGHT_PARAM_GET_LIT(&ctx->hash, "progressive")); if (progressive_flg != 0) { MagickSetInterlaceScheme(ictx->wand, LineInterlace); } 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__); of = (char *)ngx_http_small_light_image_exts[ictx->type - 1]; } else if (type == NGX_HTTP_SMALL_LIGHT_IMAGE_WEBP) { #if defined(MAGICKCORE_WEBP_DELEGATE) ictx->type = type; #else ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "WebP is not supported %s:%d", __FUNCTION__, __LINE__); of = (char *)ngx_http_small_light_image_exts[ictx->type - 1]; #endif } else { ictx->type = type; } MagickSetFormat(ictx->wand, of); ctx->of = ngx_http_small_light_image_types[ictx->type - 1]; } else { MagickSetFormat(ictx->wand, of_orig); ctx->of = ctx->inf; } ctx->content = MagickGetImageBlob(ictx->wand, &sled_image_size); ctx->content_length = sled_image_size; ictx->complete = 1; return NGX_OK; }