]>
git.saurik.com Git - wxWidgets.git/blob - utils/tex2rtf/src/bmputils.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Utilities for manipulating bitmap and metafile images for
4 // the purposes of conversion to RTF
5 // Author: Julian Smart
6 // Modified by: Wlodzimiez ABX Skiba 2003/2004 Unicode support
10 // Copyright: (c) Julian Smart
11 // Licence: wxWindows licence
12 /////////////////////////////////////////////////////////////////////////////
14 static char hexArray
[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
17 void DecToHex(int dec
, wxChar
*buf
)
19 int firstDigit
= (int)(dec
/16.0);
20 int secondDigit
= (int)(dec
- (firstDigit
*16.0));
21 buf
[0] = hexArray
[firstDigit
];
22 buf
[1] = hexArray
[secondDigit
];
26 static unsigned int getshort(FILE *fp
)
29 c
= getc(fp
); c1
= getc(fp
);
30 return ((unsigned int) c
) + (((unsigned int) c1
) << 8);
33 static unsigned long getint(FILE *fp
)
36 c
= getc(fp
); c1
= getc(fp
); c2
= getc(fp
); c3
= getc(fp
);
37 return (long)((long) c
) +
43 bool GetBMPHeader(FILE *fp
, int *Width
, int *Height
, int *Planes
, int *BitsPerPixel
)
45 // Remember about all fields but store only important ones
65 /* read the file type (first two bytes) */
66 int c
= getc(fp
); int c1
= getc(fp
);
67 if (c
!='B' || c1
!='M') { return false; }
69 /* bfSize = */ getint(fp
);
70 getshort(fp
); /* reserved and ignored */
72 /* bfOffBits = */ getint(fp
);
74 /* biSize = */ getint(fp
);
76 biHeight
= getint(fp
);
77 biPlanes
= getshort(fp
);
78 biBitCount
= getshort(fp
);
79 /* biCompression = */ getint(fp
);
80 /* biSizeImage = */ getint(fp
);
81 /* biXPelsPerMeter = */ getint(fp
);
82 /* biYPelsPerMeter = */ getint(fp
);
83 /* biClrUsed = */ getint(fp
);
84 /* biClrImportant = */ getint(fp
);
86 *Width
= (int)biWidth
;
87 *Height
= (int)biHeight
;
88 *Planes
= (int)biPlanes
;
89 *BitsPerPixel
= (int)biBitCount
;
91 // fseek(fp, bfOffBits, SEEK_SET);
96 static int scanLineWidth
= 0;
98 bool OutputBitmapHeader(FILE *fd
, bool isWinHelp
= false)
100 int Width
, Height
, Planes
, BitsPerPixel
;
101 if (!GetBMPHeader(fd
, &Width
, &Height
, &Planes
, &BitsPerPixel
))
104 scanLineWidth
= (int)((float)Width
/(8.0/(float)BitsPerPixel
));
105 if ((float)((int)(scanLineWidth
/2.0)) != (float)(scanLineWidth
/2.0))
108 int goalW
= 15*Width
;
109 int goalH
= 15*Height
;
111 TexOutput(_T("{\\pict"));
112 if (isWinHelp
) TexOutput(_T("\\wbitmap0"));
113 else TexOutput(_T("\\dibitmap)"));
116 TexOutput(_T("\\picw")); wxSnprintf(buf
, sizeof(buf
), _T("%d"), Width
); TexOutput(buf
);
117 TexOutput(_T("\\pich")); wxSnprintf(buf
, sizeof(buf
), _T("%d"), Height
); TexOutput(buf
);
118 TexOutput(_T("\\wbmbitspixel")); wxSnprintf(buf
, sizeof(buf
), _T("%d"), BitsPerPixel
); TexOutput(buf
);
119 TexOutput(_T("\\wbmplanes")); wxSnprintf(buf
, sizeof(buf
), _T("%d"), Planes
); TexOutput(buf
);
120 TexOutput(_T("\\wbmwidthbytes")); wxSnprintf(buf
, sizeof(buf
), _T("%d"), scanLineWidth
); TexOutput(buf
);
121 TexOutput(_T("\\picwgoal")); wxSnprintf(buf
, sizeof(buf
), _T("%d"), goalW
); TexOutput(buf
);
122 TexOutput(_T("\\pichgoal")); wxSnprintf(buf
, sizeof(buf
), _T("%d"), goalH
); TexOutput(buf
);
128 bool OutputBitmapData(FILE *fd
)
130 fseek(fd
, 14, SEEK_SET
);
136 if (bytesSoFar
== scanLineWidth
)
141 DecToHex(ch
, hexBuf
);
146 TexOutput(_T("\n}\n"));
151 struct mfPLACEABLEHEADER
{
160 // Returns size in TWIPS
161 bool GetMetafileHeader(FILE *handle
, int *width
, int *height
)
164 mfPLACEABLEHEADER
*theHeader
= (mfPLACEABLEHEADER
*)&buffer
;
165 fread((void *)theHeader
, sizeof(char), sizeof(mfPLACEABLEHEADER
), handle
);
166 if (theHeader
->key
!= 0x9AC6CDD7)
171 float widthInUnits
= (float)theHeader
->bbox
.right
- theHeader
->bbox
.left
;
172 float heightInUnits
= (float)theHeader
->bbox
.bottom
- theHeader
->bbox
.top
;
173 *width
= (int)((widthInUnits
*1440.0)/theHeader
->inch
);
174 *height
= (int)((heightInUnits
*1440.0)/theHeader
->inch
);
178 bool OutputMetafileHeader(FILE *handle
, bool WXUNUSED(isWinHelp
), int userWidth
, int userHeight
)
181 if (!GetMetafileHeader(handle
, &Width
, &Height
))
188 // Scale to user's dimensions if we have the information
189 if (userWidth
> 0 && userHeight
== 0)
191 double scaleFactor
= ((double)userWidth
/(double)goalW
);
193 goalH
= (int)((goalH
* scaleFactor
) + 0.5);
195 else if (userWidth
== 0 && userHeight
> 0)
197 double scaleFactor
= ((double)userHeight
/(double)goalH
);
199 goalW
= (int)((goalW
* scaleFactor
) + 0.5);
201 else if (userWidth
> 0 && userHeight
> 0)
207 TexOutput(_T("{\\pict"));
208 TexOutput(_T("\\wmetafile8"));
211 TexOutput(_T("\\picw")); wxSnprintf(buf
, sizeof(buf
), _T("%d"), Width
); TexOutput(buf
);
212 TexOutput(_T("\\pich")); wxSnprintf(buf
, sizeof(buf
), _T("%d"), Height
); TexOutput(buf
);
213 TexOutput(_T("\\picwgoal")); wxSnprintf(buf
, sizeof(buf
), _T("%d"), goalW
); TexOutput(buf
);
214 TexOutput(_T("\\pichgoal")); wxSnprintf(buf
, sizeof(buf
), _T("%d"), goalH
); TexOutput(buf
);
219 bool OutputMetafileData(FILE *handle
)
227 if (bytesSoFar
== scanLineWidth
)
234 DecToHex(ch
, hexBuf
);
239 TexOutput(_T("\n}\n"));