1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
/* vips_msb()
*
* Copyright: 2006, The Nottingham Trent University
*
* Author: Tom Vajzovic
*
* Written on: 2006-03-13
* 27/9/06
* - removed extra im_free() in im_copy() fallback
* 4/10/06
* - removed warning on uchar fallback: it happens a lot with nip2 and
* isn't very serious
* 1/2/10
* - revised, cleanups
* - gtkdoc
* 30/5/13
* - rewrite as a class
* - add --band option, remove im_msb_band()
*/
/*
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., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 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 <vips/vips.h>
#include "pconversion.h"
#include "bandary.h"
typedef struct _VipsMsb {
VipsConversion parent_instance;
/* Params.
*/
VipsImage *in;
int band;
/* Initial input offset.
*/
int offset;
/* Input step.
*/
int instep;
/* Need to convert signed to unsgned.
*/
gboolean sign;
} VipsMsb;
typedef VipsConversionClass VipsMsbClass;
G_DEFINE_TYPE( VipsMsb, vips_msb, VIPS_TYPE_CONVERSION );
static int
vips_msb_gen( VipsRegion *or, void *seq, void *a, void *b, gboolean *stop )
{
VipsRegion *ir = (VipsRegion *) seq;
VipsMsb *msb = (VipsMsb *) b;
VipsConversion *conversion = (VipsConversion *) msb;
VipsRect *r = &or->valid;
int le = r->left;
int to = r->top;
int bo = VIPS_RECT_BOTTOM( r );
int sz = r->width * conversion->out->Bands;
int x, y, i;
if( vips_region_prepare( ir, r ) )
return( -1 );
for( y = to; y < bo; y++ ) {
VipsPel *p = VIPS_REGION_ADDR( ir, le, y );
VipsPel *q = VIPS_REGION_ADDR( or, le, y );
if( msb->in->Coding == VIPS_CODING_LABQ &&
msb->band == -1 ) {
/* LABQ, no sub-band select.
*/
for( x = 0; x < r->width; x++ ) {
q[0] = p[0];
q[1] = 0x80 ^ p[1];
q[2] = 0x80 ^ p[2];
q += 4;
p += 3;
}
}
else if( msb->sign ) {
/* Copy, converting signed to unsigned.
*/
p += msb->offset;
for( i = 0; i < sz; i++ ) {
q[i] = 0x80 ^ *p;
p += msb->instep;
}
}
else {
/* Just pick out bytes.
*/
p += msb->offset;
for( i = 0; i < sz; i++ ) {
q[i] = *p;
p += msb->instep;
}
}
}
return( 0 );
}
static int
vips_msb_build( VipsObject *object )
{
VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( object );
VipsConversion *conversion = (VipsConversion *) object;
VipsMsb *msb = (VipsMsb *) object;
int vbands;
if( VIPS_OBJECT_CLASS( vips_msb_parent_class )->build( object ) )
return( -1 );
if( vips_check_coding_noneorlabq( class->nickname, msb->in ) ||
vips_check_int( class->nickname, msb->in ) )
return( -1 );
/* Effective number of bands this image has.
*/
vbands = msb->in->Coding == VIPS_CODING_LABQ ?
3 : msb->in->Bands;
if( msb->band > vbands - 1 ) {
vips_error( class->nickname, "%s", _( "bad band" ) );
return( -1 );
}
/* Step to next input element.
*/
msb->instep = VIPS_IMAGE_SIZEOF_ELEMENT( msb->in );
/* Offset into first band element of high order byte.
*/
msb->offset = vips_amiMSBfirst() ?
0 : VIPS_IMAGE_SIZEOF_ELEMENT( msb->in ) - 1;
/* If we're picking out a band, they need scaling up.
*/
if( msb->band != -1 ) {
msb->offset += VIPS_IMAGE_SIZEOF_ELEMENT( msb->in ) *
msb->band;
msb->instep *= msb->in->Bands;
}
/* May need to flip sign if we're picking out a band from labq.
*/
if( msb->in->Coding == VIPS_CODING_LABQ &&
msb->band > 0 )
msb->sign = TRUE;
if( msb->in->Coding == VIPS_CODING_NONE &&
!vips_band_format_isuint( msb->in->BandFmt ) )
msb->sign = TRUE;
if( msb->band == -1 &&
msb->in->BandFmt == VIPS_FORMAT_UCHAR )
return( vips_image_write( msb->in, conversion->out ) );
if( msb->band == 0 &&
msb->in->Bands == 1 &&
msb->in->BandFmt == VIPS_FORMAT_UCHAR )
return( vips_image_write( msb->in, conversion->out ) );
if( vips_image_pipelinev( conversion->out,
VIPS_DEMAND_STYLE_THINSTRIP, msb->in, NULL ) )
return( -1 );
if( msb->band != -1 )
conversion->out->Bands = 1;
else
conversion->out->Bands = vbands;
conversion->out->BandFmt = VIPS_FORMAT_UCHAR;
conversion->out->Coding = VIPS_CODING_NONE;
if( conversion->out->Bands == 1 )
conversion->out->Type = VIPS_INTERPRETATION_B_W;
else
conversion->out->Type = VIPS_INTERPRETATION_MULTIBAND;
if( vips_image_generate( conversion->out,
vips_start_one, vips_msb_gen, vips_stop_one, msb->in, msb ) )
return( -1 );
return( 0 );
}
static void
vips_msb_class_init( VipsMsbClass *class )
{
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
VipsObjectClass *vobject_class = VIPS_OBJECT_CLASS( class );
VipsOperationClass *operation_class = VIPS_OPERATION_CLASS( class );
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
vobject_class->nickname = "msb";
vobject_class->description =
_( "pick most-significant byte from an image" );
vobject_class->build = vips_msb_build;
operation_class->flags = VIPS_OPERATION_SEQUENTIAL_UNBUFFERED;
VIPS_ARG_IMAGE( class, "in", 0,
_( "Input" ),
_( "Input image" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsMsb, in ) );
VIPS_ARG_INT( class, "band", 3,
_( "Band" ),
_( "Band to msb" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsMsb, band ),
0, 100000000, 0 );
}
static void
vips_msb_init( VipsMsb *msb )
{
msb->band = -1;
}
/**
* vips_msb:
* @in: input image
* @out: output image
* @...: %NULL-terminated list of optional named arguments
*
* Optional arguments:
*
* * @band: %gint, msb just this band
*
* 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.
*
* Use @band to make a one-band 8-bit image.
*
* This operator also works for LABQ coding.
*
* See also: vips_scale(), vips_cast().
*
* Returns: 0 on success, -1 on error.
*/
int
vips_msb( VipsImage *in, VipsImage **out, ... )
{
va_list ap;
int result;
va_start( ap, out );
result = vips_call_split( "msb", ap, in, out );
va_end( ap );
return( result );
}
|