Debian Codegraph @

src/thirdparty/PoissonRecon/JPEG.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 METHODDEF( void )
30 my_error_exit (j_common_ptr cinfo)
31 {
32 // cinfo->err really points to a my_error_mgr struct, so coerce pointer
33 my_error_ptr myerr = (my_error_ptr) cinfo->err;
34
35 // Always display the message.
36 // We could postpone this until after returning, if we chose.
37 (*cinfo->err->output_message) (cinfo);
38
39 // Return control to the setjmp point
40 longjmp(myerr->setjmp_buffer, 1);
41 }
42
43 inline bool JPEGReader::GetInfo( const char* fileName , unsigned int& width , unsigned int& height , unsigned int& channels )
44 {
45 FILE* fp = fopen( fileName , "rb" );
46 if( !fp ) MK_THROW( "Failed to open: " , fileName );
47
48 struct jpeg_decompress_struct cInfo;
49 struct my_error_mgr jErr;
50
51 cInfo.err = jpeg_std_error( &jErr.pub );
52 jErr.pub.error_exit = my_error_exit;
53 if( setjmp( jErr.setjmp_buffer ) )
54 {
55 jpeg_destroy_decompress( &cInfo );
56 MK_THROW( "JPEG error occured" );
57 }
58
59 jpeg_create_decompress( &cInfo );
60 jpeg_stdio_src( &cInfo , fp );
61
62 (void) jpeg_read_header( &cInfo , TRUE );
63
64 channels = cInfo.num_components;
65 width = cInfo.image_width;
66 height = cInfo.image_height;
67 jpeg_destroy_decompress( &cInfo );
68
69 fclose( fp );
70 return true;
71 }
72
73 inline JPEGReader::JPEGReader( const char* fileName , unsigned int& width , unsigned int& height , unsigned int& channels )
74 {
75 _currentRow = 0;
76 _fp = fopen( fileName , "rb" );
77 if( !_fp ) MK_THROW( "Failed to open: " , fileName );
78
79 _cInfo.err = jpeg_std_error( &_jErr.pub );
80 _jErr.pub.error_exit = my_error_exit;
81 if( setjmp( _jErr.setjmp_buffer ) )
82 {
83 jpeg_destroy_decompress( &_cInfo );
84 MK_THROW( "JPEG error occured" );
85 }
86
87 jpeg_create_decompress( &_cInfo );
88 jpeg_stdio_src( &_cInfo , _fp );
89
90 (void) jpeg_read_header( &_cInfo , TRUE );
91 (void) jpeg_start_decompress( &_cInfo );
92
93 channels = _cInfo.output_components;
94 width = _cInfo.output_width;
95 height = _cInfo.output_height;
96 }
97 inline JPEGReader::~JPEGReader( void )
98 {
99 (void) jpeg_finish_decompress( &_cInfo );
100 jpeg_destroy_decompress( &_cInfo );
101 fclose( _fp );
102 }
103 inline unsigned int JPEGReader::nextRow( unsigned char* row )
104 {
105 JSAMPROW row_pointers[1];
106 row_pointers[0] = row;
107 jpeg_read_scanlines( &_cInfo , row_pointers, 1 );
108 return _currentRow++;
109 }
110
111 inline JPEGWriter::JPEGWriter( const char* fileName , unsigned int width , unsigned int height , unsigned int channels , unsigned int quality )
112 {
113 _currentRow = 0;
114 _fp = fopen( fileName , "wb" );
115 if( !_fp ) MK_THROW( "Failed to open: " , fileName );
116
117 _cInfo.err = jpeg_std_error( &_jErr.pub );
118 jpeg_create_compress( &_cInfo );
119
120 jpeg_stdio_dest( &_cInfo , _fp );
121
122 _cInfo.image_width = width;
123 _cInfo.image_height = height;
124 _cInfo.input_components = channels;
125 _cInfo.in_color_space = JCS_RGB; /* colorspace of input image */
126
127 jpeg_set_defaults( &_cInfo );
128 jpeg_set_quality( &_cInfo , quality , TRUE );
129
130 jpeg_start_compress( &_cInfo , TRUE );
131 }
132 inline JPEGWriter::~JPEGWriter( void )
133 {
134 jpeg_finish_compress( &_cInfo );
135 jpeg_destroy_compress( &_cInfo );
136 fclose( _fp );
137 }
138 inline unsigned int JPEGWriter::nextRow( const unsigned char* row )
139 {
140 JSAMPROW row_pointer[1];
141 row_pointer[0] = ( unsigned char* )row;
142 (void) jpeg_write_scanlines( &_cInfo , row_pointer , 1 );
143 return _currentRow++;
144 }
145
146 inline unsigned int JPEGWriter::nextRows( const unsigned char* rows , unsigned int rowNum )
147 {
148 JSAMPROW* row_pointers = new JSAMPROW[ rowNum ];
149 for( unsigned int r=0 ; r<rowNum ; r++ ) row_pointers[r] = (unsigned char*)( rows + r * 3 * sizeof( unsigned char ) * _cInfo.image_width );
150 (void) jpeg_write_scanlines( &_cInfo , row_pointers , rowNum );
151 delete[] row_pointers;
152 return _currentRow += rowNum;
153 }
154