Debian Codegraph @

src/thirdparty/PoissonRecon/PNG.inl

raw
1 /*
2 Copyright (c) 2010, Michael Kazhdan
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8 Redistributions of source code must retain the above copyright notice, this list of
9 conditions and the following disclaimer. Redistributions in binary form must reproduce
10 the above copyright notice, this list of conditions and the following disclaimer
11 in the documentation and/or other materials provided with the distribution.
12
13 Neither the name of the Johns Hopkins University nor the names of its contributors
14 may be used to endorse or promote products derived from this software without specific
15 prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
18 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
20 SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26 DAMAGE.
27 */
28
29 inline PNGReader::PNGReader( const char* fileName , unsigned int& width , unsigned int& height , unsigned int& channels )
30 {
31 _currentRow = 0;
32
33 _png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING , 0 , 0 , 0);
34 if( !_png_ptr ) MK_THROW( "Failed to create png pointer" );
35 _info_ptr = png_create_info_struct( _png_ptr );
36 if( !_info_ptr ) MK_THROW( "Failed to create info pointer" );
37
38 _end_info = png_create_info_struct( _png_ptr );
39 if( !_end_info ) MK_THROW( "Failed to create end pointer" );
40
41
42 _fp = fopen( fileName , "rb" );
43 if( !_fp ) MK_THROW( "Failed to open file for reading: " , fileName );
44 png_init_io( _png_ptr , _fp );
45
46 png_read_info( _png_ptr, _info_ptr );
47
48 width = png_get_image_width( _png_ptr , _info_ptr );
49 height = png_get_image_height( _png_ptr, _info_ptr );
50 channels = png_get_channels( _png_ptr , _info_ptr );
51 int bit_depth=png_get_bit_depth( _png_ptr , _info_ptr );
52 int color_type = png_get_color_type( _png_ptr , _info_ptr );
53 if( bit_depth==16 )
54 {
55 MK_WARN( "Converting 16-bit image to 8-bit image" );
56 _scratchRow = new unsigned char[ channels*width*2 ];
57 }
58 else
59 {
60 if( bit_depth!=8 ) MK_THROW( "Expected 8 bits per channel" );
61 _scratchRow = NULL;
62 }
63 if( color_type==PNG_COLOR_TYPE_PALETTE ) png_set_expand( _png_ptr ) , printf( "Expanding PNG color pallette\n" );
64
65 {
66 long int a = 1;
67 int swap = (*((unsigned char *) &a) == 1);
68 if( swap ) png_set_swap( _png_ptr );
69 }
70 }
71 inline unsigned int PNGReader::nextRow( unsigned char* row )
72 {
73 if( _scratchRow )
74 {
75 int width = png_get_image_width( _png_ptr , _info_ptr );
76 int channels = png_get_channels( _png_ptr , _info_ptr );
77
78 png_read_row( _png_ptr , (png_bytep)_scratchRow , NULL );
79 #pragma omp parallel for
80 for( int i=0 ; i<width*channels ; i++ ) row[i] = _scratchRow[2*i];
81 }
82 else png_read_row( _png_ptr , (png_bytep)row , NULL );
83 return _currentRow++;
84 }
85
86 inline PNGReader::~PNGReader( void )
87 {
88 if( _scratchRow ) delete[] _scratchRow;
89 _scratchRow = NULL;
90 png_destroy_read_struct( &_png_ptr , &_info_ptr , &_end_info );
91 }
92
93 inline bool PNGReader::GetInfo( const char* fileName , unsigned int& width , unsigned int& height , unsigned int& channels )
94 {
95 png_structp png_ptr;
96 png_infop info_ptr;
97 png_infop end_info ;
98 FILE* fp;
99
100 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING , 0 , 0 , 0);
101 if( !png_ptr ) MK_THROW( "Failed to create png pointer" );
102 info_ptr = png_create_info_struct( png_ptr );
103 if( !info_ptr ) MK_THROW( "Failed to create info pointer" );
104 end_info = png_create_info_struct( png_ptr );
105 if( !end_info ) MK_THROW( "Failed to create end pointer" );
106
107 fp = fopen( fileName , "rb" );
108 if( !fp ) MK_THROW( "Failed to open file for reading: " , fileName );
109 png_init_io( png_ptr , fp );
110
111 png_read_info( png_ptr, info_ptr );
112
113 width = png_get_image_width( png_ptr , info_ptr );
114 height = png_get_image_height( png_ptr, info_ptr );
115 channels = png_get_channels( png_ptr , info_ptr );
116
117 png_destroy_read_struct( &png_ptr , &info_ptr , &end_info );
118 fclose( fp );
119 return true;
120 }
121
122 inline PNGWriter::PNGWriter( const char* fileName , unsigned int width , unsigned int height , unsigned int channels , unsigned int quality )
123 {
124 _currentRow = 0;
125
126 _png_ptr = png_create_write_struct( PNG_LIBPNG_VER_STRING , 0 , 0 , 0 );
127 if( !_png_ptr ) MK_THROW( "Failed to create png write struct" );
128 _info_ptr = png_create_info_struct( _png_ptr );
129 if( !_info_ptr ) MK_THROW( "Failed to create png info struct");
130
131 _fp = fopen( fileName , "wb" );
132 if( !_fp ) MK_THROW( "Failed to open file for writing: %s" , fileName );
133 png_init_io( _png_ptr , _fp );
134
135 png_set_compression_level( _png_ptr , Z_BEST_SPEED );
136
137 int pngColorType;
138 switch( channels )
139 {
140 case 1: pngColorType = PNG_COLOR_TYPE_GRAY ; break;
141 case 3: pngColorType = PNG_COLOR_TYPE_RGB ; break;
142 case 4: pngColorType = PNG_COLOR_TYPE_RGBA ; break;
143 default: MK_THROW( "Only 1, 3, or 4 channel PNGs are supported" );
144 };
145 png_set_IHDR( _png_ptr , _info_ptr, width , height, 8 , pngColorType , PNG_INTERLACE_NONE , PNG_COMPRESSION_TYPE_DEFAULT , PNG_FILTER_TYPE_DEFAULT );
146 png_write_info( _png_ptr , _info_ptr );
147
148 {
149 long int a = 1;
150 int swap = (*((unsigned char *) &a) == 1);
151 if( swap ) png_set_swap( _png_ptr );
152 }
153 }
154 inline PNGWriter::~PNGWriter( void )
155 {
156 png_write_end( _png_ptr , NULL );
157 png_destroy_write_struct( &_png_ptr , &_info_ptr );
158 fclose( _fp );
159 }
160 inline unsigned int PNGWriter::nextRow( const unsigned char* row )
161 {
162 png_write_row( _png_ptr , (png_bytep)row );
163 return _currentRow++;
164 }
165 inline unsigned int PNGWriter::nextRows( const unsigned char* rows , unsigned int rowNum )
166 {
167 for( unsigned int r=0 ; r<rowNum ; r++ ) png_write_row( _png_ptr , (png_bytep)( rows + r * _channels * sizeof( unsigned char ) * png_get_image_width( _png_ptr , _info_ptr ) ) );
168 return _currentRow += rowNum;
169 }