Move the sources to trunk
[opencv] / otherlibs / VlGrFmts / vlgrfmts.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
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.
8 //
9 //
10 //                        Intel License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
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.
25 //
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.
28 //
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.
39 //
40 //M*/
41
42 #include <windows.h>
43
44 #include "vlgrfmts.h"
45 #include "grfmt_base.h"
46 #include "grfmt_bmp.h"
47 #include "grfmt_jpeg.h"
48 #include "grfmt_pxm.h"
49
50 static GrFmtReadersList* fmts_list = 0;
51
52 static void InitObjects()
53 {
54     if( fmts_list == 0 )
55     {
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() );
61     }
62 }
63
64
65 BOOL APIENTRY DllMain( HANDLE hModule, 
66                        DWORD  ul_reason_for_call, 
67                        LPVOID lpReserved )
68 {
69     hModule, lpReserved, ul_reason_for_call; // unref. params
70     switch (ul_reason_for_call)
71     {
72         case DLL_PROCESS_ATTACH:
73             InitObjects();
74             break;
75         case DLL_PROCESS_DETACH:
76         case DLL_THREAD_ATTACH:
77         case DLL_THREAD_DETACH:
78             break;
79     }
80     return TRUE;
81 }
82
83 ///////////////// API ///////////////////
84
85 DLL_API int gr_fmt_find_filter( const char* file_name )
86 {
87     GrFmtReader* filter = fmts_list->FindReader( file_name );
88     if( filter )
89     {
90         filter->SetFile( file_name );
91     }
92     return (int)filter;
93 }
94
95
96 DLL_API int gr_fmt_read_header( int filter, int* width, int* height, int* color )
97 {
98     GrFmtReader* reader = (GrFmtReader*)filter;
99     int result = 0;
100
101     if( filter != 0 && width != 0 && height != 0 && reader->ReadHeader())
102     {
103         *width  = reader->GetWidth();
104         *height = reader->GetHeight();
105         if( color ) *color  = reader->GetColor();
106         result = 1;
107     }
108
109     return result;
110 }
111
112
113 DLL_API int gr_fmt_read_data( int filter, void* data, int step, int color )
114 {
115     GrFmtReader* reader = (GrFmtReader*)filter;
116     int result = 0;
117
118     if( filter != 0 && data != 0 && step != 0 && (color&~1) == 0 ||
119         step >= ((reader->GetWidth()*(color ? 3 : 1) + 3) & -4))
120     {
121         result = reader->ReadData( (unsigned char*)data, step, color );
122     }
123
124     return result;
125 }
126
127 DLL_API int gr_fmt_close_filter( int filter )
128 {
129     if( filter != 0 ) ((GrFmtReader*)filter)->Close();
130     return 1;
131 }
132
133
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 )
137 {
138     int result = 0;
139     FILE* f = 0;
140     int bpp = color ? 24 : 8;
141     int dst_pitch = (width*(bpp/8) + 3) & -4;
142
143     if( strcmp( format, "bmp" ) && strcmp( format, "BMP" )) return 0;
144
145     if( !src_data || width <= 0 || height <= 0 || (color&~1) ||
146         dst_pitch > src_pitch || !filename ) return 0;
147     
148     f = fopen( filename, "wb" );
149     if( f )
150     {
151         int bitmap_header_size = 40;
152         int palette_size = color ? 0 : 1024;
153         int header_size = 14 /* fileheader */ + bitmap_header_size +
154                           palette_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 };
161         int i;
162
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 );
166
167         if( !color )
168         {
169             for( i = 0; i < 256; i++ )
170             {
171                 palette[i*4] = palette[i*4 + 1] = palette[i*4 + 2] = (unsigned char)i;
172                 palette[i*4 + 3] = 0;
173             }
174             fwrite( palette, 1, sizeof(palette), f );
175         }
176
177         data += src_pitch*(height - 1);
178         for( i = height; i > 0; i--, data -= src_pitch )
179         {
180             fwrite( data, 1, dst_pitch, f );
181         }
182         fclose(f);
183         result = 1;
184     }
185     return result;
186 }