示例#1
0
int
main( int argc, char **argv )
{
	GOptionContext *context;
	GError *error = NULL;
	IMAGE *im;
	unsigned char header[IM_SIZEOF_HEADER];

	if( im_init_world( argv[0] ) )
	        error_exit( "%s", _( "unable to start VIPS" ) );
	textdomain( GETTEXT_PACKAGE );
	setlocale( LC_ALL, "" );

	context = g_option_context_new( 
		_( "vipsfile - edit vipsfile header" ) );
	g_option_context_add_main_entries( context, entries, GETTEXT_PACKAGE );
	g_option_context_add_group( context, im_get_option_group() );
	if( !g_option_context_parse( context, &argc, &argv, &error ) ) {
		if( error ) {
			fprintf( stderr, "%s\n", error->message );
		        g_error_free( error );
		}

		exit( -1 );
	}
	if( argc != 2 ) {
		fprintf( stderr, _( "usage: %s [OPTION...] vipsfile\n" ), 
			g_get_prgname() );
		exit( -1 );
	}

	if( !(im = im_init( argv[1] )) ||
		(im->fd = im__open_image_file( im->filename )) == -1 ) 
		error_exit( _( "could not open image %s" ), argv[1] );
	if( read( im->fd, header, IM_SIZEOF_HEADER ) != IM_SIZEOF_HEADER ||
		im__read_header_bytes( im, header ) ) 
		error_exit( _( "could not read VIPS header for %s" ), 
			im->filename );

	if( xsize ) 
		parse_pint( xsize, &im->Xsize );
	if( ysize ) 
		parse_pint( ysize, &im->Ysize );
	if( bands ) 
		parse_pint( bands, &im->Bands );
	if( format ) {
		if( (im->BandFmt = im_char2BandFmt( format )) < 0 )
			error_exit( _( "bad format %s" ), format );
		im->Bbits = im_bits_of_fmt( im->BandFmt );
	}
	if( type ) {
		if( (im->Type = im_char2Type( type )) < 0 )
			error_exit( _( "bad type %s" ), type );
	}
	if( coding ) {
		if( (im->Coding = im_char2Coding( coding )) < 0 )
			error_exit( _( "bad coding %s" ), coding );
	}
	if( xres ) 
		im->Xres = atof( xres );
	if( yres ) 
		im->Yres = atof( yres );
	if( xoffset ) 
		im->Xoffset = atoi( xoffset );
	if( yoffset ) 
		im->Yoffset = atoi( yoffset );

	if( lseek( im->fd, 0, SEEK_SET ) == (off_t) -1 ) 
		error_exit( _( "could not seek on %s" ), im->filename );
	if( im__write_header_bytes( im, header ) ||
		im__write( im->fd, header, IM_SIZEOF_HEADER ) )
		error_exit( _( "could not write to %s" ), im->filename );

	if( setext ) {
		char *xml;
		unsigned int size;

		if( !(xml = im__file_read( stdin, "stdin", &size )) )
			error_exit( "%s", _( "could not get ext data" ) );

		/* Strip trailing whitespace ... we can get stray \n at the 
		 * end, eg. "echo | edvips --setext fred.v".
		 */
		while( size > 0 && isspace( xml[size - 1] ) )
			size -= 1;

		if( im__write_extension_block( im, xml, size ) )
			error_exit( "%s", _( "could not set extension" ) );
		im_free( xml );
	}

	im_close( im );

	vips_shutdown();

	return( 0 );
}
示例#2
0
/* Start here!
 */
int
main( int argc, char **argv )
{	
	IMAGE *im = NULL;
	FILE *out = stdout;
	int width = -1;
	int height = -1;
	int dpi = -1;
	int max = 0;
	int rotate = 0;
	int one2one = 0;
	PrinterGeometry *geo = find_printer( "2500cp" );
	char *mode;
	int i;

	if( im_init_world( argv[0] ) )
	        error_exit( "unable to start VIPS" );

	argv0 = argv[0];

	if( argc <= 1 ) {
		printf( 
"usage:\n"
"\t%s [options] <image file>\n"
"convert RGB, LAB, CMYK and mono image files to postscript\n"
"\tRGB converted to LAB, assuming sRGB\n"
"\tLAB printed with printer colour management\n"
"\tCMYK sent directly as dot percent\n"
"\tmono prints as K only\n"
"options include:\n"
"\t-printer <name>\tformat for printer <name>\n"
"\t-3500cp\t\tfor HP 3500CP printer (default 2500cp)\n"
"\t-max\t\tprint as large as possible\n"
"\t-rotate\t\trotate, if necessary, to fill the page\n"
"\t-1:1\t\tsize the image to print at 1:1 ... resolution in\n"
"\t\t\timage header must be set for this\n"
"\t-width <n>\tforce specified width, in points\n"
"\t-height <n>\tforce specified height, in points\n"
"\t-dpi <n>\tforce specified resolution (default 150dpi)\n"
"\t-a5, -a4, -a3, -a2, -a1, -a0\n"
"\t\t\tforce specified height (width ignored)\n"
"\t-o <file>\toutput to file (default stdout)\n",
			argv0 );
		printf( "supported printers:\n" );
		print_printers();
		return( 1 );
	}

	/* Decode args .. just look for file names and our three options.
	 */
	for( i = 1; i < argc; i++ )
		if( *argv[i] == '-' ) {
			if( strcmp( argv[i]+1, "width" ) == 0 ) {
				if( !argv[i+1] || sscanf( argv[i+1], 
					"%d", &width ) != 1 || width <= 10 )
					error_exit( "bad width" );
				i++;
			}
			else if( strcmp( argv[i]+1, "height" ) == 0 ) {
				if( !argv[i+1] || sscanf( argv[i+1], 
					"%d", &height ) != 1 || height <= 10 )
					error_exit( "bad height" );
				i++;
			}
			else if( strcmp( argv[i]+1, "3500cp" ) == 0 ) {
				geo = find_printer( "3500cp" );
			}
			else if( strcmp( argv[i]+1, "printer" ) == 0 ) {
				if( !argv[i+1] || 
					!(geo = find_printer( argv[i+1] )) )
					error_exit( "bad printer model" );
				i++;
			}
			else if( strcmp( argv[i]+1, "dpi" ) == 0 ) {
				if( !argv[i+1] || sscanf( argv[i+1], 
					"%d", &dpi ) != 1 || dpi <= 1 ||
					dpi >= 600 )
					error_exit( "bad dpi" );
				i++;
			}
			else if( strcmp( argv[i]+1, "o" ) == 0 ) {
				if( !argv[i+1] || !(out = fopen( 
					argv[i+1], "w" )) )
					error_exit( "bad output name" );
				i++;
			}
			else if( strcmp( argv[i]+1, "1:1" ) == 0 ) 
				one2one = 1;	
			else if( strcmp( argv[i]+1, "a5" ) == 0 ) 
				height = 595;
			else if( strcmp( argv[i]+1, "a4" ) == 0 ) 
				height = 839;
			else if( strcmp( argv[i]+1, "a3" ) == 0 ) 
				height = 1187;
			else if( strcmp( argv[i]+1, "a2" ) == 0 ) 
				height = 1678;
			else if( strcmp( argv[i]+1, "a1" ) == 0 ) 
				height = 2373;
			else if( strcmp( argv[i]+1, "a0" ) == 0 ) 
				height = 3356;
			else if( strcmp( argv[i]+1, "max" ) == 0 ) 
				max = 1;
			else if( strcmp( argv[i]+1, "rotate" ) == 0 ) 
				rotate = 1;
			else
				error_exit( "bad flag" );
		}
		else {
			/* Try to open the file. 
			 */
			if( im != NULL || !(im = im_open( argv[i], "r" )) )
				error_exit( "bad input image" );
		}

	if( im == NULL ) 
		error_exit( "no input image" );

	/* Turn 3-band uchar images into LABQ. Yuk! But convenient.
	 */
	if( im->Coding == IM_CODING_NONE &&
		im->Bands == 3 && im->BandFmt == IM_BANDFMT_UCHAR ) {
		IMAGE *t[3];

		if( im_open_local_array( im, t, 3, "vips2dj", "p" ) ||
			im_sRGB2XYZ( im, t[0] ) ||
			im_XYZ2Lab( t[0], t[1] ) ||
			im_Lab2LabQ( t[1], t[2] ) )
			error_exit( "error converting to LAB" );

		im = t[2];
	}

	/* Stop used-before-set complaints on mode.
	 */
	mode = "lab";

	/* Pick a PS mode.
	 */
	if( im->Coding == IM_CODING_LABQ )
		mode = "lab";
	else if( im->Coding == IM_CODING_NONE && 
		im->Bands == 4 && im->BandFmt == IM_BANDFMT_UCHAR )
		mode = "cmyk";
	else if( im->Coding == IM_CODING_NONE && 
		im->Bands == 1 && im->BandFmt == IM_BANDFMT_UCHAR )
		mode = "mono";
	else 
		error_exit( "unsupported image type "
			"(IM_CODING_LABQ, mono, IM_TYPE_CMYK only)" );

	/* Autorotate image to fill the page. We ought to get PS to do the
	 * rotate, really.
	 */
	if( rotate ) {
		float iaspect = (float) im->Xsize / im->Ysize;
		float paspect = (float) geo->width / geo->length;

		if( iaspect > paspect ) {
			IMAGE *t[1];

			if( im_open_local_array( im, t, 1, "vips2dj", "p" ) ||
				im_rot90( im, t[0] ) )
				error_exit( "error rotating" );

			im = t[0];
		}
	}

	/* Make sure width and height are both set.
	 */
	if( one2one ) {
		/* Set width/height from res.
		 */
		if( im->Xres <= 0 || im->Xres >= 100 ||
			im->Yres <= 0 || im->Yres >= 100 )
			error_exit( "uanble to print 1:1 - resolution not "
				"set in image" );

		height = (((im->Ysize / im->Yres) / 10.0) / 2.54) * 72.0;
		width = (((im->Xsize / im->Xres) / 10.0) / 2.54) * 72.0;
	}
	else if( max ) {
		float iaspect = (float) im->Xsize / im->Ysize;
		float paspect = (float) geo->width / geo->length;

		if( iaspect > paspect ) 
			/* Image aspect ratio > paper ... fit width.
			 */
			width = geo->width;
		else
			height = geo->length;
	}
	else if( dpi > 0 ) {
		/* Given res ... set width/height.
		 */
		height = (im->Ysize / (float) dpi) * 72.0;
		width = (im->Xsize / (float) dpi) * 72.0;
	}

	if( width >= 0 || height >= 0 ) {
		/* Given width or height or both --- set other one.
		 */
		if( height < 0 ) {
			float fdpi = im->Xsize / (width / 72.0);
			height = (im->Ysize / fdpi) * 72.0;
		}
		else {
			float fdpi = im->Ysize / (height / 72.0);
			width = (im->Xsize / fdpi) * 72.0;
		}
	}
	else {
		/* Nothing set ... default to 150 dpi.
		 */
		height = (im->Ysize / 150.0) * 72.0;
		width = (im->Xsize / 150.0) * 72.0;
	}

	if( send_file( geo, im, mode, out, width, height ) )
		error_exit( "error sending file" );

	return( 0 );
}
示例#3
0
int
main( int argc, char *argv[] )
{
	int enlar = 0;
	int center = 0;
	int rev = 0;

	IMAGE *vips;
	FILE *out;
	int n1, n2;
	int x, y;
	int xsize, ysize;
	int c;
	PEL *p;

	if( im_init_world( argv[0] ) )
	        error_exit( "unable to start VIPS" );

	while( --argc > 0 && (*++argv)[0] == '-' )
		while( (c = *++argv[0]) )
			switch( c ) {
			case 'e':
				enlar = 1;
				break;

			case 'c':
				center = 1;
				break;

			case 'r':
				rev = 1;
				break;

			default:
				error_exit( "mitsub: illegal option %c", c );
			}

	if( argc != 2 )
		error_exit( "usage: mitsub [-ecr] vipsfile mitfile\n"
			"where:\n"
			"\tvipsfile may be 1, 3 or 4 bands for mono, IM_TYPE_RGB or "
			"IM_TYPE_CMYK printing\n" 
			"\tmitfile may be '-', meaning send to stdout\n"
			"\t-e means enlarge to fill page\n"
			"\t-c means centre within page\n"
			"\t-r means reverse black/white\n"
			"\tNOTE: data is sent raw, with 0 == no ink - all correction is up to "
			"you\n"
			"example:\n"
			"\t%% mitsub -ec fred.v - > /dev/bpp0" );

	if( !(vips = im_open( argv[0], "r" )) )
		error_exit( "mitsub: unable to open \"%s\" for input", 
			argv[0] );

	if( strcmp( argv[1], "-" ) == 0 )
		out = stdout;
	else if( !(out = fopen( argv[1], "w" )) )
		error_exit( "mitsub: unable to open \"%s\" for output", 
			argv[1] );

	if( vips->Coding != IM_CODING_NONE || vips->BandFmt != IM_BANDFMT_UCHAR )
		error_exit( "mitsub: uncoded uchar only" );
	if( vips->Bands != 1 && vips->Bands != 3 && vips->Bands != 4 )
		error_exit( "mitsub: 1,3 and 4 band images only" );

	/* Set xsize and ysize.
	 */
	if( vips->Xsize <= vips->Ysize ) {
		xsize = vips->Xsize;
		ysize = vips->Ysize;
	}
	else {
		im_diagnostics( "mitsub: rotating ..." );
		xsize = vips->Ysize;
		ysize = vips->Xsize;
	}

	/* Shrink if image is too big.
	 */
	if( xsize > HMAX || ysize > VMAX ) {
		double x_factor = HMAX/xsize;
		double y_factor = VMAX/ysize;
		double factor = IM_MAX( x_factor, y_factor );
		IMAGE *sh = im_open( "shrink", "t" );

		im_diagnostics( "mitsub: shrinking by %g ...", factor );
		if( !sh || im_shrink( vips, sh, factor, factor ) )
			error_exit( "mitsub: shrink failed" );

		vips = sh;
		enlar = 0;
	}

	/* On line command and buffer clear.
	 */
 	putc( 0x11, out );
	putc( 0x1b, out );
	putc( 'Z', out );

	/* Memory clear.
	 */
	putc( 0x1b, out );
	putc( 'Z', out );

	/* Media size. (Size A4)
	 */
	putc( 0x1b, out );
	putc( '#', out );
	putc( 'P', out );
	putc( '0', out );

	/* Enlargement.
	 */
	if( enlar ) {
		double rh, rv;
		int n, m;

		/* Enlarge method: ('0'=simple enlargement, 
		 * '1'=linear enlargement)
		 */
		putc( 0x1b, out );
		putc( '&', out );
		putc( 'O', out );
		putc( '1', out );	

		rh = HMAX/(double) xsize;
		rv = VMAX/(double) ysize;
		if( rh > 8 || rv > 8 ) {
			n = 8;
			m = 1;
		}
		else if( rh > rv ) {
			double fact = VMAX/255;

			n = 255;
			m = (int) ysize/fact + 1;
		}
		else {
			double fact = HMAX/255;

			n = 255;
			m = (int) xsize/fact + 1;
		}
		im_diagnostics( "mitsub: enlarging by %g ...", (double) n/m );

		/* Horizontal enlarge.
		 */	
		putc( 0x1b, out );
		putc( '&', out );
		putc( 'P', out );
		putc( n, out );
		putc( m, out );

		/* Vertical enlarge.
		 */
		putc( 0x1b, out );
		putc( '&', out );
		putc( 'Q', out );
		putc( n, out );
		putc( m, out );

	}
	else {
		/* No enlargement.
		 */
		putc( 0x1b, out );
		putc( '&', out );
		putc( 'O', out );
		putc( '1', out );	
		putc( 0x1b, out );
		putc( '&', out );
		putc( 'P', out );
		putc( 1, out );
		putc( 1, out );
		putc( 0x1b, out );
		putc( '&', out );
		putc( 'Q', out );
		putc( 1, out );
		putc( 1, out );
	}

	if( rev ) {
		/* Colour reversing.
		 */
		putc( 0x1b, out );
		putc( '&', out );
		putc( 'W', out );
		putc( '2', out  );
	}
	else {
		/* No reverse.
		 */
		putc( 0x1b, out );
		putc( '&', out );
		putc( 'W', out );
		putc( '0', out  );
	}

	/* Number of copies.
	 */
	putc( 0x1b, out );
	putc( '#', out );
	putc( 'C', out );
	putc( NBPRINT, out  );

	/* Left margin.
	 */
	putc( 0x1b, out );
	putc( '&', out );
	putc( 'S', out );
	putc( 0, out  );

	/* Top margin.
	 */
	putc( 0x1b, out );
	putc( '&', out );
	putc( 'T', out );
	putc( 0, out  );

	/* Centering. ('1' = centering available, '0'= no centering).
	 */
	if( center ) {
		im_diagnostics( "mitsub: centering ..." );
		putc( 0x1b, out );
		putc( '&', out );
		putc( 'C', out );
		putc( '1', out );	 
	}
	else {
		/* No centering.
		 */
		putc( 0x1b, out );
		putc( '&', out );
		putc( 'C', out );
		putc( '0', out );
	}

	/* Transfer format = pixel order method for colour, = frame order 
	 * method for monochrome.
	 */	
	switch( vips->Bands ) {
	case 3:
	case 4:
		putc( 0x1b, out );
		putc( '&', out );
		putc( 'A', out );
		putc( '2', out  );
		break;

	case 1:
		putc( 0x1b, out );
		putc( '&', out );
		putc( 'A', out );
		putc( '0', out  );	
		break;

	default:
		error_exit( "internal error" );
		/*NOTREACHED*/
	}

	/* Colour specification.
	 */
	switch( vips->Bands ) {
	case 4:
	case 1:
		/* IM_TYPE_CMYK. For mono, send just K.
		 */
		putc( 0x1b, out );
		putc( '&', out );
		putc( 'I', out );
		putc( '2', out );
		break;

	case 3:
		/* IM_TYPE_RGB.
		 */
		putc( 0x1b, out );
		putc( '&', out );
		putc( 'I', out );
		putc( '0', out );
		break;
	
	default:
		error_exit( "internal error" );
		/*NOTREACHED*/
	}

	/* Gray scale level.
	 */
	putc( 0x1b, out );
	putc( '#', out );
	putc( 'L', out );
	putc( 8, out );

	/* Rotation.
	 */
	if( vips->Xsize <= vips->Ysize ) {
		putc( 0x1b, out );
		putc( '#', out );
		putc( 'R', out );
		putc( '0', out );
	}
	else  {
		putc( 0x1b, out );
		putc( '#', out );
		putc( 'R', out );
		putc( '1', out );
	}
		
	/* Horizontal shift.
	 */ 
	putc( 0x1b, out );
	putc( '&', out );
	putc( 'J', out );
	putc( 0, out );
	putc( 0, out );

	/* Vertical shift.
	 */
	putc( 0x1b, out );
	putc( '&', out );
	putc( 'K', out );
	putc( 0, out );
	putc( 0, out );

	/* Number of horizontal pixels.
	 */
	n1 = vips->Xsize >> 8;
	n2 = vips->Xsize & 0xff;
	putc(  0x1b, out );
	putc( '&', out );
	putc( 'H', out );
	putc( n1, out );
	putc( n2, out );
	
	/* Number of vertical pixels.
	 */
	n1 = vips->Ysize >> 8;
	n2 = vips->Ysize & 0xff;
	putc( 0x1b, out );
	putc( '&', out );
	putc( 'V', out );
	putc( n1, out );
	putc( n2, out );

	/* Transfer colour (for monochrome image only).
	 */
	if( vips->Bands == 1 ) {
		putc( 0x1b, out );
		putc( 'C', out );
		putc( '4', out );
	}

	/* Image data transfer. Image must be sent as YMCK.
	 */
	putc( 0x1b, out );
	putc( 'O', out );
	if( im_incheck( vips ) )
		error_exit( "mitsub: unable to read image data" );
	p = (PEL *) vips->data;
	switch( vips->Bands ) {
	case 4:
		im_diagnostics( "mitsub: sending IM_TYPE_CMYK ..." );
		for( y = 0; y < vips->Ysize; y++ )
			for( x = 0; x < vips->Xsize; x++ ) {
 				putc( p[2], out );
				putc( p[1], out );
				putc( p[0], out );
				putc( p[3], out );
				p += 4;
			}
		break;

	case 3:
		im_diagnostics( "mitsub: sending IM_TYPE_RGB ..." );
		for( y = 0; y < vips->Ysize; y++ )
			for( x = 0; x < vips->Xsize; x++ ) {
 				putc( p[0], out );
				putc( p[1], out );
				putc( p[2], out );
				p += 3;
			}
		break;

	case 1:
		im_diagnostics( "mitsub: sending K ..." );
		for( y = 0; y < vips->Ysize; y++ )
			for( x = 0; x < vips->Xsize; x++ )
 				putc( *p++, out );
		break;
	}

	/* Form feed. Page end.
	 */
	putc( 0x0c, out  );

	/* Now try to reset printer to default settings. 
	 *
	 * No enlargement.
	 */
	putc( 0x1b, out );
	putc( '&', out );
	putc( 'O', out );
	putc( '1', out );	
	putc( 0x1b, out );
	putc( '&', out );
	putc( 'P', out );
	putc( 1, out );
	putc( 1, out );
	putc( 0x1b, out );
	putc( '&', out );
	putc( 'Q', out );
	putc( 1, out );
	putc( 1, out );

	/* No centering.
	 */
	putc( 0x1b, out );
	putc( '&', out );
	putc( 'C', out );
	putc( '0', out );

	/* No colour reverse.
	 */
	putc( 0x1b, out );
	putc( '&', out );
	putc( 'W', out );
	putc( '0', out  );

	return( 0 );
}