switch to new vips_copy()

This commit is contained in:
John Cupitt 2011-10-15 06:49:59 +01:00
parent 22a585c45b
commit 7999edbc73
10 changed files with 44 additions and 428 deletions

View File

@ -3563,9 +3563,8 @@ namespace cimg_library {
}
if (name) {
if (argc>0) {
int k=0,i;
int k=0;
while (k<argc && cimg::strcmp(argv[k],name)) k++;
i=k;
res=(k++==argc?defaut:(k==argc?argv[--k]:argv[k]));
} else res = defaut;
if (visu && usage) std::fprintf(stderr," %s%-8s%s = %-12s : %s%s%s\n",

View File

@ -11,7 +11,6 @@ libconversion_la_SOURCES = \
im_c2imag.c \
im_c2real.c \
im_clip2fmt.c \
im_copy.c \
im_copy_file.c \
im_extract.c \
im_falsecolour.c \

View File

@ -1,385 +0,0 @@
/* Copy an image.
*
* Copyright: 1990, N. Dessipris, based on im_powtra()
* Author: Nicos Dessipris
* Written on: 02/05/1990
* Modified on:
* 23/4/93 J.Cupitt
* - adapted to work with partial images
* 30/6/93 JC
* - adapted for partial v2
* - and ANSI C
* 7/7/93 JC
* - now does IM_CODING_LABQ too
* 22/2/95 JC
* - new use of im_region_region()
* 25/6/02 JC
* - added im_copy_set()
* - hint is IM_ANY
* 5/9/02 JC
* - added xoff/yoff to copy_set
* 14/4/04 JC
* - im_copy() now zeros Xoffset/Yoffset (since origin is the same as
* input)
* 26/5/04 JC
* - added im_copy_swap()
* 1/6/05
* - added im_copy_morph()
* 13/6/05
* - oop, im_copy_set() was messed up
* 29/9/06
* - added im_copy_set_meta(), handy wrapper for nip2 to set meta fields
* 2/11/06
* - moved im__convert_saveable() here so it's always defined (was part
* of JPEG write code)
* 15/2/08
* - added im__saveable_t ... so we can have CMYK JPEG write
* 24/3/09
* - added IM_CODING_RAD support
* 28/1/10
* - gtk-doc
* - cleanups
* - removed im_copy_from() and associated stuff
* - added im_copy_native()
* 28/11/10
* - im_copy_set() now sets xoff / yoff again hmmm
*/
/*
This file is part of VIPS.
VIPS is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <vips/intl.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <vips/vips.h>
#include <vips/internal.h>
/* Copy a small area.
*/
static int
copy_gen( REGION *or, void *seq, void *a, void *b )
{
REGION *ir = (REGION *) seq;
Rect *r = &or->valid;
/* Ask for input we need.
*/
if( im_prepare( ir, r ) )
return( -1 );
/* Attach output region to that.
*/
if( im_region_region( or, ir, r, r->left, r->top ) )
return( -1 );
return( 0 );
}
/* Copy image, changing header fields.
*/
static int
im_copy_set_all( IMAGE *in, IMAGE *out,
VipsType type, float xres, float yres, int xoffset, int yoffset,
int bands, VipsBandFmt bandfmt, VipsCoding coding )
{
/* Check args.
*/
if( im_check_coding_known( "im_copy", in ) ||
im_piocheck( in, out ) )
return( -1 );
if( coding != IM_CODING_NONE &&
coding != IM_CODING_LABQ &&
coding != IM_CODING_RAD ) {
im_error( "im_copy", "%s",
_( "coding must be NONE, LABQ or RAD" ) );
return( -1 );
}
if( bandfmt < 0 || bandfmt > IM_BANDFMT_DPCOMPLEX ) {
im_error( "im_copy", _( "bandfmt must be in range [0,%d]" ),
IM_BANDFMT_DPCOMPLEX );
return( -1 );
}
if( im_cp_desc( out, in ) )
return( -1 );
out->Type = type;
out->Xres = xres;
out->Yres = yres;
out->Xoffset = xoffset;
out->Yoffset = yoffset;
out->Bands = bands;
out->BandFmt = bandfmt;
out->Coding = coding;
/* Sanity check: we (may) have changed bytes-per-pixel since we've
* changed Bands and BandFmt ... bad!
*/
if( IM_IMAGE_SIZEOF_PEL( in ) != IM_IMAGE_SIZEOF_PEL( out ) ) {
im_error( "im_copy", "%s", _( "sizeof( pixel ) has changed" ) );
return( -1 );
}
/* Generate!
*/
if( im_demand_hint( out, IM_THINSTRIP, in, NULL ) ||
im_generate( out,
im_start_one, copy_gen, im_stop_one, in, NULL ) )
return( -1 );
return( 0 );
}
/**
* im_copy_set:
* @in: input image
* @out: output image
* @type: new VipsType to set
* @xres: new Xres to set
* @yres: new Yres to set
* @xoffset: new Xoffset to set
* @yoffset: new Yoffset to set
*
* Copy an image, changing informational header fields on the way.
*
* See also: im_copy().
*
* Returns: 0 on success, -1 on error.
*/
int
im_copy_set( IMAGE *in, IMAGE *out,
VipsType type, float xres, float yres, int xoffset, int yoffset )
{
return( im_copy_set_all( in, out,
type, xres, yres, xoffset, yoffset,
in->Bands, in->BandFmt, in->Coding ) );
}
/**
* im_copy_morph:
* @in: input image
* @out: output image
* @bands: new number of bands
* @bandfmt: new band format
* @coding: new coding
*
* Copy an image, changing header fields which alter pixel addressing. The
* pixel data itself is unchanged, this operation just changes the header
* fields.
*
* If you change the header fields such that the sizeof() a pixel changes,
* you'll get an error.
*
* See also: im_copy().
*
* Returns: 0 on success, -1 on error.
*/
int
im_copy_morph( IMAGE *in, IMAGE *out,
int bands, VipsBandFmt bandfmt, VipsCoding coding )
{
return( im_copy_set_all( in, out,
in->Type, in->Xres, in->Yres, 0, 0,
bands, bandfmt, coding ) );
}
/**
* im_copy:
* @in: input image
* @out: output image
*
* Copy an image. VIPS copies images by copying pointers, so this operation is
* fast, even for very large images.
*
* See also: im_copy(), im_copy_set(), im_copy_morph().
*
* Returns: 0 on success, -1 on error.
*/
int
im_copy( IMAGE *in, IMAGE *out )
{
return( im_copy_set( in, out,
in->Type, in->Xres, in->Yres, 0, 0 ) );
}
/**
* im_copy_set_meta:
* @in: input image
* @out: output image
* @field: metadata field to set
* @value: value to set for the field
*
* Copy an image, changing a metadata field. You can use this to, for example,
* update the ICC profile attached to an image.
*
* See also: im_copy().
*
* Returns: 0 on success, -1 on error.
*/
int
im_copy_set_meta( IMAGE *in, IMAGE *out, const char *field, GValue *value )
{
if( im_copy( in, out ) )
return( 1 );
im_meta_set( out, field, value );
return( 0 );
}
/* Swap pairs of bytes.
*/
static void
im_copy_swap2_gen( PEL *in, PEL *out, int width, IMAGE *im )
{
int x;
int sz = IM_IMAGE_SIZEOF_PEL( im ) * width; /* Bytes in buffer */
for( x = 0; x < sz; x += 2 ) {
out[x] = in[x + 1];
out[x + 1] = in[x];
}
}
/* Swap 4- of bytes.
*/
static void
im_copy_swap4_gen( PEL *in, PEL *out, int width, IMAGE *im )
{
int x;
int sz = IM_IMAGE_SIZEOF_PEL( im ) * width; /* Bytes in buffer */
for( x = 0; x < sz; x += 4 ) {
out[x] = in[x + 3];
out[x + 1] = in[x + 2];
out[x + 2] = in[x + 1];
out[x + 3] = in[x];
}
}
/* Swap 8- of bytes.
*/
static void
im_copy_swap8_gen( PEL *in, PEL *out, int width, IMAGE *im )
{
int x;
int sz = IM_IMAGE_SIZEOF_PEL( im ) * width; /* Bytes in buffer */
for( x = 0; x < sz; x += 8 ) {
out[x] = in[x + 7];
out[x + 1] = in[x + 6];
out[x + 2] = in[x + 5];
out[x + 3] = in[x + 4];
out[x + 4] = in[x + 3];
out[x + 5] = in[x + 2];
out[x + 6] = in[x + 1];
out[x + 7] = in[x];
}
}
/**
* im_copy_swap:
* @in: input image
* @out: output image
*
* Copy an image, swapping byte order between little and big endian. This
* really does change image pixels and does not just alter the header.
*
* See also: im_copy(), im_amiMSBfirst(), im_isMSBfirst().
*
* Returns: 0 on success, -1 on error.
*/
int
im_copy_swap( IMAGE *in, IMAGE *out )
{
if( im_piocheck( in, out ) ||
im_check_uncoded( "im_copy_swap", in ) ||
im_cp_desc( out, in ) )
return( -1 );
switch( in->BandFmt ) {
case IM_BANDFMT_CHAR:
case IM_BANDFMT_UCHAR:
if( im_copy( in, out ) )
return( -1 );
break;
case IM_BANDFMT_SHORT:
case IM_BANDFMT_USHORT:
if( im_wrapone( in, out,
(im_wrapone_fn) im_copy_swap2_gen, in, NULL ) )
return( -1 );
break;
case IM_BANDFMT_INT:
case IM_BANDFMT_UINT:
case IM_BANDFMT_FLOAT:
case IM_BANDFMT_COMPLEX:
if( im_wrapone( in, out,
(im_wrapone_fn) im_copy_swap4_gen, in, NULL ) )
return( -1 );
break;
case IM_BANDFMT_DOUBLE:
case IM_BANDFMT_DPCOMPLEX:
if( im_wrapone( in, out,
(im_wrapone_fn) im_copy_swap8_gen, in, NULL ) )
return( -1 );
break;
default:
im_error( "im_copy_swap", "%s", _( "unsupported image type" ) );
return( -1 );
}
return( 0 );
}
/**
* im_copy_native:
* @in: input image
* @out: output image
* @is_msb_first: %TRUE if @in is in most-significant first form
*
* Copy an image to native order, that is, the order for the executing
* program.
*
* See also: im_copy_swap(), im_amiMSBfirst().
*
* Returns: 0 on success, -1 on error.
*/
int
im_copy_native( IMAGE *in, IMAGE *out, gboolean is_msb_first )
{
if( is_msb_first != im_amiMSBfirst() )
return( im_copy_swap( in, out ) );
else
return( im_copy( in, out ) );
}

View File

@ -113,7 +113,7 @@ im_resize_linear( IMAGE *in, IMAGE *out, int X, int Y )
PEL *q, *p;
int ils, ips, ies; /* Input and output line, pel and */
int ols, ops, oes; /* element sizes */
int ols, oes; /* element sizes */
if( im_iocheck( in, out ) )
return( -1 );
@ -139,7 +139,6 @@ im_resize_linear( IMAGE *in, IMAGE *out, int X, int Y )
ies = IM_IMAGE_SIZEOF_ELEMENT( in );
ols = IM_IMAGE_SIZEOF_LINE( out );
ops = IM_IMAGE_SIZEOF_PEL( out );
oes = IM_IMAGE_SIZEOF_ELEMENT( out );
/* buffer lines

View File

@ -907,13 +907,7 @@ im_add( IMAGE *in1, IMAGE *in2, IMAGE *out )
g_object_unref( x );
return( -1 );
}
/* When im_copy() is vips8'd it'll make a ref to in which will be
* junked when the copy shuts down and we can unref x directly.
*
* Until then, we have to use the "close" signal to delay the unref.
*/
vips_object_local( out, x );
g_object_unref( x );
return( 0 );
}
@ -929,13 +923,7 @@ im_subtract( IMAGE *in1, IMAGE *in2, IMAGE *out )
g_object_unref( x );
return( -1 );
}
/* When im_copy() is vips8'd it'll make a ref to in which will be
* junked when the copy shuts down and we can unref x directly.
*
* Until then, we have to use the "close" signal to delay the unref.
*/
vips_object_local( out, x );
g_object_unref( x );
return( 0 );
}
@ -993,7 +981,7 @@ im_copy_set( IMAGE *in, IMAGE *out,
{
VipsImage *x;
if( vips_copy( in, out,
if( vips_copy( in, &x,
"interpretation", type,
"xres", xres,
"yres", yres,
@ -1001,38 +989,65 @@ im_copy_set( IMAGE *in, IMAGE *out,
"yoffset", yoffset,
NULL ) )
return( -1 );
if( vips_image_write( x, out ) ) {
g_object_unref( x );
return( -1 );
}
g_object_unref( x );
return( 0 );
}
int
im_copy_morph( IMAGE *in, IMAGE *out,
int bands, VipsBandFmt bandfmt, VipsCoding coding )
{
return( vips_copy( in, out,
VipsImage *x;
if( vips_copy( in, &x,
"bands", bands,
"format", bandfmt,
"coding", coding,
NULL ) );
NULL ) )
return( -1 );
if( vips_image_write( x, out ) ) {
g_object_unref( x );
return( -1 );
}
g_object_unref( x );
return( 0 );
}
int
im_copy( IMAGE *in, IMAGE *out )
{
return( im_copy_set( in, out,
in->Type, in->Xres, in->Yres, 0, 0 ) );
return( vips_image_write( in, out ) );
}
int
im_copy_swap( IMAGE *in, IMAGE *out )
{
return( vips_copy( in, out, "swap", TRUE, NULL ) );
VipsImage *x;
if( vips_copy( in, &x,
"swap", TRUE,
NULL ) )
return( -1 );
if( vips_image_write( x, out ) ) {
g_object_unref( x );
return( -1 );
}
g_object_unref( x );
return( 0 );
}
int
im_copy_set_meta( IMAGE *in, IMAGE *out, const char *field, GValue *value )
{
if( im_copy( in, out ) )
return( 1 );
return( -1 );
im_meta_set( out, field, value );
return( 0 );

View File

@ -470,8 +470,6 @@ vips_image_init_fields( VipsImage *image,
static void *
meta_cp_field( VipsMeta *meta, VipsImage *dst )
{
VipsMeta *meta_copy;
#ifdef DEBUG
{
char *str_value;
@ -483,7 +481,7 @@ meta_cp_field( VipsMeta *meta, VipsImage *dst )
}
#endif /*DEBUG*/
meta_copy = meta_new( dst, meta->field, &meta->value );
(void) meta_new( dst, meta->field, &meta->value );
#ifdef DEBUG
meta_sanity( dst );
@ -659,13 +657,11 @@ vips_image_copy_fields( VipsImage *out, VipsImage *in )
void
vips_image_set( VipsImage *image, const char *field, GValue *value )
{
VipsMeta *meta;
g_assert( field );
g_assert( value );
meta_init( image );
meta = meta_new( image, field, value );
(void) meta_new( image, field, value );
#ifdef DEBUG
meta_sanity( image );

View File

@ -326,7 +326,7 @@ vips_image_to_string( VipsObject *object, VipsBuf *buf )
static int
vips_image_write_object( VipsObject *object, const char *string )
{
return( vips_image_write( VIPS_IMAGE( object ), string ) );
return( vips_image_write_filename( VIPS_IMAGE( object ), string ) );
}
static void *
@ -1798,7 +1798,7 @@ vips_image_write( VipsImage *image, VipsImage *out )
if( vips_image_generate( out,
vips_start_one, vips_image_write_gen, vips_stop_one,
copy->input, copy ) )
image, NULL ) )
return( -1 );
return( 0 );

View File

@ -172,7 +172,7 @@ vips__mmap( int fd, int writeable, size_t length, gint64 offset )
* LARGEFILE.
*/
baseaddr = mmap( 0, length, prot, MAP_SHARED, fd, (off_t) offset );
baseaddr = mmap( 0, length, prot, flags, fd, (off_t) offset );
if( baseaddr == MAP_FAILED ) {
vips_error_system( errno, "vips_mapfile",
"%s", _( "unable to mmap" ) );

View File

@ -475,7 +475,7 @@ im_read_dmask( const char *filename )
double sc, off;
int xs, ys;
DOUBLEMASK *out;
int x, y, i, size;
int x, y, i;
char buf[MAX_LINE];
if( !(fp = im__file_open_read( filename, NULL, TRUE )) )
@ -492,7 +492,6 @@ im_read_dmask( const char *filename )
}
out->scale = sc;
out->offset = off;
size = xs * ys;
for( i = 0, y = 0; y < ys; y++ ) {
char *p;

View File

@ -74,7 +74,6 @@ im__clinear( TIE_POINTS *points )
double scale, angle, xdelta, ydelta;
int *xref, *yref, *xsec, *ysec;
double *dx, *dy, *dev;
double resx, resy;
xref = &points->x_reference[0];
yref = &points->y_reference[0];
@ -92,8 +91,6 @@ im__clinear( TIE_POINTS *points )
return( -1 );
}
resx = 0.0;
resy = 0.0;
for( i = 0; i < points->nopoints; i++ ) {
sx1 += xref[i];
sx1x1 += xref[i] * xref[i];
@ -108,9 +105,6 @@ im__clinear( TIE_POINTS *points )
sy2 += ysec[i];
}
resx = fabs( sx1-sx2 )/points->nopoints;
resy = fabs( sy1-sy2 )/points->nopoints;
mat[0][0] = sx1x1 + sy1y1;
mat[0][1] = 0;
mat[0][2] = sx1;