File: bmwmf.c

package info (click to toggle)
ted 2.16-5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 13,944 kB
  • ctags: 20,273
  • sloc: ansic: 167,980; makefile: 12,518; sh: 263
file content (261 lines) | stat: -rw-r--r-- 7,257 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#   include	"bitmapConfig.h"

#   include	<stdlib.h>
#   include	"bmintern.h"
#   include	<appDebugon.h>
#   include	<sioStdio.h>
#   include	<sioEndian.h>
#   include	<utilEndian.h>

/************************************************************************/
/*									*/
/*  Write a bitmap to a wmf stream.					*/
/*									*/
/************************************************************************/

#   define	MM_ANISOTROPIC			8

#   define	WINMETA_SaveDC			0x001e
#   define	WINMETA_SetMapMode		0x0103
#   define	WINMETA_SetWindowExt		0x020c
#   define	WINMETA_SetWindowOrg		0x020b
#   define	WINMETA_StretchBlt		0x0b41
#   define	WINMETA_RestoreDC		0x0127

static int bmWmfWriteSpecialHeader(
				const BitmapDescription *	bd,
				SimpleOutputStream *		sos )
    {
    unsigned int	checksum= 0;
    unsigned int	val;

    int			twipsWide;
    int			twipsHigh;

    const unsigned long	key= 0x9ac6cdd7;

    bmImageSizeTwips( &twipsWide, &twipsHigh, bd );

    sioEndianPutLeInt32( key, sos );
    checksum ^= ( key & 0x0000ffff );
    checksum ^= ( key & 0xffff0000 ) >> 16;

    /*  handle  */
    val= 0;
    sioEndianPutLeUint16( val, sos );
    checksum ^= val;

    /*  left,top  */
    val= 0;
    sioEndianPutLeUint16( val, sos );
    checksum ^= val;
    sioEndianPutLeUint16( val, sos );
    checksum ^= val;

    /*  right,bottom  */
    sioEndianPutLeUint16( twipsWide, sos );
    checksum ^= twipsWide;
    sioEndianPutLeUint16( twipsHigh, sos );
    checksum ^= twipsHigh;

    /*  inch  */
    val= 1440;
    sioEndianPutLeUint16( val, sos );
    checksum ^= val;

    /*  reserved  */
    val= 0;
    sioEndianPutLeUint32( val, sos );
    checksum ^= ( val & 0x0000ffff );
    checksum ^= ( val & 0xffff0000 ) >> 16;

    /*  checksum  */
    sioEndianPutLeUint16( checksum, sos );

    return 22;
    }

static int bmWmfWriteWmfIntern(	const BitmapDescription *	bd,
				const unsigned char *		buffer,
				SimpleOutputStream *		sos,
				int				specialHeader )
    {
    int			done;
    int			bytesWritten= 0;
    long		bltArgCountPos= -1L;
    long		bltArgCount;

    int			headSize= 0;
    long		fileSize= 0L;
    long		headerOffset= 0L;
    long		recordSize;
    long		maxRecordSize= 0L;

    if  ( specialHeader )
	{
	done= bmWmfWriteSpecialHeader( bd, sos );

	if  ( done < 0 )
	    { LDEB(done); return -1;	}

	headSize= done;
	bytesWritten += done;
	}

    sioEndianPutLeInt16( 1, sos );		/*  fileType		*/
    sioEndianPutLeInt16( 9, sos );		/*  headerSize		*/
    sioEndianPutLeInt16( 768, sos );		/*  windowsVersion	*/
    sioEndianPutLeInt32( fileSize, sos );	/*  fileSize		*/
    sioEndianPutLeInt16( 0, sos );		/*  objectCount		*/
    sioEndianPutLeInt32( maxRecordSize, sos );	/*  maxRecordSize	*/
    sioEndianPutLeInt16( 0, sos );		/*  parameterCount	*/

    bytesWritten += 2+ 2+ 2+ 4+ 2+ 4+ 2;

    recordSize= 2+ 1;
    sioEndianPutLeInt32( recordSize, sos );
    sioEndianPutLeInt16( WINMETA_SaveDC, sos );

    bytesWritten += 2* recordSize;
    if  ( maxRecordSize < recordSize )
	{ maxRecordSize=  recordSize;	}

    recordSize= 2+ 1+ 1;
    sioEndianPutLeInt32( recordSize, sos );
    sioEndianPutLeInt16( WINMETA_SetMapMode, sos );
    sioEndianPutLeInt16( MM_ANISOTROPIC, sos );

    bytesWritten += 2* recordSize;
    if  ( maxRecordSize < recordSize )
	{ maxRecordSize=  recordSize;	}

    recordSize= 2+ 1+ 1+ 1;
    sioEndianPutLeInt32( recordSize, sos );
    sioEndianPutLeInt16( WINMETA_SetWindowOrg, sos );
    sioEndianPutLeInt16( 0, sos );
    sioEndianPutLeInt16( 0, sos );

    bytesWritten += 2* recordSize;
    if  ( maxRecordSize < recordSize )
	{ maxRecordSize=  recordSize;	}

    recordSize= 2+ 1+ 1+ 1;
    sioEndianPutLeInt32( recordSize, sos );
    sioEndianPutLeInt16( WINMETA_SetWindowExt, sos );
    sioEndianPutLeInt16( bd->bdPixelsHigh, sos );
    sioEndianPutLeInt16( bd->bdPixelsWide, sos );

    bytesWritten += 2* recordSize;
    if  ( maxRecordSize < recordSize )
	{ maxRecordSize=  recordSize;	}

    bltArgCountPos= bytesWritten;
    recordSize= 2+ 1+ 2+ ( 1+ 1+ 1+ 1 )+ ( 1+ 1+ 1+ 1 );
    sioEndianPutLeInt32( recordSize, sos );
    sioEndianPutLeInt16( WINMETA_StretchBlt, sos );
    sioEndianPutLeInt32( 0xcc0020, sos );
    sioEndianPutLeInt16( bd->bdPixelsHigh, sos );
    sioEndianPutLeInt16( bd->bdPixelsWide, sos );
    sioEndianPutLeInt16( 0, sos );
    sioEndianPutLeInt16( 0, sos );
    sioEndianPutLeInt16( bd->bdPixelsHigh, sos );
    sioEndianPutLeInt16( bd->bdPixelsWide, sos );
    sioEndianPutLeInt16( 0, sos );
    sioEndianPutLeInt16( 0, sos );

    done= bmBmpSaveDib( bd, buffer, bytesWritten+ 2* recordSize, (void *)sos );
    if  ( done < 0 || done % 2 )
	{ LDEB(done); return -1;	}

    recordSize += done/ 2;
    bltArgCount= recordSize;

    bytesWritten += 2* recordSize;
    if  ( maxRecordSize < recordSize )
	{ maxRecordSize=  recordSize;	}

    recordSize= 2+ 1+ 1;
    sioEndianPutLeInt32( recordSize, sos );
    sioEndianPutLeInt16( WINMETA_RestoreDC, sos );
    sioEndianPutLeInt16( -1, sos );

    bytesWritten += 2* recordSize;
    if  ( maxRecordSize < recordSize )
	{ maxRecordSize=  recordSize;	}

    recordSize= 2+ 1;
    sioEndianPutLeInt32( recordSize, sos );
    sioEndianPutLeInt16( 0, sos );

    bytesWritten += 2* recordSize;
    if  ( maxRecordSize < recordSize )
	{ maxRecordSize=  recordSize;	}

    fileSize= bytesWritten- headSize;

    if  ( sioOutSeek( sos, headerOffset ) )
	{ LDEB(headerOffset); return -1; }

    if  ( specialHeader )
	{
	done= bmWmfWriteSpecialHeader( bd, sos );

	if  ( done < 0 )
	    { LDEB(done); return -1;	}
	}

    sioEndianPutLeInt16( 1, sos );		/*  fileType		*/
    sioEndianPutLeInt16( 9, sos );		/*  headerSize		*/
    sioEndianPutLeInt16( 768, sos );		/*  windowsVersion	*/
    sioEndianPutLeInt32( fileSize, sos );	/*  fileSize		*/
    sioEndianPutLeInt16( 0, sos );		/*  objectCount		*/
    sioEndianPutLeInt32( maxRecordSize, sos );	/*  maxRecordSize	*/
    sioEndianPutLeInt16( 0, sos );		/*  parameterCount	*/

    if  ( sioOutSeek( sos, bltArgCountPos ) )
	{ LDEB(headerOffset); return -1; }

    sioEndianPutLeInt32( bltArgCount, sos );

    if  ( sioOutSeek( sos, headSize+ fileSize ) )
	{ LDEB(headerOffset); return -1; }

    return 0;
    }

int bmWmfWriteWmf(		const BitmapDescription *	bd,
				const unsigned char *		buffer,
				SimpleOutputStream *		sos )
    { return bmWmfWriteWmfIntern( bd, buffer, sos, 0 ); }

/************************************************************************/
/*									*/
/*  Can write a bitmap to a WMF file?					*/
/*									*/
/************************************************************************/

int bmCanWriteWmfFile( const BitmapDescription *	bd,
			int				privateFormat,
			double				compressionFactor )
    { return bmCanWriteBmpFile( bd, privateFormat, compressionFactor ); }

int bmWriteWmfFile(	const char *			filename,
			const unsigned char *		buffer,
			const BitmapDescription *	bd,
			int				privateFormat,
			double				compressionFactor )
    {
    SimpleOutputStream *	sos;

    sos= sioOutStdioOpen( filename );
    if  ( ! sos )
	{ SDEB(filename); return -1;	}

    if  ( bmWmfWriteWmfIntern( bd, buffer, sos, 1 ) )
	{ SDEB(filename); sioOutClose( sos ); return -1; }

    sioOutClose( sos );

    return 0;
    }