Merge pull request #307 from Jondeen/add-hsv

Add hsv
This commit is contained in:
John Cupitt 2015-06-20 09:53:12 +01:00
commit d37454a45c
13 changed files with 209 additions and 2662 deletions

2
TODO
View File

@ -1,3 +1,5 @@
- vips_sink_screen() should create its output images
- how about something like vips_grid() which turns a tall thin one-band
image into a much smaller many-band image?

View File

@ -38,26 +38,58 @@
#include <stdio.h>
#include <math.h>
#include <vips/vips.h>
#include "pcolour.h"
#define SIXTH_OF_CHAR 42.5
typedef VipsColourCode VipsHSV2sRGB;
typedef VipsColourCodeClass VipsHSV2sRGBClass;
G_DEFINE_TYPE( VipsHSV2sRGB, vips_HSV2sRGB, VIPS_TYPE_COLOUR_CODE );
static void
vips_HSV2sRGB_line( VipsColour *colour, VipsPel *out, VipsPel **in, int width )
{
static void vips_HSV2sRGB_line(VipsColour *colour, VipsPel *out, VipsPel **in,
int width) {
unsigned char *p = (unsigned char *) in[0];
unsigned char *q = (unsigned char *) out;
int i;
float c, x, m;
for (i = 0; i < width; i++) {
q[0] = p[0];
q[1] = p[1];
q[2] = p[2];
c = p[2] * p[1] / 255.0f;
x = c * (1 - fabs(fmod(p[0] / SIXTH_OF_CHAR, 2) - 1));
m = p[2] - c;
if (p[0] < SIXTH_OF_CHAR) {
q[0]= (c+m);
q[1]= (x+m);
q[2]= (0+m);
} else if (p[0] < 2*SIXTH_OF_CHAR) {
q[0]= (x+m);
q[1]= (c+m);
q[2]= (0+m);
} else if (p[0] < 3*SIXTH_OF_CHAR) {
q[0]= (0+m);
q[1]= (c+m);
q[2]= (x+m);
} else if (p[0] < 4*SIXTH_OF_CHAR) {
q[0]= (0+m);
q[1]= (x+m);
q[2]= (c+m);
} else if (p[0] < 5*SIXTH_OF_CHAR) {
q[0]= (x+m);
q[1]= (0+m);
q[2]= (c+m);
} else {
q[0]= (c+m);
q[1]= (0+m);
q[2]= (x+m);
}
p += 3;
q += 3;

View File

@ -54,38 +54,36 @@ vips_sRGB2HSV_line( VipsColour *colour, VipsPel *out, VipsPel **in, int width )
unsigned char *q = (unsigned char *) out;
int i;
for( i = 0; i < width; i++ ) {
unsigned char c_max;
unsigned char c_min;
unsigned char delta;
float wrap_around_hue;
float secondary_diff;
unsigned char c_max,c_min,delta;
wrap_around_hue = 0;
secondary_diff = 0;
float wrap_around_hue, secondary_diff;
for( i = 0; i < width; i++ ) {
if (p[1] < p[2]) {
if (p[2] < p[0]) {
// Center red (at top)
c_max = p[0];
c_min = p[1];
secondary_diff = p[1] - p[2];
wrap_around_hue = 255.0f;
}
else {
} else {
// Center blue
c_max = p[2];
c_min = VIPS_MIN(p[1], p[0]);
secondary_diff = p[0] - p[1];
wrap_around_hue = 170.0f;
}
}
else {
} else {
if (p[1] < p[0]) {
// Center red (at bottom)
c_max = p[0];
c_min = p[2];
secondary_diff = p[1] - p[2];
wrap_around_hue = 0.0f;
}
else {
} else {
// Center green
c_max = p[1];
c_min = VIPS_MIN(p[2], p[0]);
secondary_diff = p[2] - p[0];
@ -93,24 +91,22 @@ vips_sRGB2HSV_line( VipsColour *colour, VipsPel *out, VipsPel **in, int width )
}
}
if (c_max == 0) {
q[0] = 0;
q[1] = 0;
q[2] = 0;
}
else {
} else {
q[2] = c_max;
delta = c_max - c_min;
if( delta == 0 )
if (delta == 0) {
q[0] = 0;
else {
q[0] = (unsigned char)
(42.5f * (secondary_diff / delta) +
wrap_around_hue);
} else {
q[0] = (42.5f*(secondary_diff / (float) delta) + wrap_around_hue);
}
q[1] = delta * 255.0f / c_max;
q[1] = (( delta*255.0f / (float) c_max));
}
p += 3;

View File

@ -2,6 +2,8 @@
*
* 5/6/15
* - from copy.c
* 10/6/15
* - add @factor option
*/
/*
@ -58,6 +60,8 @@ typedef struct _VipsBandfold {
*/
VipsImage *in;
int factor;
} VipsBandfold;
typedef VipsConversionClass VipsBandfoldClass;
@ -68,8 +72,8 @@ static int
vips_bandfold_gen( VipsRegion *or,
void *seq, void *a, void *b, gboolean *stop )
{
VipsBandfold *bandfold = (VipsBandfold *) b;
VipsRegion *ir = (VipsRegion *) seq;
VipsImage *in = ir->im;
VipsImage *out = or->im;
VipsRect *r = &or->valid;
int psize = VIPS_IMAGE_SIZEOF_PEL( out );
@ -77,21 +81,22 @@ vips_bandfold_gen( VipsRegion *or,
VipsRect need;
int y;
need.left = 0;
need.left = r->left * bandfold->factor;
need.top = r->top;
need.width = in->Xsize;
need.width = r->width * bandfold->factor;
need.height = r->height;
if( vips_region_prepare( ir, &need ) )
return( -1 );
for( y = 0; y < r->height; y++ ) {
VipsPel *p = VIPS_REGION_ADDR( ir, 0, r->top + y );
VipsPel *q = VIPS_REGION_ADDR( or, 0, r->top + y );
VipsPel *p = VIPS_REGION_ADDR( ir,
r->left * bandfold->factor, r->top + y );
VipsPel *q = VIPS_REGION_ADDR( or, r->left, r->top + y );
/* We can't use vips_region_region() since we change pixel
* coordinates.
*/
memcpy( q, p, psize );
memcpy( q, p, psize * r->width );
}
return( 0 );
@ -100,6 +105,7 @@ vips_bandfold_gen( VipsRegion *or,
static int
vips_bandfold_build( VipsObject *object )
{
VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( object );
VipsConversion *conversion = VIPS_CONVERSION( object );
VipsBandfold *bandfold = (VipsBandfold *) object;
@ -109,12 +115,20 @@ vips_bandfold_build( VipsObject *object )
if( vips_image_pio_input( bandfold->in ) )
return( -1 );
if( bandfold->factor == 0 )
bandfold->factor = bandfold->in->Xsize;
if( bandfold->in->Xsize % bandfold->factor != 0 ) {
vips_error( class->nickname,
"%s", _( "@factor must be a factor of image width" ) );
return( -1 );
}
if( vips_image_pipelinev( conversion->out,
VIPS_DEMAND_STYLE_THINSTRIP, bandfold->in, NULL ) )
return( -1 );
conversion->out->Xsize = 1;
conversion->out->Bands *= bandfold->in->Xsize;
conversion->out->Xsize /= bandfold->factor;
conversion->out->Bands *= bandfold->factor;
if( vips_image_generate( conversion->out,
vips_start_one, vips_bandfold_gen, vips_stop_one,
@ -148,11 +162,20 @@ vips_bandfold_class_init( VipsBandfoldClass *class )
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsBandfold, in ) );
VIPS_ARG_INT( class, "factor", 11,
_( "Factor" ),
_( "Fold by this factor" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsBandfold, factor ),
0, 10000000, 0 );
}
static void
vips_bandfold_init( VipsBandfold *bandfold )
{
/* 0 means fold by width, see above.
*/
bandfold->factor = 0;
}
/**
@ -161,9 +184,14 @@ vips_bandfold_init( VipsBandfold *bandfold )
* @out: output image
* @...: %NULL-terminated list of optional named arguments
*
* Optional arguments:
*
* @factor: fold by this factor
*
* Fold up an image horizontally: width is collapsed into bands.
* @out has width 1 and has bands
* equal to @in bands times @in width.
* Use @factor to set how much to fold by: @factor 3, for example, will make
* the output image three times narrower than the input, and with three times
* as many bands. By default the whole of the input width is folded up.
*
* See also: vips_csvload(), vips_bandunfold().
*

View File

@ -2,6 +2,8 @@
*
* 5/6/15
* - from copy.c
* 10/6/15
* - add @factor option
*/
/*
@ -58,6 +60,8 @@ typedef struct _VipsBandunfold {
*/
VipsImage *in;
int factor;
} VipsBandunfold;
typedef VipsConversionClass VipsBandunfoldClass;
@ -68,30 +72,34 @@ static int
vips_bandunfold_gen( VipsRegion *or,
void *seq, void *a, void *b, gboolean *stop )
{
VipsBandunfold *bandunfold = (VipsBandunfold *) b;
VipsRegion *ir = (VipsRegion *) seq;
VipsImage *in = ir->im;
VipsImage *out = or->im;
VipsRect *r = &or->valid;
int esize = VIPS_IMAGE_SIZEOF_ELEMENT( in );
int psize = VIPS_IMAGE_SIZEOF_PEL( out );
VipsRect need;
int y;
need.left = 0;
need.left = r->left / bandunfold->factor;
need.top = r->top;
need.width = 1;
need.width = (1 + r->width) / bandunfold->factor;
need.height = r->height;
if( vips_region_prepare( ir, &need ) )
return( -1 );
for( y = 0; y < r->height; y++ ) {
VipsPel *p = VIPS_REGION_ADDR( ir, 0, r->top + y ) +
r->left * esize;
VipsPel *p = VIPS_REGION_ADDR( ir,
r->left / bandunfold->factor, r->top + y ) +
(r->left % bandunfold->factor) * esize;
VipsPel *q = VIPS_REGION_ADDR( or, r->left, r->top + y );
/* We can't use vips_region_region() since we change pixel
* coordinates.
*/
memcpy( q, p, r->width * esize );
memcpy( q, p, r->width * psize );
}
return( 0 );
@ -100,6 +108,7 @@ vips_bandunfold_gen( VipsRegion *or,
static int
vips_bandunfold_build( VipsObject *object )
{
VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( object );
VipsConversion *conversion = VIPS_CONVERSION( object );
VipsBandunfold *bandunfold = (VipsBandunfold *) object;
@ -109,12 +118,20 @@ vips_bandunfold_build( VipsObject *object )
if( vips_image_pio_input( bandunfold->in ) )
return( -1 );
if( bandunfold->factor == 0 )
bandunfold->factor = bandunfold->in->Bands;
if( bandunfold->in->Bands % bandunfold->factor != 0 ) {
vips_error( class->nickname,
"%s", _( "@factor must be a factor of image bands" ) );
return( -1 );
}
if( vips_image_pipelinev( conversion->out,
VIPS_DEMAND_STYLE_THINSTRIP, bandunfold->in, NULL ) )
return( -1 );
conversion->out->Xsize *= bandunfold->in->Bands;
conversion->out->Bands = 1;
conversion->out->Xsize *= bandunfold->factor;
conversion->out->Bands /= bandunfold->factor;
if( vips_image_generate( conversion->out,
vips_start_one, vips_bandunfold_gen, vips_stop_one,
@ -148,11 +165,20 @@ vips_bandunfold_class_init( VipsBandunfoldClass *class )
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsBandunfold, in ) );
VIPS_ARG_INT( class, "factor", 11,
_( "Factor" ),
_( "Unfold by this factor" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsBandunfold, factor ),
0, 10000000, 0 );
}
static void
vips_bandunfold_init( VipsBandunfold *bandunfold )
{
/* 0 means unfold by width, see above.
*/
bandunfold->factor = 0;
}
/**
@ -161,8 +187,14 @@ vips_bandunfold_init( VipsBandunfold *bandunfold )
* @out: output image
* @...: %NULL-terminated list of optional named arguments
*
* Unfold image bands into x axis. @out has 1 band, and has width
* equal to @in bands times @in width.
* Optional arguments:
*
* @factor: unfold by this factor
*
* Unfold image bands into x axis.
* Use @factor to set how much to unfold by: @factor 3, for example, will make
* the output image three times wider than the input, and with one third
* as many bands. By default, all bands are unfolded.
*
* See also: vips_csvload(), vips_bandfold().
*

View File

@ -189,7 +189,8 @@ vips_foreign_load_csv_init( VipsForeignLoadCsv *csv )
* @separator: set of separator characters
*
* Load a CSV (comma-separated values) file. The output image is always 1
* band (monochrome), #VIPS_FORMAT_DOUBLE.
* band (monochrome), #VIPS_FORMAT_DOUBLE. Use vips_bandfold() to turn
* RGBRGBRGB mono images into colour iamges.
*
* Items in lines can be either floating point numbers in the C locale, or
* strings enclosed in double-quotes ("), or empty.
@ -213,7 +214,7 @@ vips_foreign_load_csv_init( VipsForeignLoadCsv *csv )
* @separator sets the characters that separate fields.
* Default ;,<emphasis>tab</emphasis>. Separators are never run together.
*
* See also: vips_image_new_from_file().
* See also: vips_image_new_from_file(), vips_bandfold().
*
* Returns: 0 on success, -1 on error.
*/

View File

@ -342,7 +342,7 @@ vips_gsf_has_zip64( void )
{
return( libgsf_major_version > 1 ||
libgsf_minor_version > 14 ||
libgsf_micro_version > 31 );
libgsf_micro_version >= 31 );
}
typedef struct _VipsForeignSaveDz VipsForeignSaveDz;

View File

@ -10,6 +10,8 @@
* - pack and unpack rad to scrgb
* 18/8/14
* - fix conversion to 16-bit RGB, thanks John
* 18/6/15
* - forward progress signals from load
*/
/*
@ -758,6 +760,31 @@ vips_foreign_load_iscompat( VipsImage *a, VipsImage *b )
return( TRUE );
}
/* Forward pre-eval-post signals to our output image so our caller can see
* progress for load via disc or memory.
*/
static void
vips_foreign_load_preeval( VipsImage *image,
VipsProgress *progress, VipsForeignLoad *load )
{
vips_image_preeval( load->out );
}
static void
vips_foreign_load_eval( VipsImage *image,
VipsProgress *progress, VipsForeignLoad *load )
{
vips_image_eval( load->out, progress->npels );
}
static void
vips_foreign_load_posteval( VipsImage *image,
VipsProgress *progress, VipsForeignLoad *load )
{
vips_image_posteval( load->out );
}
/* Our start function ... do the lazy open, if necessary, and return a region
* on the new image.
*/
@ -775,8 +802,20 @@ vips_foreign_load_start( VipsImage *out, void *a, void *b )
printf( "vips_foreign_load_start: triggering ->load()\n" );
#endif /*DEBUG*/
/* Read the image in.
/* Read the image in. This may involve a long computation and
* will finish with load->real holding the decompressed image.
*
* We want our caller to be able to see this computation on
* @out, so we need to forward the signals.
*/
g_signal_connect( load->real, "preeval",
G_CALLBACK( vips_foreign_load_preeval ), load );
g_signal_connect( load->real, "eval",
G_CALLBACK( vips_foreign_load_eval ), load );
g_signal_connect( load->real, "posteval",
G_CALLBACK( vips_foreign_load_posteval ), load );
vips_image_set_progress( load->real, TRUE );
if( class->load( load ) ||
vips_image_pio_input( load->real ) )
return( NULL );

View File

@ -482,6 +482,7 @@ vips_image_guess_interpretation( const VipsImage *image )
case VIPS_INTERPRETATION_sRGB:
case VIPS_INTERPRETATION_HSV:
case VIPS_INTERPRETATION_scRGB:
case VIPS_INTERPRETATION_HSV:
case VIPS_INTERPRETATION_YXY:
if( image->Bands < 3 )
sane = FALSE;

View File

@ -746,7 +746,7 @@ vips_image_save_cb( VipsImage *image, int *result )
/* Progress feedback.
*/
static int
static void
vips_image_preeval_cb( VipsImage *image, VipsProgress *progress, int *last )
{
int tile_width;
@ -754,7 +754,7 @@ vips_image_preeval_cb( VipsImage *image, VipsProgress *progress, int *last )
int nlines;
if( vips_image_get_typeof( image, "hide-progress" ) )
return( 0 );
return;
*last = -1;
@ -767,15 +767,13 @@ vips_image_preeval_cb( VipsImage *image, VipsProgress *progress, int *last )
vips_concurrency_get(),
tile_width, tile_height, nlines );
printf( "\n" );
return( 0 );
}
static int
static void
vips_image_eval_cb( VipsImage *image, VipsProgress *progress, int *last )
{
if( vips_image_get_typeof( image, "hide-progress" ) )
return( 0 );
return;
if( progress->percent != *last ) {
printf( _( "%s %s: %d%% complete" ),
@ -790,23 +788,19 @@ vips_image_eval_cb( VipsImage *image, VipsProgress *progress, int *last )
vips_region_dump_all();
*/
}
return( 0 );
}
static int
static void
vips_image_posteval_cb( VipsImage *image, VipsProgress *progress )
{
if( vips_image_get_typeof( image, "hide-progress" ) )
return( 0 );
return;
/* Spaces at end help to erase the %complete message we overwrite.
*/
printf( _( "%s %s: done in %.3gs \n" ),
g_get_prgname(), image->filename,
g_timer_elapsed( progress->start, NULL ) );
return( 0 );
}
/* Attach progress feedback, if required.

View File

@ -237,6 +237,15 @@ class TestConversion(unittest.TestCase):
self.assertEqual(y.bands, 1)
self.assertEqual(x.avg(), y.avg())
x = self.mono.bandfold(factor = 2)
self.assertEqual(x.width, self.mono.width / 2)
self.assertEqual(x.bands, 2)
y = x.bandunfold(factor = 2)
self.assertEqual(y.width, self.mono.width)
self.assertEqual(y.bands, 1)
self.assertEqual(x.avg(), y.avg())
def test_byteswap(self):
x = self.mono.cast("ushort")
y = x.byteswap().byteswap()

2587
vips

File diff suppressed because one or more lines are too long