fix up vipsheader with complex field types

"-f name" displays a detailed form, "-a" displays a summary of each
field

fix a problem with string fields as well
This commit is contained in:
John Cupitt 2016-11-01 11:51:20 +00:00
parent 3681ce0e77
commit a96d851ff2
6 changed files with 51 additions and 33 deletions

View File

@ -3,6 +3,7 @@
- added tiff save to buffer - added tiff save to buffer
- added dzsave save to buffer (zip only) - added dzsave save to buffer (zip only)
- revise header get/set functions - revise header get/set functions
- better vipsheader behaviour with complex field types
- added vips_image_hasalpha() - added vips_image_hasalpha()
18/10/16 started 8.4.3 18/10/16 started 8.4.3

6
TODO
View File

@ -1,9 +1,3 @@
- look at vips_image_get_as_string() vs. vips_buf_appendgv()
should Hist and getext be true header fields?
vipsheader is not displaying filename correctly
- not sure about utf8 error messages on win - not sure about utf8 error messages on win
- strange: - strange:

View File

@ -493,10 +493,12 @@ vips_buf_appendd( VipsBuf *buf, int d )
* @buf: the buffer * @buf: the buffer
* @value: #GValue to format and append * @value: #GValue to format and append
* *
* Format and append a #GValue. This doesn't use g_strdup_value_contents(): * Format and append a #GValue as a printable thing. We display text line "3144
* that doesn't do what we want for some types and can change too much between * bytes of binary data" for BLOBs like icc-profile-data.
* glib versions. *
* * Use vips_image_get_as_string() to make a text representation of a field.
* That will base64-encode blobs, for example.
*
* Returns: %FALSE on overflow, %TRUE otherwise. * Returns: %FALSE on overflow, %TRUE otherwise.
*/ */
gboolean gboolean
@ -513,11 +515,16 @@ vips_buf_appendgv( VipsBuf *buf, GValue *value )
switch( fundamental ) { switch( fundamental ) {
case G_TYPE_STRING: case G_TYPE_STRING:
/* These are GStrings, vips refstrings are handled by boxed, see {
* below. const char *str;
/* These are GStrings (gchararray). vips refstrings are
* handled by boxed, see below.
*/ */
result = vips_buf_appends( buf, g_value_get_string( value ) ); str = g_value_get_string( value );
result = vips_buf_appends( buf, str );
handled = TRUE; handled = TRUE;
}
break; break;
case G_TYPE_OBJECT: case G_TYPE_OBJECT:
@ -583,15 +590,26 @@ vips_buf_appendgv( VipsBuf *buf, GValue *value )
break; break;
case G_TYPE_BOXED: case G_TYPE_BOXED:
if( type == VIPS_TYPE_REF_STRING || if( type == VIPS_TYPE_REF_STRING ) {
type == VIPS_TYPE_BLOB ) {
const char *str; const char *str;
size_t str_len; size_t str_len;
/* These should be printable.
*/
str = vips_value_get_ref_string( value, &str_len ); str = vips_value_get_ref_string( value, &str_len );
result = vips_buf_appends( buf, str ); result = vips_buf_appends( buf, str );
handled = TRUE; handled = TRUE;
} }
else if( type == VIPS_TYPE_BLOB ) {
size_t str_len;
/* Binary data and not printable.
*/
(void) vips_value_get_ref_string( value, &str_len );
result = vips_buf_appendf( buf,
_( "%zd bytes of binary data" ), str_len );
handled = TRUE;
}
else if( type == VIPS_TYPE_ARRAY_DOUBLE ) { else if( type == VIPS_TYPE_ARRAY_DOUBLE ) {
double *arr; double *arr;
int n; int n;

View File

@ -966,7 +966,7 @@ vips_set_value_from_pointer( GValue *value, void *data )
else if( fundamental == G_TYPE_ENUM ) else if( fundamental == G_TYPE_ENUM )
g_value_set_enum( value, *((int *) data) ); g_value_set_enum( value, *((int *) data) );
else if( fundamental == G_TYPE_STRING ) else if( fundamental == G_TYPE_STRING )
g_value_set_string( value, ((char *) data) ); g_value_set_string( value, *((char **) data) );
else else
g_warning( "%s: unimplemented vips_set_value_from_pointer() " g_warning( "%s: unimplemented vips_set_value_from_pointer() "
"type %s", "type %s",
@ -1486,7 +1486,10 @@ vips_image_set_string( VipsImage *image, const char *field, const char *str )
* This function will read any field, returning it as a printable string. * This function will read any field, returning it as a printable string.
* You need to free the string with g_free() when you are done with it. * You need to free the string with g_free() when you are done with it.
* *
* See also: vips_image_get(), vips_image_get_typeof(). * This will base64-encode BLOBs, for example. Use vips_buf_appendgv() to
* make a string that's for humans.
*
* See also: vips_image_get(), vips_image_get_typeof(), vips_buf_appendgv().
* *
* Returns: 0 on success, -1 otherwise. * Returns: 0 on success, -1 otherwise.
*/ */

View File

@ -5,23 +5,28 @@ vipsheader \- prints information about an image file
vipsheader [OPTIONS ...] files ... vipsheader [OPTIONS ...] files ...
.SH DESCRIPTION .SH DESCRIPTION
.B vipsheader(1) .B vipsheader(1)
prints image header fields to stdout. prints image header fields to stdout.
.SH OPTIONS .SH OPTIONS
.TP
.B -a, --all
Show all fields. Fields are displayed to be convenient for humans to read, so
binary data, for example, is summarized rather than simply copied.
.TP .TP
.B -f FIELD, --field=FIELD .B -f FIELD, --field=FIELD
Print value of Print value of
.B FIELD .B FIELD
from image header. The special field name getext prints from image header. Fields are printed in a way suitable for programs to
the VIPS extension block: the XML defining the image metadata. You can alter understand, so, for example, binary data is base64-encoded and printed as a
this, then reattach with stream of characters.
.B vipsedit(1).
.TP The special field name
.B -a, --all .B getext
Show all fields. Normally prints the VIPS extension block: the XML defining the image metadata. You can
.B vipsheader alter this, then reattach with
just shows a one-line summary. .B vipsedit(1).
.SH EXAMPLES .SH EXAMPLES
$ vipsheader -f Xsize ~/pics/*.v $ vipsheader -f Xsize ~/pics/*.v

View File

@ -147,14 +147,11 @@ print_header( VipsImage *im, gboolean many )
else if( strcmp( main_option_field, "Hist" ) == 0 ) else if( strcmp( main_option_field, "Hist" ) == 0 )
printf( "%s", vips_image_get_history( im ) ); printf( "%s", vips_image_get_history( im ) );
else { else {
GValue value = { 0 }; char *str;
char str[256];
VipsBuf buf = VIPS_BUF_STATIC( str );
vips_image_get( im, main_option_field, &value ); vips_image_get_as_string( im, main_option_field, &str );
vips_buf_appendgv( &buf, &value );
printf( "%s\n", str ); printf( "%s\n", str );
g_value_unset( &value ); g_free( str );
} }
return( 0 ); return( 0 );