[sane-devel] Re: Unpaper 1.0.1 problem

Martin Collins martin@mkcollins.org
Mon, 13 Jun 2005 11:39:34 +0100


This is a multi-part message in MIME format.

--Multipart=_Mon__13_Jun_2005_11_39_34_+0100_EZ09bMYK9KJdqf8o
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

On 13 Jun 2005 06:14:33 +0200
jsbien@mimuw.edu.pl (Janusz S. Bie=F1) wrote:

> BTW, what whould you recommend for splitting spreads into single
> pages? I intend to use `convert' with `crop' option, but for this
> specific job it is cumbersome. The scanning has been done by a
> library, and for almost every page the size of its scan are
> different! Hence I have to compute the crop coordinates separately
> for every page.

ImageMagick has an API you can use for this kind of thing. Attached
is a program I wrote to cut scans in half. It also rotates, scales
down and saves as jpeg so you will need to modify it but that should
just entail removing the sections of code you don't need.

Martin

--Multipart=_Mon__13_Jun_2005_11_39_34_+0100_EZ09bMYK9KJdqf8o
Content-Type: text/x-csrc;
 name="scanfix.c"
Content-Disposition: attachment;
 filename="scanfix.c"
Content-Transfer-Encoding: 7bit

/* scanfix.c
   Uses the ImageMagick API to convert the image of two pages of a magazine
   or book output by a scanner into a separate file for each page.
   It reduces the size by half, rotates by 90 degrees, cuts in half and
   saves each half as a jpeg for colour or as pnm for mono or greyscale.
   FILES ARE SAVED IN THE CURRENT DIRECTORY.

   compile with:
   gcc `Magick-config --cflags --cppflags` scanfix.c `Magick-config --ldflags --libs` -o scanfix
*/

#include <stdio.h>
#include <string.h>
#include <time.h>
#include <libgen.h>
#include <sys/types.h>
#include <magick/api.h>

int main(int argc,char **argv)
{
	ExceptionInfo exception;
	Image *image, *resize_image, *rotate_image, *new_image;
	ImageInfo *image_info;
	RectangleInfo geometry;
	char filename[1024];
	char *sep;

	/* figure out the output filename */
	(void) strcpy(filename, basename(argv[1]));
	sep=strrchr(filename,'.');
	*sep='\0';
	printf("%s\n", filename);

	/* Initialize the image info structure and read an image. */
	InitializeMagick(*argv);
	GetExceptionInfo(&exception);
	image_info=CloneImageInfo((ImageInfo *) NULL);
	(void) strcpy(image_info->filename,argv[1]);
	image=ReadImage(image_info,&exception);
	if (image == (Image *) NULL)
		MagickError(exception.severity,exception.reason,exception.description);
	printf("%ldx%ld\n", image->columns, image->rows);

	/* reduce the image in size by 50%  */
	resize_image=SampleImage(image,image->columns/2,image->rows/2,&exception);
	if (resize_image == (Image *) NULL)
		MagickError(exception.severity,exception.reason,exception.description);
	DestroyImage(image);

	/* rotate the image 90 degrees */
	rotate_image=RotateImage(resize_image, 90, &exception);
	if (rotate_image == (Image *) NULL)
		MagickError(exception.severity,exception.reason,exception.description);
	DestroyImage(resize_image);

	/* Write the left half of the image and destroy it */
	geometry.width=rotate_image->columns/2;
	geometry.height=rotate_image->rows;
	geometry.x=0;
	geometry.y=0;
	new_image=CropImage(rotate_image,&geometry,&exception);
	(void) strcpy(new_image->filename,filename);
	/* determine type of file to write */
	if (IsGrayImage(new_image,&exception))
	{
		(void) strcpy(new_image->magick,"PGM");
		image_info->monochrome=0;
		(void) strcat(new_image->filename,"a.pgm");
	}
	else if (IsMonochromeImage(new_image,&exception))
	{
		(void) strcpy(new_image->magick,"PBM");
		image_info->monochrome=1;
		(void) strcat(new_image->filename,"a.pbm");
	}
	else
	{
		(void) strcpy(new_image->magick,"JPEG");
		image_info->quality=80;
		image_info->monochrome=0;
		(void) strcat(new_image->filename,"a.jpg");
	}
	WriteImage(image_info,new_image);
	DestroyImage(new_image);

	/* Write the right half of the image and destroy it */
	geometry.width=rotate_image->columns/2;
	geometry.height=rotate_image->rows;
	geometry.x=rotate_image->columns/2+1;
	geometry.y=0;
	new_image=CropImage(rotate_image,&geometry,&exception);
	(void) strcpy(new_image->filename,filename);
	/* determine type of file to write */
	if (IsGrayImage(new_image,&exception))
	{
		(void) strcpy(new_image->magick,"PGM");
		image_info->monochrome=0;
		(void) strcat(new_image->filename,"b.pgm");
	}
	else if (IsMonochromeImage(new_image,&exception))
	{
		(void) strcpy(new_image->magick,"PBM");
		image_info->monochrome=1;
		(void) strcat(new_image->filename,"b.pbm");
	}
	else
	{
		(void) strcpy(new_image->magick,"JPEG");
		image_info->quality=80;
		image_info->monochrome=0;
		(void) strcat(new_image->filename,"b.jpg");
	}
	WriteImage(image_info,new_image);
	DestroyImage(new_image);

	DestroyImage(rotate_image);
	DestroyImageInfo(image_info);
	DestroyMagick();
	return(0);
}

--Multipart=_Mon__13_Jun_2005_11_39_34_+0100_EZ09bMYK9KJdqf8o--