]> git.saurik.com Git - wxWidgets.git/blob - utils/tex2rtf/src/bmputils.h
wxHTML not used
[wxWidgets.git] / utils / tex2rtf / src / bmputils.h
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
13 static char hexArray[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
14 'C', 'D', 'E', 'F' };
15
16 void DecToHex(int dec, wxChar *buf)
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
25 static 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
32 static 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
42 bool GetBMPHeader(FILE *fp, int *Width, int *Height, int *Planes, int *BitsPerPixel)
43 {
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 ;
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
68 /* bfSize = */ getint(fp);
69 getshort(fp); /* reserved and ignored */
70 getshort(fp);
71 /* bfOffBits = */ getint(fp);
72
73 /* biSize = */ getint(fp);
74 biWidth = getint(fp);
75 biHeight = getint(fp);
76 biPlanes = getshort(fp);
77 biBitCount = getshort(fp);
78 /* biCompression = */ getint(fp);
79 /* biSizeImage = */ getint(fp);
80 /* biXPelsPerMeter = */ getint(fp);
81 /* biYPelsPerMeter = */ getint(fp);
82 /* biClrUsed = */ getint(fp);
83 /* biClrImportant = */ getint(fp);
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
95 static int scanLineWidth = 0;
96
97 bool 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
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"));
123 return TRUE;
124 }
125
126
127 bool OutputBitmapData(FILE *fd)
128 {
129 fseek(fd, 14, SEEK_SET);
130 int bytesSoFar = 0;
131 int ch = getc(fd);
132 wxChar hexBuf[3];
133 while (ch != EOF)
134 {
135 if (bytesSoFar == scanLineWidth)
136 {
137 bytesSoFar = 0;
138 TexOutput(_T("\n"));
139 }
140 DecToHex(ch, hexBuf);
141 TexOutput(hexBuf);
142 bytesSoFar ++;
143 ch = getc(fd);
144 }
145 TexOutput(_T("\n}\n"));
146 return TRUE;
147 }
148
149 #ifdef __WXMSW__
150 struct 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
160 bool 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
177 bool OutputMetafileHeader(FILE *handle, bool WXUNUSED(isWinHelp), int userWidth, int userHeight)
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
206 TexOutput(_T("{\\pict"));
207 TexOutput(_T("\\wmetafile8"));
208
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"));
215 return TRUE;
216 }
217
218 bool OutputMetafileData(FILE *handle)
219 {
220 int bytesSoFar = 0;
221 wxChar hexBuf[3];
222 int ch;
223 do
224 {
225 ch = getc(handle);
226 if (bytesSoFar == scanLineWidth)
227 {
228 bytesSoFar = 0;
229 TexOutput(_T("\n"));
230 }
231 if (ch != EOF)
232 {
233 DecToHex(ch, hexBuf);
234 TexOutput(hexBuf);
235 bytesSoFar ++;
236 }
237 } while (ch != EOF);
238 TexOutput(_T("\n}\n"));
239 return TRUE;
240 }
241
242 #endif
243