[svn-r15383] bug fix: when a requested palette index did not exist , the verbose printing message was referring to the default palette

tested: windows, linux
This commit is contained in:
Pedro Vicente Nunes 2008-07-17 09:47:07 -05:00
parent 86dff28679
commit d1596b2003

View File

@ -46,7 +46,7 @@ static struct long_options l_opts[] = {
{ "version", no_arg, 'V' },
{ "verbose", no_arg, 'v' },
{ "image", require_arg, 'i' },
{ "convert", no_arg, 'c' },
{ "convert", no_arg, 'c' },
{ "palette", require_arg, 'p' },
{ NULL, 0, '\0' }
};
@ -59,7 +59,7 @@ typedef struct
const char *template_name;
const char *image_name;
int image_type;
int convert_true;
int convert_true;
int idx_palette;
int verbose;
} h52jpeg_opt_t;
@ -68,10 +68,10 @@ typedef struct
/* prototypes */
static void usage(const char *prog);
static int h52jpeg(h52jpeg_opt_t opt);
static void make_jpeg_name( const char* template_name, const char* image_name, char* jpeg_name);
static void make_jpeg_name( const char* template_name, const char* image_name, char* jpeg_name);
static int do_object(hid_t fid, h52jpeg_opt_t opt, const char* image_name);
static int do_image(hid_t fid, h52jpeg_opt_t opt, const char* image_name, char* jpeg_name);
static void write_JPEG_file(char *filename, JSAMPLE *image_buffer, int image_height, int image_width, int planes);
static void write_JPEG_file(char *filename, JSAMPLE *image_buffer, int image_height, int image_width, int planes);
static void convert_to_true( hsize_t width, hsize_t height, unsigned char* ibuf, unsigned char* pbuf, unsigned char* tbuf);
/*-------------------------------------------------------------------------
@ -91,7 +91,7 @@ int main(int argc, const char *argv[])
int op;
/* initialze options to 0 */
memset(&opt,0,sizeof(h52jpeg_opt_t));
memset(&opt,0,sizeof(h52jpeg_opt_t));
/* parse command line options */
while ((op = get_option(argc, argv, s_opts, l_opts)) != EOF)
@ -114,9 +114,9 @@ int main(int argc, const char *argv[])
case 'c':
opt.convert_true = 1;
break;
case 'p':
opt.idx_palette = atoi(opt_arg);
break;
case 'p':
opt.idx_palette = atoi(opt_arg);
break;
} /* switch */
@ -165,7 +165,7 @@ static void usage(const char *prog)
printf(" -v, --verbose Verbose mode, print object information\n");
printf(" -V, --version Print HDF5 version number and exit\n");
printf(" -i, --image Image name (full path in HDF5 file)\n");
printf(" -c, --convert Convert image from graycolor to truecolor\n");
printf(" -c, --convert Convert image from graycolor to truecolor\n");
printf(" -p P, --palette=P Use HDF5 palette index P in conversion -c\n");
printf("\n");
@ -279,82 +279,82 @@ out:
return -1;
}
/*-------------------------------------------------------------------------
* Function: do_object
*
* Parameters: HDF5 file id, command line options, an object name
*
* Purpose: read HDF5 object, save jpeg image
*
* Return: 0, all is fine, -1 not all is fine
*
*-------------------------------------------------------------------------
*/
static
int do_object(hid_t fid, h52jpeg_opt_t opt, const char* object_name)
{
int done=0; /* return value from do_image */
char jpeg_name[1024];
/* build the jpeg file name */
make_jpeg_name( opt.template_name, object_name, jpeg_name);
if ( opt.verbose)
{
printf("%s ...", object_name );
}
/*-------------------------------------------------------------------------
* HDF5 Image
*-------------------------------------------------------------------------
*/
if ( H5IMis_image( fid, object_name ) )
{
/* read image, save jpeg image */
done = do_image(fid, opt, object_name, jpeg_name);
}
/*-------------------------------------------------------------------------
* HDF5 Image palette, ignore
*-------------------------------------------------------------------------
*/
else if ( H5IMis_palette( fid, object_name ) )
{
}
/*-------------------------------------------------------------------------
* regular dataset
*-------------------------------------------------------------------------
*/
else
{
} /* else */
if ( opt.verbose)
{
if ( done )
{
printf("saved to %s\n", jpeg_name );
}
else
{
printf("\n");
}
}
return 0;
/*-------------------------------------------------------------------------
* Function: do_object
*
* Parameters: HDF5 file id, command line options, an object name
*
* Purpose: read HDF5 object, save jpeg image
*
* Return: 0, all is fine, -1 not all is fine
*
*-------------------------------------------------------------------------
*/
static
int do_object(hid_t fid, h52jpeg_opt_t opt, const char* object_name)
{
int done=0; /* return value from do_image */
char jpeg_name[1024];
/* build the jpeg file name */
make_jpeg_name( opt.template_name, object_name, jpeg_name);
if ( opt.verbose)
{
printf("%s ...", object_name );
}
/*-------------------------------------------------------------------------
* HDF5 Image
*-------------------------------------------------------------------------
*/
if ( H5IMis_image( fid, object_name ) )
{
/* read image, save jpeg image */
done = do_image(fid, opt, object_name, jpeg_name);
}
/*-------------------------------------------------------------------------
* HDF5 Image palette, ignore
*-------------------------------------------------------------------------
*/
else if ( H5IMis_palette( fid, object_name ) )
{
}
/*-------------------------------------------------------------------------
* regular dataset
*-------------------------------------------------------------------------
*/
else
{
} /* else */
if ( opt.verbose)
{
if ( done )
{
printf("saved to %s\n", jpeg_name );
}
else
{
printf("\n");
}
}
return 0;
}
@ -378,7 +378,7 @@ int do_image(hid_t fid, h52jpeg_opt_t opt, const char* image_name, char* jpeg_na
char interlace[20];
hssize_t npals;
const char* name;
int done;
int done;
unsigned char* ibuf=NULL;
name = image_name;
@ -391,115 +391,115 @@ int do_image(hid_t fid, h52jpeg_opt_t opt, const char* image_name, char* jpeg_na
if (NULL == (ibuf = HDmalloc( (size_t)width * (size_t)height * (size_t)planes )))
goto out;
if ( H5IMread_image( fid, name, ibuf ) < 0 )
{
goto out;
}
/*-------------------------------------------------------------------------
* no conversion to true color requested or true color image, just save what we found
* this will result either in
*
* 24bit HDF5 ---> true color jpeg
* 8bit HDF5 ---> grey color jpeg
*
*-------------------------------------------------------------------------
*/
if ( planes == 3 || !opt.convert_true )
if ( H5IMread_image( fid, name, ibuf ) < 0 )
{
goto out;
}
/*-------------------------------------------------------------------------
* no conversion to true color requested or true color image, just save what we found
* this will result either in
*
* 24bit HDF5 ---> true color jpeg
* 8bit HDF5 ---> grey color jpeg
*
*-------------------------------------------------------------------------
*/
if ( planes == 3 || !opt.convert_true )
{
/* write the jpeg file */
write_JPEG_file (jpeg_name,
ibuf,
(int) height,
(int) width,
(int) planes);
done = 1;
}
/*-------------------------------------------------------------------------
* conversion to truecolor
* this will result in
*
* 8bit HDF5 ---> true color jpeg
*
*-------------------------------------------------------------------------
*/
else if (opt.convert_true && planes == 1 )
{
hsize_t pdims[2]; /* palette dimensions */
unsigned char *pbuf=NULL;/* palette array */
unsigned char *tbuf=NULL;/* true color array */
int ipal; /* palette to use */
if ( H5IMget_npalettes( fid, name, &npals ) < 0 )
{
goto out;
}
/*-------------------------------------------------------------------------
* there are palettes
*-------------------------------------------------------------------------
*/
if ( npals > 0 )
{
/* use either the default (0) palette or a requested one */
ipal = ( opt.idx_palette > 0 ) ? opt.idx_palette : 0;
/* the requested palette may not exist . use the default */
if ( opt.idx_palette >= npals )
{
ipal = 0;
if ( opt.verbose )
{
printf("palette index <%d> does not exist. Using default...",
ipal );
}
}
if ( H5IMget_palette_info( fid, name, ipal, pdims ) < 0 )
goto out;
if (NULL == (pbuf = HDmalloc( (size_t)pdims[0] * (size_t)pdims[1] )))
goto out;
if (NULL == (tbuf = HDmalloc( (size_t)width * (size_t)height * 3 )))
goto out;
if ( H5IMget_palette( fid, name, ipal, pbuf ) < 0 )
goto out;
/* convert indexed image to true color image */
convert_to_true(width, height, ibuf, pbuf, tbuf);
/* write the jpeg file */
write_JPEG_file (jpeg_name,
tbuf,
(int) height,
(int) width,
3);
done = 1;
free( pbuf );
free( tbuf );
pbuf = NULL;
tbuf = NULL;
}
/*-------------------------------------------------------------------------
* there are no palettes
*-------------------------------------------------------------------------
*/
else
{
done = 0;
if ( opt.verbose )
{
printf("image <%s> has no palette...", name );
}
} /* no palettes */
(int) planes);
done = 1;
}
/*-------------------------------------------------------------------------
* conversion to truecolor
* this will result in
*
* 8bit HDF5 ---> true color jpeg
*
*-------------------------------------------------------------------------
*/
else if (opt.convert_true && planes == 1 )
{
hsize_t pdims[2]; /* palette dimensions */
unsigned char *pbuf=NULL;/* palette array */
unsigned char *tbuf=NULL;/* true color array */
int ipal; /* palette to use */
if ( H5IMget_npalettes( fid, name, &npals ) < 0 )
{
goto out;
}
/*-------------------------------------------------------------------------
* there are palettes
*-------------------------------------------------------------------------
*/
if ( npals > 0 )
{
/* use either the default (0) palette or a requested one */
ipal = ( opt.idx_palette > 0 ) ? opt.idx_palette : 0;
/* the requested palette may not exist . use the default */
if ( opt.idx_palette >= npals )
{
ipal = 0;
if ( opt.verbose )
{
printf("palette index <%d> does not exist. Using default...",
opt.idx_palette );
}
}
if ( H5IMget_palette_info( fid, name, ipal, pdims ) < 0 )
goto out;
if (NULL == (pbuf = HDmalloc( (size_t)pdims[0] * (size_t)pdims[1] )))
goto out;
if (NULL == (tbuf = HDmalloc( (size_t)width * (size_t)height * 3 )))
goto out;
if ( H5IMget_palette( fid, name, ipal, pbuf ) < 0 )
goto out;
/* convert indexed image to true color image */
convert_to_true(width, height, ibuf, pbuf, tbuf);
/* write the jpeg file */
write_JPEG_file (jpeg_name,
tbuf,
(int) height,
(int) width,
3);
done = 1;
free( pbuf );
free( tbuf );
pbuf = NULL;
tbuf = NULL;
}
/*-------------------------------------------------------------------------
* there are no palettes
*-------------------------------------------------------------------------
*/
else
{
done = 0;
if ( opt.verbose )
{
printf("image <%s> has no palette...", name );
}
} /* no palettes */
} /* conversion to truecolor */
free( ibuf );
@ -559,46 +559,46 @@ void make_jpeg_name( const char* template_name, const char* image_name, char* jp
}
/*-------------------------------------------------------------------------
* Function: convert_to_true
*
* Parameters:
*
* Purpose: convert a greycolor buffer to a true color using a palette buffer
*
* Return:
*
*-------------------------------------------------------------------------
*/
static
void convert_to_true( hsize_t width, hsize_t height, unsigned char* ibuf, unsigned char* pbuf, unsigned char* tbuf)
{
hsize_t i, j;
for ( i = 0, j = 0; i < width * height; i++, j += 3)
{
unsigned char idx;
unsigned char r;
unsigned char g;
unsigned char b;
/* get the index from the grey image */
idx = ibuf[i];
/* get the RGB color */
r = pbuf[3*idx];
g = pbuf[3*idx+1];
b = pbuf[3*idx+2];
/* define the color buffer */
tbuf[j] = r;
tbuf[j+1] = g;
tbuf[j+2] = b;
}
}
/*-------------------------------------------------------------------------
* Function: convert_to_true
*
* Parameters:
*
* Purpose: convert a greycolor buffer to a true color using a palette buffer
*
* Return:
*
*-------------------------------------------------------------------------
*/
static
void convert_to_true( hsize_t width, hsize_t height, unsigned char* ibuf, unsigned char* pbuf, unsigned char* tbuf)
{
hsize_t i, j;
for ( i = 0, j = 0; i < width * height; i++, j += 3)
{
unsigned char idx;
unsigned char r;
unsigned char g;
unsigned char b;
/* get the index from the grey image */
idx = ibuf[i];
/* get the RGB color */
r = pbuf[3*idx];
g = pbuf[3*idx+1];
b = pbuf[3*idx+2];
/* define the color buffer */
tbuf[j] = r;
tbuf[j+1] = g;
tbuf[j+2] = b;
}
}
/*