ImageConverter.cpp
Go to the documentation of this file.
1 
7 #include "ImageConverter.h"
8 
9 #include <iostream>
10 
11 extern "C" {
12 #define XMD_H
13 #include <jpeglib.h>
14 }
15 #include <png.h>
16 
17 
18 using namespace std;
19 using namespace hrp;
20 
21 
22 
23 //==================================================================================================
33 //==================================================================================================
34 bool ImageConverter::initializeSFImage( )
35 {
36  image->width = 0;
37  image->height = 0;
38  image->numComponents = 0;
39  image->pixels.clear();
40 
41  return true;
42 }
43 
44 
45 
46 //==================================================================================================
59 //==================================================================================================
60 SFImage* ImageConverter::convert( string url )
61 {
62  // 拡張子抽出 //
63  string ext = url.substr( url.rfind( '.' ) );
64 
65  // 拡張子を小文字に変換 //
66  transform( ext.begin(), ext.end(), ext.begin(), (int(*)(int))tolower );
67 
68  // SFImageの作成 //
69  if( !ext.compare( ".png" ) )
70  {
71  if(loadPNG( url ))
72  return image;
73  }
74  else if( !ext.compare( ".jpg" ) )
75  {
76  if(loadJPEG( url ))
77  return image;
78  }
79  else
80  {
81  // cout << "ImageTexture read error: unsupported format." << '\n';
82  }
83 
84  image->height = 0;
85  image->width = 0;
86  image->numComponents = 0;
87  image->pixels.resize(0);
88  return image;
89 }
90 
91 
92 
93 //==================================================================================================
103 //==================================================================================================bool
104 bool
105 ImageConverter::loadPNG(
106  string & filePath
107 )
108 {
109  initializeSFImage( );
110 
111  FILE * fp;
112 
113  try
114  {
115  // ファイルオープン //
116  fp = fopen( filePath.c_str(), "rb" );
117  if( !fp ) throw "File open error.";
118 
119 
120  // シグネチャの確認 //
121  png_size_t number = 8;
122  png_byte header[8];
123  int is_png;
124 
125  fread( header, 1, number, fp );
126  is_png = !png_sig_cmp( header, 0, number );
127  if( !is_png ) throw "File is not png.";
128 
129 
130  // png_struct 構造体の初期化 //
131  png_structp pPng;
132 
133  pPng = png_create_read_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL );
134  if( !pPng ) throw "Failed to create png_struct";
135 
136 
137  // png_info 構造体の初期化 //
138  png_infop pInfo;
139 
140  pInfo = png_create_info_struct( pPng );
141  if( !pInfo )
142  {
143  png_destroy_read_struct( &pPng, NULL, NULL );
144  throw "Failed to create png_info";
145  }
146 
147 
148  // ファイルポインタのセットとシグネチャ確認のためポインタが進んでいることを通知 //
149  png_init_io( pPng, fp );
150  png_set_sig_bytes( pPng, (int)number );
151 
152  png_read_info(pPng, pInfo);
153  png_uint_32 width = png_get_image_width( pPng, pInfo );
154  png_uint_32 height = png_get_image_height( pPng, pInfo );
155  png_byte color_type = png_get_color_type( pPng, pInfo );
156  png_byte depth = png_get_bit_depth( pPng, pInfo );
157 
158  if (png_get_valid( pPng, pInfo, PNG_INFO_tRNS))
159  png_set_tRNS_to_alpha(pPng);
160  if (depth < 8)
161  png_set_packing(pPng);
162 
163  int numComponents;
164  switch( color_type )
165  {
166  case PNG_COLOR_TYPE_GRAY:
167  numComponents = 1;
168  if(depth < 8)
169 #if PNG_LIBPNG_VER_MINOR >= 4
171 #else
173 #endif
174  break;
175 
177  numComponents = 2;
178  if (depth == 16)
179  png_set_strip_16(pPng);
180  break;
181 
182  case PNG_COLOR_TYPE_RGB:
183  numComponents = 3;
184  if (depth == 16)
185  png_set_strip_16(pPng);
186  break;
187 
189  numComponents = 4;
190  if (depth == 16)
191  png_set_strip_16(pPng);
192  break;
193 
196  numComponents = 3;
197  break;
198 
199  default:
200  numComponents = 1;
201  // throw "Unsupported color type.";
202 
203  }
204 
205  png_read_update_info( pPng, pInfo );
206 
207  unsigned char** row_pointers;
209  png_uint_32 rowbytes = png_get_rowbytes(pPng, pInfo);
210  image->pixels.resize(rowbytes*height);
211  for(unsigned int i=0; i<height; i++)
212  row_pointers[i] = &(image->pixels[i*rowbytes]);
213 
215 
216  image->width = width;
217  image->height = height;
218  image->numComponents = numComponents;
219 
221  png_destroy_read_struct( &pPng, &pInfo, NULL );
222 
223  fclose(fp);
224 
225  }
226 
227  catch( char * str )
228  {
229  cout << "PNG read error: " << str << '\n';
230  if( fp ) fclose( fp );
231  return false;
232  }
233 
234  return true;
235 }
236 
237 
238 
239 //==================================================================================================
249 //==================================================================================================bool
250 bool
251 ImageConverter::loadJPEG(
252  string & filePath )
253 {
254  initializeSFImage( );
255 
256  FILE * fp;
257 
258  try
259  {
260  // File open
261  fp = fopen( filePath.c_str(), "rb" );
262  if( !fp ) throw "File open error.";
263 
264 
265  struct jpeg_decompress_struct cinfo;
266  struct jpeg_error_mgr jerr;
267 
268  // Step 1: allocate and initialize JPEG decompression object
269  cinfo.err = jpeg_std_error( &jerr );
270  jpeg_create_decompress( &cinfo );
271 
272 
273  // Step 2: specify data source (eg, a file)
274  jpeg_stdio_src( &cinfo, fp );
275 
276  // Step 3: read file parameters with jpeg_read_header()
277  (void)jpeg_read_header( &cinfo, TRUE );
278 
279 
280  // Step 4: set parameters for decompression
281 
282 
283  // Step 5: Start decompression
284  (void)jpeg_start_decompress( &cinfo );
285 
286 
287  // get image attribute
288  image->width = cinfo.output_width;
289  image->height = cinfo.output_height;
290  image->numComponents = cinfo.out_color_components;
291 
293  row_pointers = (JSAMPARRAY)malloc( sizeof( JSAMPROW ) * image->height );
294  image->pixels.resize(cinfo.output_components * image->width * image->height);
295  for (int i = 0; i < image->height; i++ )
296  row_pointers[i] = &(image->pixels[i * cinfo.output_components * image->width]);
297 
298  while( cinfo.output_scanline < cinfo.output_height ) {
299  jpeg_read_scanlines( &cinfo,
301  cinfo.output_height - cinfo.output_scanline
302  );
303  }
304 
306 
307  // Step 7: Finish decompression
308  (void)jpeg_finish_decompress( &cinfo );
309 
310 
311  // Step 8: Release JPEG decompression object
312  jpeg_destroy_decompress( &cinfo );
313 
314 
315  fclose( fp );
316  }
317 
318  catch( char * str )
319  {
320  cout << "JPEG read error: " << str << '\n';
321  if( fp ) fclose( fp );
322  return false;
323  }
324 
325  return true;
326 }
png_infop
png_info FAR * png_infop
Definition: png.h:981
ImageConverter.h
Header file of Image Converter class.
png_get_rowbytes
png_uint_32 PNGAPI png_get_rowbytes(png_structp png_ptr, png_infop info_ptr)
Definition: pngget.c:25
png_destroy_read_struct
void PNGAPI png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr, png_infopp end_info_ptr_ptr)
Definition: pngread.c:1080
png_get_color_type
png_byte PNGAPI png_get_color_type(png_structp png_ptr, png_infop info_ptr)
Definition: pngget.c:77
width
png_infop png_uint_32 * width
Definition: png.h:2306
i
png_uint_32 i
Definition: png.h:2732
jpeglib.h
jpeg_decompress_struct::output_scanline
JDIMENSION output_scanline
Definition: jpeglib.h:489
jpeg_std_error
jpeg_std_error(struct jpeg_error_mgr *err)
Definition: jerror.c:231
malloc
char * malloc()
PNG_INFO_tRNS
#define PNG_INFO_tRNS
Definition: png.h:1068
png_bytep
png_byte FAR * png_bytep
Definition: pngconf.h:1195
png_get_image_width
png_uint_32 PNGAPI png_get_image_width(png_structp png_ptr, png_infop info_ptr)
Definition: pngget.c:47
jpeg_finish_decompress
jpeg_finish_decompress(j_decompress_ptr cinfo)
Definition: jdapimin.c:369
jpeg_read_scanlines
jpeg_read_scanlines(j_decompress_ptr cinfo, JSAMPARRAY scanlines, JDIMENSION max_lines)
Definition: jdapistd.c:152
jpeg_decompress_struct::output_width
JDIMENSION output_width
Definition: jpeglib.h:459
jpeg_error_mgr
Definition: jpeglib.h:643
hrp
Definition: ColdetModel.h:28
jpeg_create_decompress
#define jpeg_create_decompress(cinfo)
Definition: jpeglib.h:897
png_set_expand_gray_1_2_4_to_8
void PNGAPI png_set_expand_gray_1_2_4_to_8(png_structp png_ptr)
Definition: pngrtran.c:584
PNG_COLOR_TYPE_RGB_ALPHA
#define PNG_COLOR_TYPE_RGB_ALPHA
Definition: png.h:1003
jpeg_decompress_struct::output_height
JDIMENSION output_height
Definition: jpeglib.h:460
png_create_info_struct
png_infop PNGAPI png_create_info_struct(png_structp png_ptr)
Definition: png.c:239
png_set_tRNS_to_alpha
void PNGAPI png_set_tRNS_to_alpha(png_structp png_ptr)
Definition: pngrtran.c:608
row_pointers
png_infop png_bytepp row_pointers
Definition: png.h:2170
png_bytepp
png_byte FAR *FAR * png_bytepp
Definition: pngconf.h:1217
PNG_COLOR_TYPE_RGB
#define PNG_COLOR_TYPE_RGB
Definition: png.h:1002
png_set_sig_bytes
void PNGAPI png_set_sig_bytes(png_structp png_ptr, int num_bytes)
Definition: png.c:93
png_byte
unsigned char png_byte
Definition: pngconf.h:1118
jpeg_decompress_struct::output_components
int output_components
Definition: jpeglib.h:462
png_sig_cmp
int PNGAPI png_sig_cmp(png_bytep sig, png_size_t start, png_size_t num_to_check)
Definition: png.c:112
png_set_strip_16
void PNGAPI png_set_strip_16(png_structp png_ptr)
Definition: pngrtran.c:103
PNG_COLOR_TYPE_GRAY
#define PNG_COLOR_TYPE_GRAY
Definition: png.h:1000
png_read_info
void PNGAPI png_read_info(png_structp png_ptr, png_infop info_ptr)
Definition: pngread.c:327
image
png_bytepp image
Definition: png.h:1769
png_structp
png_struct FAR * png_structp
Definition: png.h:1105
png_get_valid
png_uint_32 PNGAPI png_get_valid(png_structp png_ptr, png_infop info_ptr, png_uint_32 flag)
Definition: pngget.c:16
png_init_io
void PNGAPI png_init_io(png_structp png_ptr, png_FILE_p fp)
Definition: png.c:633
png_set_palette_to_rgb
void PNGAPI png_set_palette_to_rgb(png_structp png_ptr)
Definition: pngrtran.c:573
png_get_image_height
png_uint_32 PNGAPI png_get_image_height(png_structp png_ptr, png_infop info_ptr)
Definition: pngget.c:57
png_create_read_struct
png_structp PNGAPI png_create_read_struct(png_const_charp user_png_ver, png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warn_fn)
Definition: pngread.c:20
hrp::SFImage
Definition: VrmlNodes.h:61
TRUE
#define TRUE
Definition: OPC_IceHook.h:13
PNG_LIBPNG_VER_STRING
#define PNG_LIBPNG_VER_STRING
Definition: png.h:413
png_get_bit_depth
png_byte PNGAPI png_get_bit_depth(png_structp png_ptr, png_infop info_ptr)
Definition: pngget.c:67
jpeg_decompress_struct::out_color_components
int out_color_components
Definition: jpeglib.h:461
png_size_t
size_t png_size_t
Definition: pngconf.h:1126
jpeg_destroy_decompress
jpeg_destroy_decompress(j_decompress_ptr cinfo)
Definition: jdapimin.c:91
void
typedef void(PNGAPI *png_error_ptr) PNGARG((png_structp
png_uint_32
unsigned long png_uint_32
Definition: pngconf.h:1114
height
png_infop png_uint_32 png_uint_32 * height
Definition: png.h:2306
free
int free()
JSAMPARRAY
JSAMPROW * JSAMPARRAY
Definition: jpeglib.h:67
png_set_packing
void PNGAPI png_set_packing(png_structp png_ptr)
Definition: pngtrans.c:41
png.h
JSAMPROW
JSAMPLE FAR * JSAMPROW
Definition: jpeglib.h:66
jpeg_stdio_src
jpeg_stdio_src(j_decompress_ptr cinfo, FILE *infile)
Definition: jdatasrc.c:182
color_type
png_infop png_uint_32 png_uint_32 int int * color_type
Definition: png.h:2307
PNG_COLOR_TYPE_GRAY_ALPHA
#define PNG_COLOR_TYPE_GRAY_ALPHA
Definition: png.h:1004
png_set_gray_1_2_4_to_8
void PNGAPI png_set_gray_1_2_4_to_8(png_structp png_ptr)
Definition: pngrtran.c:597
fp
png_FILE_p fp
Definition: png.h:1948
png_read_image
void PNGAPI png_read_image(png_structp png_ptr, png_bytepp image)
Definition: pngread.c:868
jpeg_read_header
jpeg_read_header(j_decompress_ptr cinfo, boolean require_image)
Definition: jdapimin.c:241
jpeg_decompress_struct
Definition: jpeglib.h:411
PNG_COLOR_TYPE_PALETTE
#define PNG_COLOR_TYPE_PALETTE
Definition: png.h:1001
png_read_update_info
void PNGAPI png_read_update_info(png_structp png_ptr, png_infop info_ptr)
Definition: pngread.c:534


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Wed Sep 7 2022 02:51:03