]> git.saurik.com Git - wxWidgets.git/blob - utils/tex2rtf/src/bmputils.h
fixed menu accelerators
[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, char *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 unsigned long bfSize, bfOffBits, biSize, biWidth, biHeight, biPlanes;
45 unsigned long biBitCount, biCompression, biSizeImage, biXPelsPerMeter;
46 unsigned long biYPelsPerMeter, biClrUsed, biClrImportant;
47
48 /* read the file type (first two bytes) */
49 int c = getc(fp); int c1 = getc(fp);
50 if (c!='B' || c1!='M') { return FALSE; }
51
52 bfSize = getint(fp);
53 getshort(fp); /* reserved and ignored */
54 getshort(fp);
55 bfOffBits = getint(fp);
56
57 biSize = getint(fp);
58 biWidth = getint(fp);
59 biHeight = getint(fp);
60 biPlanes = getshort(fp);
61 biBitCount = getshort(fp);
62 biCompression = getint(fp);
63 biSizeImage = getint(fp);
64 biXPelsPerMeter = getint(fp);
65 biYPelsPerMeter = getint(fp);
66 biClrUsed = getint(fp);
67 biClrImportant = getint(fp);
68
69 *Width = (int)biWidth;
70 *Height = (int)biHeight;
71 *Planes = (int)biPlanes;
72 *BitsPerPixel = (int)biBitCount;
73
74 // fseek(fp, bfOffBits, SEEK_SET);
75
76 return TRUE;
77 }
78
79 static int scanLineWidth = 0;
80
81 bool OutputBitmapHeader(FILE *fd, bool isWinHelp = FALSE)
82 {
83 int Width, Height, Planes, BitsPerPixel;
84 if (!GetBMPHeader(fd, &Width, &Height, &Planes, &BitsPerPixel))
85 return FALSE;
86
87 scanLineWidth = (int)((float)Width/(8.0/(float)BitsPerPixel));
88 if ((float)((int)(scanLineWidth/2.0)) != (float)(scanLineWidth/2.0))
89 scanLineWidth ++;
90
91 int goalW = 15*Width;
92 int goalH = 15*Height;
93
94 TexOutput("{\\pict");
95 if (isWinHelp) TexOutput("\\wbitmap0");
96 else TexOutput("\\dibitmap");
97
98 char buf[50];
99 TexOutput("\\picw"); sprintf(buf, "%d", Width); TexOutput(buf);
100 TexOutput("\\pich"); sprintf(buf, "%d", Height); TexOutput(buf);
101 TexOutput("\\wbmbitspixel"); sprintf(buf, "%d", BitsPerPixel); TexOutput(buf);
102 TexOutput("\\wbmplanes"); sprintf(buf, "%d", Planes); TexOutput(buf);
103 TexOutput("\\wbmwidthbytes"); sprintf(buf, "%d", scanLineWidth); TexOutput(buf);
104 TexOutput("\\picwgoal"); sprintf(buf, "%d", goalW); TexOutput(buf);
105 TexOutput("\\pichgoal"); sprintf(buf, "%d", goalH); TexOutput(buf);
106 TexOutput("\n");
107 return TRUE;
108 }
109
110
111 bool OutputBitmapData(FILE *fd)
112 {
113 fseek(fd, 14, SEEK_SET);
114 int bytesSoFar = 0;
115 int ch = getc(fd);
116 char hexBuf[3];
117 while (ch != EOF)
118 {
119 if (bytesSoFar == scanLineWidth)
120 {
121 bytesSoFar = 0;
122 TexOutput("\n");
123 }
124 DecToHex(ch, hexBuf);
125 TexOutput(hexBuf);
126 bytesSoFar ++;
127 ch = getc(fd);
128 }
129 TexOutput("\n}\n");
130 return TRUE;
131 }
132
133 #ifdef __WXMSW__
134 struct mfPLACEABLEHEADER {
135 DWORD key;
136 HANDLE hmf;
137 RECT bbox;
138 WORD inch;
139 DWORD reserved;
140 WORD checksum;
141 };
142
143 // Returns size in TWIPS
144 bool GetMetafileHeader(FILE *handle, int *width, int *height)
145 {
146 char buffer[40];
147 mfPLACEABLEHEADER *theHeader = (mfPLACEABLEHEADER *)&buffer;
148 fread((void *)theHeader, sizeof(char), sizeof(mfPLACEABLEHEADER), handle);
149 if (theHeader->key != 0x9AC6CDD7)
150 {
151 return FALSE;
152 }
153
154 float widthInUnits = (float)theHeader->bbox.right - theHeader->bbox.left;
155 float heightInUnits = (float)theHeader->bbox.bottom - theHeader->bbox.top;
156 *width = (int)((widthInUnits*1440.0)/theHeader->inch);
157 *height = (int)((heightInUnits*1440.0)/theHeader->inch);
158 return TRUE;
159 }
160
161 bool OutputMetafileHeader(FILE *handle, bool isWinHelp, int userWidth, int userHeight)
162 {
163 int Width, Height;
164 if (!GetMetafileHeader(handle, &Width, &Height))
165 return FALSE;
166
167 scanLineWidth = 64;
168 int goalW = Width;
169 int goalH = Height;
170
171 // Scale to user's dimensions if we have the information
172 if (userWidth > 0 && userHeight == 0)
173 {
174 double scaleFactor = ((double)userWidth/(double)goalW);
175 goalW = userWidth;
176 goalH = (int)((goalH * scaleFactor) + 0.5);
177 }
178 else if (userWidth == 0 && userHeight > 0)
179 {
180 double scaleFactor = ((double)userHeight/(double)goalH);
181 goalH = userHeight;
182 goalW = (int)((goalW * scaleFactor) + 0.5);
183 }
184 else if (userWidth > 0 && userHeight > 0)
185 {
186 goalW = userWidth;
187 goalH = userHeight;
188 }
189
190 TexOutput("{\\pict");
191 TexOutput("\\wmetafile8");
192
193 char buf[50];
194 TexOutput("\\picw"); sprintf(buf, "%d", Width); TexOutput(buf);
195 TexOutput("\\pich"); sprintf(buf, "%d", Height); TexOutput(buf);
196 TexOutput("\\picwgoal"); sprintf(buf, "%d", goalW); TexOutput(buf);
197 TexOutput("\\pichgoal"); sprintf(buf, "%d", goalH); TexOutput(buf);
198 TexOutput("\n");
199 return TRUE;
200 }
201
202 bool OutputMetafileData(FILE *handle)
203 {
204 int bytesSoFar = 0;
205 char hexBuf[3];
206 int ch;
207 do
208 {
209 ch = getc(handle);
210 if (bytesSoFar == scanLineWidth)
211 {
212 bytesSoFar = 0;
213 TexOutput("\n");
214 }
215 if (ch != EOF)
216 {
217 DecToHex(ch, hexBuf);
218 TexOutput(hexBuf);
219 bytesSoFar ++;
220 }
221 } while (ch != EOF);
222 TexOutput("\n}\n");
223 return TRUE;
224 }
225
226 #endif
227