static void optimize_image(MagickWand *mw, const char *src, const char *dst, size_t size_in, unsigned char *blob_in, const struct imgmin_options *opt) { MagickWand *tmp; size_t size_out = size_in + 1; report_before(mw, size_in); #ifdef IMGMIN_STANDALONE /* * NOTE: for now only allow use of external PNG tools in a standalone cmdline app, * not the built-in apache -- it's slower and less trustworthy. */ if (strcmp("-", src) && !strcmp("PNG", MagickGetImageFormat(mw))) { do_png(mw, src, dst, opt); return; } else { #endif tmp = search_quality(mw, dst, opt); #ifdef IMGMIN_STANDALONE } #endif size_out = blob_write(blob_in, size_in, tmp, dst); report_after(tmp, size_in, size_out); DestroyMagickWand(tmp); }
static void doit(const char *src, const char *dst, size_t oldsize, const struct imgmin_options *opt) { MagickWand *mw, *tmp; MagickBooleanType status; double ks; size_t newsize = oldsize + 1; MagickWandGenesis(); mw = NewMagickWand(); /* load image... */ if (0 == strcmp("-", src)) { /* ...from stdin */ # define BIGBUF (16 * 1024 * 1024) char *blob = malloc(BIGBUF); oldsize = read(STDIN_FILENO, blob, BIGBUF); if (BIGBUF == oldsize) { fprintf(stderr, "Image too large for hardcoded imgmin stdin buffer\n"); exit(1); } MagickReadImageBlob(mw, blob, oldsize); free(blob); } else { /* ...from disk */ status = MagickReadImage(mw, src); if (status == MagickFalse) ThrowWandException(mw); } ks = oldsize / 1024.; fprintf(stderr, "Before quality:%lu colors:%lu size:%5.1fkB type:%s ", quality(mw), (unsigned long)unique_colors(mw), ks, type2str(MagickGetImageType(mw))); tmp = search_quality(mw, dst, opt); /* output image... */ { unsigned char *blob = MagickGetImageBlob(tmp, &newsize); /* if resulting image is larger than original, use original instead */ if (newsize > oldsize) { (void) MagickRelinquishMemory(blob); blob = MagickGetImageBlob(mw, &newsize); } { int fd; if (0 == strcmp("-", dst)) { fd = STDOUT_FILENO; } else { fd = open(dst, O_WRONLY | O_CREAT, 0644); if (-1 == fd) { perror("open"); exit(1); } } if ((ssize_t)newsize != write(fd, blob, newsize)) { perror("write"); exit(1); } (void) MagickRelinquishMemory(blob); if (fd != STDOUT_FILENO) close(fd); } } { double kd = newsize / 1024.; double ksave = ks - kd; double kpct = ksave * 100. / ks; fprintf(stderr, "After quality:%lu colors:%lu size:%5.1fkB saved:%5.1fkB (%.1f%%)\n", (unsigned long)quality(tmp), (unsigned long)unique_colors(tmp), kd, ksave, kpct); } /* tear it down */ DestroyMagickWand(tmp); DestroyMagickWand(mw); MagickWandTerminus(); }