From 21f9a7f69b426abe78313daffb06e385ee7a9239 Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Wed, 25 Jan 2012 15:03:13 +0000 Subject: [PATCH] find hist of any image type ... by casting to u8/u16. --- libvips/histograms_lut/im_histgr.c | 28 +++++++++++++++++++++++++--- libvips/histograms_lut/im_maplut.c | 2 +- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/libvips/histograms_lut/im_histgr.c b/libvips/histograms_lut/im_histgr.c index 19ff43f9..8e776ce6 100644 --- a/libvips/histograms_lut/im_histgr.c +++ b/libvips/histograms_lut/im_histgr.c @@ -19,7 +19,9 @@ * - number bands from zero * 24/3/10 * - gtkdoc - * - small celanups + * - small cleanups + * 25/1/12 + * - cast @in to u8/u16. */ /* @@ -281,6 +283,19 @@ find_ushort_hist_extract( REGION *reg, return( 0 ); } +/* Save a bit of typing. + */ +#define UC IM_BANDFMT_UCHAR +#define US IM_BANDFMT_USHORT +#define UI IM_BANDFMT_UINT + +/* Type mapping: go to uchar or ushort. + */ +static int bandfmt_histgr[10] = { +/* UC C US S UI I F X D DX */ + UC, UC, US, US, US, US, US, US, US, US +}; + /** * im_histgr: * @in: input image @@ -291,7 +306,7 @@ find_ushort_hist_extract( REGION *reg, * one-band histogram), or for all bands (producing an n-band histogram) if * @bandno is -1. * - * @in must be u8 or u16. @out is always u32. + * @in is cast to u8 or u16. @out is always u32. * * See also: im_hist_indexed(), im_histeq(). * @@ -300,6 +315,7 @@ find_ushort_hist_extract( REGION *reg, int im_histgr( IMAGE *in, IMAGE *out, int bandno ) { + IMAGE *t; int size; /* Length of hist */ int bands; /* Number of bands in output */ Histogram *mhist; @@ -310,12 +326,18 @@ im_histgr( IMAGE *in, IMAGE *out, int bandno ) /* Check images. PIO from in, WIO to out. */ if( im_check_uncoded( "im_histgr", in ) || - im_check_u8or16( "im_histgr", in ) || im_check_bandno( "im_histgr", in, bandno ) || im_pincheck( in ) || im_outcheck( out ) ) return( -1 ); + /* Cast in to u8/u16. + */ + if( !(t = im_open_local( out, "im_histgr", "p" )) || + im_clip2fmt( in, t, bandfmt_histgr[in->BandFmt] ) ) + return( -1 ); + in = t; + /* Find the range of pixel values we must handle. */ size = in->BandFmt == IM_BANDFMT_UCHAR ? 256 : 65536; diff --git a/libvips/histograms_lut/im_maplut.c b/libvips/histograms_lut/im_maplut.c index 6e3bf60d..37becb81 100644 --- a/libvips/histograms_lut/im_maplut.c +++ b/libvips/histograms_lut/im_maplut.c @@ -586,7 +586,7 @@ im_maplut( IMAGE *in, IMAGE *out, IMAGE *lut ) im_incheck( lut ) ) return( -1 ); - /* Cast in to u8/u16. + /* Cast in to u8/u16/u32. */ if( !(t = im_open_local( out, "im_maplut", "p" )) || im_clip2fmt( in, t, bandfmt_maplut[in->BandFmt] ) )