From a601a38f83adc2c0c9d031b2a4c4fceb7ef127dd Mon Sep 17 00:00:00 2001 From: alpine9000 Date: Sat, 12 Mar 2016 10:51:08 +1100 Subject: [PATCH] First pass at resize utility --- doc/BuildingCrossDev.md | 10 ++++ tools/resize/Makefile | 27 ++++++++++ tools/resize/resize.c | 110 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 147 insertions(+) create mode 100644 tools/resize/Makefile create mode 100644 tools/resize/resize.c diff --git a/doc/BuildingCrossDev.md b/doc/BuildingCrossDev.md index ad0655e..8dbcdf7 100644 --- a/doc/BuildingCrossDev.md +++ b/doc/BuildingCrossDev.md @@ -101,3 +101,13 @@ Notes: # cp *.h /usr/local/include/pngquant/ # cp *.a /usr/local/lib ``` + +9. GraphicsMagick + ``` + # wget http://78.108.103.11/MIRROR/ftp/GraphicsMagick/1.3/GraphicsMagick-1.3.23.tar.gz + # tar zxfv GraphicsMagick-1.3.23.tar.gz + # cd GraphicsMagick-1.3.23 + # ./configure --prefix=/usr/local + # make + # make install +``` diff --git a/tools/resize/Makefile b/tools/resize/Makefile new file mode 100644 index 0000000..9f87076 --- /dev/null +++ b/tools/resize/Makefile @@ -0,0 +1,27 @@ +PROGRAM=./out/resize +OBJS=out/resize.o +WARN_ERROR=-Werror +HOST_WARNINGS=$(WARN_ERROR) -pedantic-errors -Wfatal-errors -Wall -Wextra -Wno-unused-parameter -Wshadow -limagequant +HOST_CFLAGS=$(HOST_WARNINGS) -O3 `GraphicsMagick-config --cppflags` +LIBS=`GraphicsMagick-config --ldflags --libs` + +$(PROGRAM): out bin $(OBJS) + gcc $(OBJS) -o $(PROGRAM) $(LIBS) + +-include $(OBJS:.o=.d) + +out/%.o: %.c + gcc -c $(HOST_CFLAGS) $< -o $@ + @gcc -MM $(HOST_CFLAGS) $*.c > out/$*.d + @mv -f out/$*.d out/$*.d.tmp + @sed 's/^.*\:/out\/&/' < out/$*.d.tmp > out/$*.d + @rm -f out/$*.d.tmp + +out: + mkdir out + +bin: + mkdir bin + +clean: + rm -rf out bin *~ \ No newline at end of file diff --git a/tools/resize/resize.c b/tools/resize/resize.c new file mode 100644 index 0000000..f10f7ba --- /dev/null +++ b/tools/resize/resize.c @@ -0,0 +1,110 @@ +#include +#include +#include +#include +#include +#include + +int +main(int argc, char **argv) +{ + Image *image = 0, *resizeImage = 0, *croppedImage = 0; + char infile[MaxTextExtent]; + char outfile[MaxTextExtent]; + int arg = 1, exit_status = 0; + ImageInfo *imageInfo; + ExceptionInfo exception; + + InitializeMagick(NULL); + imageInfo=CloneImageInfo(0); + GetExceptionInfo(&exception); + +#if 0 + const int targetWidth = 320; + const int targetHeight = 256; + const int interlaced = 0; + const float blur = 0.75; +#else + const int targetWidth = 320; + const int targetHeight = 512; + const int interlaced = 1; + const float blur = 0.75; +#endif + + if (argc != 3) { + (void) fprintf ( stderr, "Usage: %s infile outfile\n", argv[0] ); + (void) fflush(stderr); + exit_status = 1; + goto program_exit; + } + + (void) strncpy(infile, argv[arg], MaxTextExtent-1 ); + arg++; + (void) strncpy(outfile, argv[arg], MaxTextExtent-1 ); + + (void) strcpy(imageInfo->filename, infile); + image = ReadImage(imageInfo, &exception); + if (image == (Image *) NULL) { + CatchException(&exception); + exit_status = 1; + goto program_exit; + } + + int width = image->columns; + int height = image->rows; + + float scale = (float)height/(float)targetHeight; + int newWidth = width/scale; + + resizeImage=ResizeImage(image, newWidth/(interlaced?2:1), targetHeight, + //GaussianFilter, + //BoxFilter, + //TriangleFilter, + //HermiteFilter, + //HanningFilter, + //HammingFilter, + //BlackmanFilter, + //GaussianFilter, + //QuadraticFilter, + //CubicFilter, + //CatromFilter, + //MitchellFilter, + //LanczosFilter, + BesselFilter, + //SincFilter, + blur, &exception); + + RectangleInfo rect = { + .x = ((newWidth/(interlaced?2:1))-targetWidth)/2, + .y = 0, + .width = targetWidth, + .height = targetHeight + }; + + croppedImage = CropImage(resizeImage, &rect, &exception); + + (void) strcpy(croppedImage->filename, outfile); + if (!WriteImage (imageInfo, croppedImage)) + { + CatchException(&croppedImage->exception); + exit_status = 1; + goto program_exit; + } + + program_exit: + + if (image != (Image *) NULL) + DestroyImage(image); + + if (resizeImage != (Image *)NULL) + DestroyImage(resizeImage); + + if (croppedImage != (Image *)NULL) + DestroyImage(croppedImage); + + if (imageInfo != (ImageInfo *) NULL) + DestroyImageInfo(imageInfo); + DestroyMagick(); + + return exit_status; +}