/* Convert to a saveable format. * * im__saveable_t gives the general type of image * we make: vanilla 1/3 bands (eg. PPM), with an optional alpha (eg. PNG), or * with CMYK as an option (eg. JPEG). * * format_table[] says how to convert each input format. * * Need to im_close() the result IMAGE. */ IMAGE * im__convert_saveable( IMAGE *in, im__saveable_t saveable, int format_table[10] ) { IMAGE *out; if( !(out = im_open( "convert-for-save", "p" )) ) return( NULL ); /* If this is an IM_CODING_LABQ, we can go straight to RGB. */ if( in->Coding == IM_CODING_LABQ ) { IMAGE *t = im_open_local( out, "conv:1", "p" ); static void *table = NULL; /* Make sure fast LabQ2disp tables are built. 7 is sRGB. */ if( !table ) table = im_LabQ2disp_build_table( NULL, im_col_displays( 7 ) ); if( !t || im_LabQ2disp_table( in, t, table ) ) { im_close( out ); return( NULL ); } in = t; } /* If this is an IM_CODING_RAD, we go to float RGB or XYZ. We should * probably un-gamma-correct the RGB :( */ if( in->Coding == IM_CODING_RAD ) { IMAGE *t; if( !(t = im_open_local( out, "conv:1", "p" )) || im_rad2float( in, t ) ) { im_close( out ); return( NULL ); } in = t; } /* Get the bands right. */ if( in->Coding == IM_CODING_NONE ) { if( in->Bands == 2 && saveable != IM__RGBA ) { IMAGE *t = im_open_local( out, "conv:1", "p" ); if( !t || im_extract_band( in, t, 0 ) ) { im_close( out ); return( NULL ); } in = t; } else if( in->Bands > 3 && saveable == IM__RGB ) { IMAGE *t = im_open_local( out, "conv:1", "p" ); if( !t || im_extract_bands( in, t, 0, 3 ) ) { im_close( out ); return( NULL ); } in = t; } else if( in->Bands > 4 && (saveable == IM__RGB_CMYK || saveable == IM__RGBA) ) { IMAGE *t = im_open_local( out, "conv:1", "p" ); if( !t || im_extract_bands( in, t, 0, 4 ) ) { im_close( out ); return( NULL ); } in = t; } /* Else we have saveable IM__ANY and we don't chop bands down. */ } /* Interpret the Type field for colorimetric images. */ if( in->Bands == 3 && in->BandFmt == IM_BANDFMT_SHORT && in->Type == IM_TYPE_LABS ) { IMAGE *t = im_open_local( out, "conv:1", "p" ); if( !t || im_LabS2LabQ( in, t ) ) { im_close( out ); return( NULL ); } in = t; } if( in->Coding == IM_CODING_LABQ ) { IMAGE *t = im_open_local( out, "conv:1", "p" ); if( !t || im_LabQ2Lab( in, t ) ) { im_close( out ); return( NULL ); } in = t; } if( in->Coding != IM_CODING_NONE ) { im_close( out ); return( NULL ); } if( in->Bands == 3 && in->Type == IM_TYPE_LCH ) { IMAGE *t[2]; if( im_open_local_array( out, t, 2, "conv-1", "p" ) || im_clip2fmt( in, t[0], IM_BANDFMT_FLOAT ) || im_LCh2Lab( t[0], t[1] ) ) { im_close( out ); return( NULL ); } in = t[1]; } if( in->Bands == 3 && in->Type == IM_TYPE_YXY ) { IMAGE *t[2]; if( im_open_local_array( out, t, 2, "conv-1", "p" ) || im_clip2fmt( in, t[0], IM_BANDFMT_FLOAT ) || im_Yxy2XYZ( t[0], t[1] ) ) { im_close( out ); return( NULL ); } in = t[1]; } if( in->Bands == 3 && in->Type == IM_TYPE_UCS ) { IMAGE *t[2]; if( im_open_local_array( out, t, 2, "conv-1", "p" ) || im_clip2fmt( in, t[0], IM_BANDFMT_FLOAT ) || im_UCS2XYZ( t[0], t[1] ) ) { im_close( out ); return( NULL ); } in = t[1]; } if( in->Bands == 3 && in->Type == IM_TYPE_LAB ) { IMAGE *t[2]; if( im_open_local_array( out, t, 2, "conv-1", "p" ) || im_clip2fmt( in, t[0], IM_BANDFMT_FLOAT ) || im_Lab2XYZ( t[0], t[1] ) ) { im_close( out ); return( NULL ); } in = t[1]; } if( in->Bands == 3 && in->Type == IM_TYPE_XYZ ) { IMAGE *t[2]; if( im_open_local_array( out, t, 2, "conv-1", "p" ) || im_clip2fmt( in, t[0], IM_BANDFMT_FLOAT ) || im_XYZ2disp( t[0], t[1], im_col_displays( 7 ) ) ) { im_close( out ); return( NULL ); } in = t[1]; } /* Cast to the output format. */ { IMAGE *t = im_open_local( out, "conv:1", "p" ); if( !t || im_clip2fmt( in, t, format_table[in->BandFmt] ) ) { im_close( out ); return( NULL ); } in = t; } if( im_copy( in, out ) ) { im_close( out ); return( NULL ); } return( out ); }
/* Call im_rad2float() via arg vector. */ static int rad2float_vec( im_object *argv ) { return( im_rad2float( argv[0], argv[1] ) ); }
static int shrink_factor( IMAGE *in, IMAGE *out, int shrink, double residual, VipsInterpolate *interp ) { IMAGE *t[9]; VipsImage **s = (VipsImage **) vips_object_local_array( VIPS_OBJECT( out ), 1 ); IMAGE *x; int tile_width; int tile_height; int nlines; if( im_open_local_array( out, t, 9, "thumbnail", "p" ) ) return( -1 ); x = in; /* Unpack the two coded formats we support to float for processing. */ if( x->Coding == IM_CODING_LABQ ) { if( verbose ) printf( "unpacking LAB to RGB\n" ); if( im_LabQ2disp( x, t[1], im_col_displays( 7 ) ) ) return( -1 ); x = t[1]; } else if( x->Coding == IM_CODING_RAD ) { if( verbose ) printf( "unpacking Rad to float\n" ); if( im_rad2float( x, t[1] ) ) return( -1 ); x = t[1]; } if( im_shrink( x, t[2], shrink, shrink ) ) return( -1 ); /* We want to make sure we read the image sequentially. * However, the convolution we may be doing later will force us * into SMALLTILE or maybe FATSTRIP mode and that will break * sequentiality. * * So ... read into a cache where tiles are scanlines, and make sure * we keep enough scanlines to be able to serve a line of tiles. */ vips_get_tile_size( t[2], &tile_width, &tile_height, &nlines ); if( vips_tilecache( t[2], &s[0], "tile_width", t[2]->Xsize, "tile_height", 10, "max_tiles", (nlines * 2) / 10, "strategy", VIPS_CACHE_SEQUENTIAL, NULL ) || im_affinei_all( s[0], t[4], interp, residual, 0, 0, residual, 0, 0 ) ) return( -1 ); x = t[4]; /* If we are upsampling, don't sharpen, since nearest looks dumb * sharpened. */ if( shrink > 1 && residual <= 1.0 && !nosharpen ) { if( verbose ) printf( "sharpening thumbnail\n" ); if( im_conv( x, t[5], sharpen_filter() ) ) return( -1 ); x = t[5]; } /* Colour management: we can transform the image if we have an output * profile and an input profile. The input profile can be in the * image, or if there is no profile there, supplied by the user. */ if( export_profile && (im_header_get_typeof( x, IM_META_ICC_NAME ) || import_profile) ) { if( im_header_get_typeof( x, IM_META_ICC_NAME ) ) { if( verbose ) printf( "importing with embedded profile\n" ); if( im_icc_import_embedded( x, t[6], IM_INTENT_RELATIVE_COLORIMETRIC ) ) return( -1 ); } else { if( verbose ) printf( "importing with profile %s\n", import_profile ); if( im_icc_import( x, t[6], import_profile, IM_INTENT_RELATIVE_COLORIMETRIC ) ) return( -1 ); } if( verbose ) printf( "exporting with profile %s\n", export_profile ); if( im_icc_export_depth( t[6], t[7], 8, export_profile, IM_INTENT_RELATIVE_COLORIMETRIC ) ) return( -1 ); x = t[7]; } if( delete_profile ) { if( verbose ) printf( "deleting profile from output image\n" ); if( im_meta_get_typeof( x, IM_META_ICC_NAME ) && !im_meta_remove( x, IM_META_ICC_NAME ) ) return( -1 ); } if( im_copy( x, out ) ) return( -1 ); return( 0 ); }