/* Check whether arch corresponds to native byte order. */ gboolean im_isnative( im_arch_type arch ) { switch( arch ) { case IM_ARCH_NATIVE: return( TRUE ); case IM_ARCH_BYTE_SWAPPED: return( FALSE ); case IM_ARCH_LSB_FIRST: return( !im_amiMSBfirst() ); case IM_ARCH_MSB_FIRST: return( im_amiMSBfirst() ); default: g_assert( 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 ) ); }
int im_copy_from( IMAGE *in, IMAGE *out, im_arch_type architecture ) { switch( architecture ) { case IM_ARCH_NATIVE: return( im_copy( in, out ) ); case IM_ARCH_BYTE_SWAPPED: return( im_copy_swap( in, out ) ); case IM_ARCH_LSB_FIRST: return( im_amiMSBfirst() ? im_copy_swap( in, out ) : im_copy( in, out ) ); case IM_ARCH_MSB_FIRST: return( im_amiMSBfirst() ? im_copy( in, out ) : im_copy_swap( in, out ) ); default: im_error( "im_copy_from", _( "bad architecture: %d" ), architecture ); return( -1 ); } }
/** * im_msb: * @in: input image * @out: output image * * Turn any integer image to 8-bit unsigned char by discarding all but the most * significant byte. * Signed values are converted to unsigned by adding 128. * * This operator also works for LABQ coding. * * See also: im_msb_band(). * * Returns: 0 on success, -1 on error */ int im_msb( IMAGE *in, IMAGE *out ) { Msb *msb; im_wrapone_fn func; if( in->Coding == IM_CODING_NONE && in->BandFmt == IM_BANDFMT_UCHAR ) return( im_copy( in, out ) ); if( im_piocheck( in, out ) || !(msb = IM_NEW( out, Msb )) ) return( -1 ); if( in->Coding == IM_CODING_NONE ) { if( im_check_int( "im_msb", in ) ) return( -1 ); msb->width = IM_IMAGE_SIZEOF_ELEMENT( in ); msb->index = im_amiMSBfirst() ? 0 : msb->width - 1; msb->repeat = in->Bands; if( vips_bandfmt_isuint( in->BandFmt ) ) func = (im_wrapone_fn) byte_select; else func = (im_wrapone_fn) byte_select_flip; } else if( IM_CODING_LABQ == in->Coding ) func = (im_wrapone_fn) msb_labq; else { im_error( "im_msb", "%s", _( "unknown coding" ) ); return( -1 ); } if( im_cp_desc( out, in ) ) return( -1 ); out->BandFmt = IM_BANDFMT_UCHAR; out->Coding = IM_CODING_NONE; return( im_wrapone( in, out, func, msb, NULL ) ); }
/* Read a PNG file (header) into a VIPS (header). */ static int png2vips( Read *read, int header_only ) { int bands, bpp, type; if( setjmp( read->pPng->jmpbuf ) ) return( -1 ); png_init_io( read->pPng, read->fp ); png_read_info( read->pPng, read->pInfo ); /* png_get_channels() gives us 1 band for palette images ... so look * at colour_type for output bands. */ switch( read->pInfo->color_type ) { case PNG_COLOR_TYPE_PALETTE: bands = 3; /* Don't know if this is really correct. If there are * transparent pixels, assume we're going to output RGBA. */ if( read->pInfo->num_trans ) bands = 4; break; case PNG_COLOR_TYPE_GRAY: bands = 1; break; case PNG_COLOR_TYPE_GRAY_ALPHA: bands = 2; break; case PNG_COLOR_TYPE_RGB: bands = 3; break; case PNG_COLOR_TYPE_RGB_ALPHA: bands = 4; break; default: im_error( "im_png2vips", "%s", _( "unsupported color type" ) ); return( -1 ); } /* 8 or 16 bit. */ bpp = read->pInfo->bit_depth > 8 ? 2 : 1; if( bpp > 1 ) { if( bands < 3 ) type = IM_TYPE_GREY16; else type = IM_TYPE_RGB16; } else { if( bands < 3 ) type = IM_TYPE_B_W; else type = IM_TYPE_sRGB; } /* Expand palette images. */ if( read->pInfo->color_type == PNG_COLOR_TYPE_PALETTE ) png_set_expand( read->pPng ); /* Expand <8 bit images to full bytes. */ if( read->pInfo->bit_depth < 8 ) { png_set_packing( read->pPng ); png_set_shift( read->pPng, &(read->pInfo->sig_bit) ); } /* If we're an INTEL byte order machine and this is 16bits, we need * to swap bytes. */ if( read->pInfo->bit_depth > 8 && !im_amiMSBfirst() ) png_set_swap( read->pPng ); /* Set VIPS header. */ im_initdesc( read->out, read->pInfo->width, read->pInfo->height, bands, bpp == 1 ? IM_BBITS_BYTE : IM_BBITS_SHORT, bpp == 1 ? IM_BANDFMT_UCHAR : IM_BANDFMT_USHORT, IM_CODING_NONE, type, 1.0, 1.0, 0, 0 ); if( !header_only ) { if( png_set_interlace_handling( read->pPng ) > 1 ) { if( png2vips_interlace( read ) ) return( -1 ); } else { if( png2vips_noninterlace( read ) ) return( -1 ); } } return( 0 ); }
/** * im_msb_band: * @in: input image * @out: output image * @band: select this band * * Turn any integer image to a single-band 8-bit unsigned char by discarding * all but the most significant byte from the selected band. * Signed values are converted to unsigned by adding 128. * * This operator also works for LABQ coding. * * See also: im_msb_band(). * * Returns: 0 on success, -1 on error */ int im_msb_band( IMAGE *in, IMAGE *out, int band ) { Msb *msb; im_wrapone_fn func; if( band < 0 ) { im_error( "im_msb_band", "%s", _( "bad arguments" ) ); return( -1 ); } if( im_piocheck( in, out ) || !(msb = IM_NEW( out, Msb )) ) return( -1 ); if( in->Coding == IM_CODING_NONE ) { if( im_check_int( "im_msb_band", in ) ) return( -1 ); if( band >= in->Bands ) { im_error( "im_msb_band", "%s", _( "image does not have that many bands" ) ); return( -1 ); } msb->width = IM_IMAGE_SIZEOF_ELEMENT( in ); msb->index = im_amiMSBfirst() ? msb->width * band : msb->width * (band + 1) - 1; msb->repeat = 1; if( vips_bandfmt_isuint( in->BandFmt ) ) func = (im_wrapone_fn) byte_select; else func = (im_wrapone_fn) byte_select_flip; } else if( IM_CODING_LABQ == in->Coding ) { if( band > 2 ) { im_error( "im_msb_band", "%s", _( "image does not have that many bands" ) ); return( -1 ); } msb->width = 4; msb->repeat = 1; msb->index = band; if( band ) func = (im_wrapone_fn) byte_select_flip; else func = (im_wrapone_fn) byte_select; } else { im_error( "im_msb", "%s", _( "unknown coding" ) ); return( -1 ); } if( im_cp_desc( out, in ) ) return( -1 ); out->BandFmt = IM_BANDFMT_UCHAR; out->Coding = IM_CODING_NONE; out->Bands = 1; return( im_wrapone( in, out, func, msb, NULL ) ); }
void im_printdesc( IMAGE *image ) { if( !image ) { printf( "NULL descriptor\n" ); return; } printf( "IMAGE* %p\n", image ); if( im_isMSBfirst( image ) ) printf( "SPARC (MSB first) " ); else printf( "Intel (LSB first) " ); printf( "byte order image, on a " ); if( im_amiMSBfirst() ) printf( "SPARC (MSB first) " ); else printf( "Intel (LSB first) " ); printf( "byte order machine\n" ); (void) im_header_map( image, (im_header_map_fn) print_field_fn, NULL ); printf( "Hist: %s", im_history_get( image ) ); /* Print other (non-header) fields. */ if( image->generate ) printf( "generate function attached\n" ); if( image->closefns ) printf( "close callbacks attached\n" ); if( image->evalfns ) printf( "eval callbacks attached\n" ); if( image->evalendfns ) printf( "evalend callbacks attached\n" ); if( image->evalstartfns ) printf( "evalstart callbacks attached\n" ); if( image->preclosefns ) printf( "preclose callbacks attached\n" ); if( image->invalidatefns ) printf( "invalidate callbacks attached\n" ); if( image->regions ) { printf( "%d regions present\n", g_slist_length( image->regions ) ); im_slist_map2( image->regions, (VSListMap2Fn) print_region, NULL, NULL ); } if( image->kill ) printf( "kill flag set\n" ); if( image->closing ) printf( "closing flag set\n" ); if( image->close_pending ) printf( "close_pending flag set\n" ); #ifdef DEBUG /* Can't get these with im_header_get(), so only show for debugging. */ printf( "dhint: %s\n", im_dhint2char( image->dhint ) ); printf( "dtype: %s\n", im_dtype2char( image->dtype ) ); #endif /*DEBUG*/ }