]> git.saurik.com Git - wxWidgets.git/blame - utils/tex2rtf/src/bmputils.h
testing wxComboBox::SetBackgroundColour()
[wxWidgets.git] / utils / tex2rtf / src / bmputils.h
CommitLineData
9a29912f
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: bmputils.h
3// Purpose: Utilities for manipulating bitmap and metafile images for
4// the purposes of conversion to RTF
5// Author: Julian Smart
6// Modified by:
7// Created: 7.9.93
8// RCS-ID: $Id$
9// Copyright: (c) Julian Smart
10// Licence: wxWindows licence
11/////////////////////////////////////////////////////////////////////////////
12
13static char hexArray[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
14 'C', 'D', 'E', 'F' };
15
6c155d33 16void DecToHex(int dec, wxChar *buf)
9a29912f
JS
17{
18 int firstDigit = (int)(dec/16.0);
19 int secondDigit = (int)(dec - (firstDigit*16.0));
20 buf[0] = hexArray[firstDigit];
21 buf[1] = hexArray[secondDigit];
22 buf[2] = 0;
23}
24
25static unsigned int getshort(FILE *fp)
26{
27 int c, c1;
28 c = getc(fp); c1 = getc(fp);
29 return ((unsigned int) c) + (((unsigned int) c1) << 8);
30}
31
32static unsigned long getint(FILE *fp)
33{
34 int c, c1, c2, c3;
35 c = getc(fp); c1 = getc(fp); c2 = getc(fp); c3 = getc(fp);
36 return (long)((long) c) +
37 (((long) c1) << 8) +
38 (((long) c2) << 16) +
39 (((long) c3) << 24);
40}
41
42bool GetBMPHeader(FILE *fp, int *Width, int *Height, int *Planes, int *BitsPerPixel)
43{
6c155d33
JS
44 // Remember about all fields but store only important ones
45 unsigned long /*
46 bfSize,
47 bfOffBits,
48 biSize,
49 */
50 biWidth,
51 biHeight,
52 biPlanes,
53 biBitCount
54 /* ,
55 biCompression,
56 biSizeImage,
57 biXPelsPerMeter,
58 biYPelsPerMeter,
59 biClrUsed,
60 biClrImportant
61 */
62 ;
9a29912f
JS
63
64 /* read the file type (first two bytes) */
65 int c = getc(fp); int c1 = getc(fp);
66 if (c!='B' || c1!='M') { return FALSE; }
67
6c155d33 68 /* bfSize = */ getint(fp);
9a29912f
JS
69 getshort(fp); /* reserved and ignored */
70 getshort(fp);
6c155d33 71 /* bfOffBits = */ getint(fp);
9a29912f 72
6c155d33 73 /* biSize = */ getint(fp);
9a29912f
JS
74 biWidth = getint(fp);
75 biHeight = getint(fp);
76 biPlanes = getshort(fp);
77 biBitCount = getshort(fp);
6c155d33
JS
78 /* biCompression = */ getint(fp);
79 /* biSizeImage = */ getint(fp);
80 /* biXPelsPerMeter = */ getint(fp);
81 /* biYPelsPerMeter = */ getint(fp);
82 /* biClrUsed = */ getint(fp);
83 /* biClrImportant = */ getint(fp);
9a29912f
JS
84
85 *Width = (int)biWidth;
86 *Height = (int)biHeight;
87 *Planes = (int)biPlanes;
88 *BitsPerPixel = (int)biBitCount;
89
90// fseek(fp, bfOffBits, SEEK_SET);
91
92 return TRUE;
93}
94
95static int scanLineWidth = 0;
96
97bool OutputBitmapHeader(FILE *fd, bool isWinHelp = FALSE)
98{
99 int Width, Height, Planes, BitsPerPixel;
100 if (!GetBMPHeader(fd, &Width, &Height, &Planes, &BitsPerPixel))
101 return FALSE;
102
103 scanLineWidth = (int)((float)Width/(8.0/(float)BitsPerPixel));
104 if ((float)((int)(scanLineWidth/2.0)) != (float)(scanLineWidth/2.0))
105 scanLineWidth ++;
106
107 int goalW = 15*Width;
108 int goalH = 15*Height;
109
6c155d33
JS
110 TexOutput(_T("{\\pict"));
111 if (isWinHelp) TexOutput(_T("\\wbitmap0"));
112 else TexOutput(_T("\\dibitmap)"));
113
114 wxChar buf[50];
115 TexOutput(_T("\\picw")); wxSprintf(buf, _T("%d"), Width); TexOutput(buf);
116 TexOutput(_T("\\pich")); wxSprintf(buf, _T("%d"), Height); TexOutput(buf);
117 TexOutput(_T("\\wbmbitspixel")); wxSprintf(buf, _T("%d"), BitsPerPixel); TexOutput(buf);
118 TexOutput(_T("\\wbmplanes")); wxSprintf(buf, _T("%d"), Planes); TexOutput(buf);
119 TexOutput(_T("\\wbmwidthbytes")); wxSprintf(buf, _T("%d"), scanLineWidth); TexOutput(buf);
120 TexOutput(_T("\\picwgoal")); wxSprintf(buf, _T("%d"), goalW); TexOutput(buf);
121 TexOutput(_T("\\pichgoal")); wxSprintf(buf, _T("%d"), goalH); TexOutput(buf);
122 TexOutput(_T("\n"));
9a29912f
JS
123 return TRUE;
124}
125
126
127bool OutputBitmapData(FILE *fd)
128{
129 fseek(fd, 14, SEEK_SET);
130 int bytesSoFar = 0;
131 int ch = getc(fd);
6c155d33 132 wxChar hexBuf[3];
9a29912f
JS
133 while (ch != EOF)
134 {
135 if (bytesSoFar == scanLineWidth)
136 {
137 bytesSoFar = 0;
6c155d33 138 TexOutput(_T("\n"));
9a29912f
JS
139 }
140 DecToHex(ch, hexBuf);
141 TexOutput(hexBuf);
142 bytesSoFar ++;
143 ch = getc(fd);
144 }
6c155d33 145 TexOutput(_T("\n}\n"));
9a29912f
JS
146 return TRUE;
147}
148
149#ifdef __WXMSW__
150struct mfPLACEABLEHEADER {
151 DWORD key;
152 HANDLE hmf;
153 RECT bbox;
154 WORD inch;
155 DWORD reserved;
156 WORD checksum;
157};
158
159// Returns size in TWIPS
160bool GetMetafileHeader(FILE *handle, int *width, int *height)
161{
162 char buffer[40];
163 mfPLACEABLEHEADER *theHeader = (mfPLACEABLEHEADER *)&buffer;
164 fread((void *)theHeader, sizeof(char), sizeof(mfPLACEABLEHEADER), handle);
165 if (theHeader->key != 0x9AC6CDD7)
166 {
167 return FALSE;
168 }
169
170 float widthInUnits = (float)theHeader->bbox.right - theHeader->bbox.left;
171 float heightInUnits = (float)theHeader->bbox.bottom - theHeader->bbox.top;
172 *width = (int)((widthInUnits*1440.0)/theHeader->inch);
173 *height = (int)((heightInUnits*1440.0)/theHeader->inch);
174 return TRUE;
175}
176
6c155d33 177bool OutputMetafileHeader(FILE *handle, bool WXUNUSED(isWinHelp), int userWidth, int userHeight)
9a29912f
JS
178{
179 int Width, Height;
180 if (!GetMetafileHeader(handle, &Width, &Height))
181 return FALSE;
182
183 scanLineWidth = 64;
184 int goalW = Width;
185 int goalH = Height;
186
187 // Scale to user's dimensions if we have the information
188 if (userWidth > 0 && userHeight == 0)
189 {
190 double scaleFactor = ((double)userWidth/(double)goalW);
191 goalW = userWidth;
192 goalH = (int)((goalH * scaleFactor) + 0.5);
193 }
194 else if (userWidth == 0 && userHeight > 0)
195 {
196 double scaleFactor = ((double)userHeight/(double)goalH);
197 goalH = userHeight;
198 goalW = (int)((goalW * scaleFactor) + 0.5);
199 }
200 else if (userWidth > 0 && userHeight > 0)
201 {
202 goalW = userWidth;
203 goalH = userHeight;
204 }
205
6c155d33
JS
206 TexOutput(_T("{\\pict"));
207 TexOutput(_T("\\wmetafile8"));
9a29912f 208
6c155d33
JS
209 wxChar buf[50];
210 TexOutput(_T("\\picw")); wxSprintf(buf, _T("%d"), Width); TexOutput(buf);
211 TexOutput(_T("\\pich")); wxSprintf(buf, _T("%d"), Height); TexOutput(buf);
212 TexOutput(_T("\\picwgoal")); wxSprintf(buf, _T("%d"), goalW); TexOutput(buf);
213 TexOutput(_T("\\pichgoal")); wxSprintf(buf, _T("%d"), goalH); TexOutput(buf);
214 TexOutput(_T("\n"));
9a29912f
JS
215 return TRUE;
216}
217
218bool OutputMetafileData(FILE *handle)
219{
220 int bytesSoFar = 0;
6c155d33 221 wxChar hexBuf[3];
9a29912f
JS
222 int ch;
223 do
224 {
225 ch = getc(handle);
226 if (bytesSoFar == scanLineWidth)
227 {
228 bytesSoFar = 0;
6c155d33 229 TexOutput(_T("\n"));
9a29912f
JS
230 }
231 if (ch != EOF)
232 {
233 DecToHex(ch, hexBuf);
234 TexOutput(hexBuf);
235 bytesSoFar ++;
236 }
237 } while (ch != EOF);
6c155d33 238 TexOutput(_T("\n}\n"));
9a29912f
JS
239 return TRUE;
240}
241
242#endif
243