1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of Intel Corporation may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
45 #include "grfmt_base.h"
46 #include "grfmt_bmp.h"
47 #include "grfmt_jpeg.h"
48 #include "grfmt_pxm.h"
50 static GrFmtReadersList* fmts_list = 0;
52 static void InitObjects()
56 fmts_list = new GrFmtReadersList;
57 fmts_list->AddReader( new GrFmtBmpReader() );
58 fmts_list->AddReader( new GrFmtSunRasterReader() );
59 fmts_list->AddReader( new GrFmtJpegReader() );
60 fmts_list->AddReader( new GrFmtJpegReader() );
65 BOOL APIENTRY DllMain( HANDLE hModule,
66 DWORD ul_reason_for_call,
69 hModule, lpReserved, ul_reason_for_call; // unref. params
70 switch (ul_reason_for_call)
72 case DLL_PROCESS_ATTACH:
75 case DLL_PROCESS_DETACH:
76 case DLL_THREAD_ATTACH:
77 case DLL_THREAD_DETACH:
83 ///////////////// API ///////////////////
85 DLL_API int gr_fmt_find_filter( const char* file_name )
87 GrFmtReader* filter = fmts_list->FindReader( file_name );
90 filter->SetFile( file_name );
96 DLL_API int gr_fmt_read_header( int filter, int* width, int* height, int* color )
98 GrFmtReader* reader = (GrFmtReader*)filter;
101 if( filter != 0 && width != 0 && height != 0 && reader->ReadHeader())
103 *width = reader->GetWidth();
104 *height = reader->GetHeight();
105 if( color ) *color = reader->GetColor();
113 DLL_API int gr_fmt_read_data( int filter, void* data, int step, int color )
115 GrFmtReader* reader = (GrFmtReader*)filter;
118 if( filter != 0 && data != 0 && step != 0 && (color&~1) == 0 ||
119 step >= ((reader->GetWidth()*(color ? 3 : 1) + 3) & -4))
121 result = reader->ReadData( (unsigned char*)data, step, color );
127 DLL_API int gr_fmt_close_filter( int filter )
129 if( filter != 0 ) ((GrFmtReader*)filter)->Close();
134 DLL_API int gr_fmt_write_image( void* src_data, int src_pitch,
135 int width, int height, int color,
136 const char* filename, const char* format )
140 int bpp = color ? 24 : 8;
141 int dst_pitch = (width*(bpp/8) + 3) & -4;
143 if( strcmp( format, "bmp" ) && strcmp( format, "BMP" )) return 0;
145 if( !src_data || width <= 0 || height <= 0 || (color&~1) ||
146 dst_pitch > src_pitch || !filename ) return 0;
148 f = fopen( filename, "wb" );
151 int bitmap_header_size = 40;
152 int palette_size = color ? 0 : 1024;
153 int header_size = 14 /* fileheader */ + bitmap_header_size +
155 char signature[2] = { 'B', 'M' };
156 unsigned char palette[256*4];
157 char* data = (char*)src_data;
158 int file_header[] = { dst_pitch*height + header_size, 0, header_size };
159 int bitmap_header[] = { bitmap_header_size, width, height,
160 1|(bpp << 16), 0, 0, 0, 0, 0, 0 };
163 fwrite( signature, 1, sizeof(signature), f );
164 fwrite( file_header, 1, sizeof(file_header), f );
165 fwrite( bitmap_header, 1, bitmap_header_size, f );
169 for( i = 0; i < 256; i++ )
171 palette[i*4] = palette[i*4 + 1] = palette[i*4 + 2] = (unsigned char)i;
172 palette[i*4 + 3] = 0;
174 fwrite( palette, 1, sizeof(palette), f );
177 data += src_pitch*(height - 1);
178 for( i = height; i > 0; i--, data -= src_pitch )
180 fwrite( data, 1, dst_pitch, f );