00001
#include "config.h"
00002
00003
#include <volume_io/internal_volume_io.h>
00004
#include <bicpl/objects.h>
00005
#include <bicpl/images.h>
00006
00007
#include <image.h>
00008
00009
00010
static void error_function(
00011
char error[] )
00012 {
00013 print_error( error );
00014 }
00015
00016
00017
extern unsigned short *
ibufalloc(IMAGE *image);
00018
00019
extern IMAGE *
iopen(
char *file,
char *mode,
unsigned int type,
00020
unsigned int dim,
unsigned int xsize,
00021
unsigned int ysize,
unsigned int zsize );
00022
00023
extern int getrow(IMAGE *image,
unsigned short *buffer,
00024
unsigned int y,
unsigned int z);
00025
00026
extern int iclose(IMAGE *image);
00027
00028
extern int putrow(IMAGE *image,
unsigned short *buffer,
00029
unsigned int y,
unsigned int z);
00030
00031
extern int i_seterror(
void (*func)() );
00032
00033
00034
00035 public Status
input_rgb_file(
00036 STRING filename,
00037
pixels_struct *pixels )
00038 {
00039 IMAGE *iimage;
00040
int x_size, y_size, z_size;
00041
int x,
y;
00042
int r, g, b, a;
00043
unsigned short rbuf[8192];
00044
unsigned short gbuf[8192];
00045
unsigned short bbuf[8192];
00046
unsigned short abuf[8192];
00047
00048
i_seterror( error_function );
00049
00050
if( (iimage =
iopen(filename,
"r",0,0,0,0,0)) == NULL )
00051 {
00052
return( ERROR );
00053 }
00054
00055 x_size = (
int) iimage->xsize;
00056 y_size = (
int) iimage->ysize;
00057 z_size = (
int) iimage->zsize;
00058
00059
initialize_pixels( pixels, 0, 0, x_size, y_size, 1.0, 1.0,
RGB_PIXEL );
00060
00061
if( z_size != 3 && z_size != 4 )
00062 {
00063 print_error(
"Error: z_size (%d) != 3 or 4\n", z_size );
00064
return( ERROR );
00065 }
00066
00067 for_less(
y, 0, y_size )
00068 {
00069
getrow( iimage, rbuf, (
unsigned int)
y, 0 );
00070
getrow( iimage, gbuf, (
unsigned int)
y, 1 );
00071
getrow( iimage, bbuf, (
unsigned int)
y, 2 );
00072
if( z_size == 4 )
00073
getrow( iimage, abuf, (
unsigned int)
y, 3 );
00074
00075 for_less( x, 0, x_size )
00076 {
00077 r = (
int) rbuf[x];
00078 g = (
int) gbuf[x];
00079 b = (
int) bbuf[x];
00080
if( z_size == 4 )
00081 a = (
int) abuf[x];
00082
else
00083 a = 255;
00084
00085
PIXEL_RGB_COLOUR( *pixels, x,
y ) = make_rgba_Colour( r, g, b, a );
00086 }
00087 }
00088
00089
iclose(iimage);
00090
return( OK );
00091 }
00092
00093 public Status
output_rgb_file(
00094 STRING filename,
00095
pixels_struct *pixels )
00096 {
00097 IMAGE *oimage;
00098
int x,
y, n_components;
00099 Colour col;
00100
unsigned short rbuf[8192];
00101
unsigned short gbuf[8192];
00102
unsigned short bbuf[8192];
00103
unsigned short abuf[8192];
00104
00105
i_seterror( error_function );
00106
00107
if( !file_directory_exists( filename ) )
00108 {
00109 print_error(
"Error: output file directory does not exist: %s\n",
00110 filename );
00111
return( ERROR );
00112 }
00113
00114 n_components = 3;
00115 for_less(
y, 0, pixels->
y_size )
00116 {
00117 for_less( x, 0, pixels->
x_size )
00118 {
00119 col =
PIXEL_RGB_COLOUR( *pixels, x,
y );
00120
if( get_Colour_a( col ) != 255 )
00121 {
00122 n_components = 4;
00123
break;
00124 }
00125 }
00126
00127
if( n_components == 4 )
00128
break;
00129 }
00130
00131 oimage =
iopen( filename,
"w", RLE(1), 3, (
unsigned int) pixels->
x_size,
00132 (
unsigned int) pixels->
y_size,
00133 (
unsigned int) n_components );
00134
00135
if( oimage == NULL )
00136 {
00137
return( ERROR );
00138 }
00139
00140 for_less(
y, 0, pixels->
y_size )
00141 {
00142 for_less( x, 0, pixels->
x_size )
00143 {
00144 col =
PIXEL_RGB_COLOUR( *pixels, x,
y );
00145 rbuf[x] = (
unsigned short) get_Colour_r( col );
00146 gbuf[x] = (
unsigned short) get_Colour_g( col );
00147 bbuf[x] = (
unsigned short) get_Colour_b( col );
00148
if( n_components == 4 )
00149 abuf[x] = (
unsigned short) get_Colour_a( col );
00150 }
00151
00152
putrow( oimage, rbuf, (
unsigned int)
y, 0 );
00153
putrow( oimage, gbuf, (
unsigned int)
y, 1 );
00154
putrow( oimage, bbuf, (
unsigned int)
y, 2 );
00155
if( n_components == 4 )
00156
putrow( oimage, abuf, (
unsigned int)
y, 3 );
00157 }
00158
00159
iclose(oimage);
00160
return( OK );
00161 }
00162