]>
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
9 // Copyright: (c) Julian Smart
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 static char hexArray
[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
16 void DecToHex(int dec
, wxChar
*buf
)
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
];
25 static unsigned int getshort(FILE *fp
)
28 c
= getc(fp
); c1
= getc(fp
);
29 return ((unsigned int) c
) + (((unsigned int) c1
) << 8);
32 static unsigned long getint(FILE *fp
)
35 c
= getc(fp
); c1
= getc(fp
); c2
= getc(fp
); c3
= getc(fp
);
36 return (long)((long) c
) +
42 bool GetBMPHeader(FILE *fp
, int *Width
, int *Height
, int *Planes
, int *BitsPerPixel
)
44 // Remember about all fields but store only important ones
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
; }
68 /* bfSize = */ getint(fp
);
69 getshort(fp
); /* reserved and ignored */
71 /* bfOffBits = */ getint(fp
);
73 /* biSize = */ 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
);
85 *Width
= (int)biWidth
;
86 *Height
= (int)biHeight
;
87 *Planes
= (int)biPlanes
;
88 *BitsPerPixel
= (int)biBitCount
;
90 // fseek(fp, bfOffBits, SEEK_SET);
95 static int scanLineWidth
= 0;
97 bool OutputBitmapHeader(FILE *fd
, bool isWinHelp
= FALSE
)
99 int Width
, Height
, Planes
, BitsPerPixel
;
100 if (!GetBMPHeader(fd
, &Width
, &Height
, &Planes
, &BitsPerPixel
))
103 scanLineWidth
= (int)((float)Width
/(8.0/(float)BitsPerPixel
));
104 if ((float)((int)(scanLineWidth
/2.0)) != (float)(scanLineWidth
/2.0))
107 int goalW
= 15*Width
;
108 int goalH
= 15*Height
;
110 TexOutput(_T("{\\pict"));
111 if (isWinHelp
) TexOutput(_T("\\wbitmap0"));
112 else TexOutput(_T("\\dibitmap)"));
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
);
127 bool OutputBitmapData(FILE *fd
)
129 fseek(fd
, 14, SEEK_SET
);
135 if (bytesSoFar
== scanLineWidth
)
140 DecToHex(ch
, hexBuf
);
145 TexOutput(_T("\n}\n"));
150 struct mfPLACEABLEHEADER
{
159 // Returns size in TWIPS
160 bool GetMetafileHeader(FILE *handle
, int *width
, int *height
)
163 mfPLACEABLEHEADER
*theHeader
= (mfPLACEABLEHEADER
*)&buffer
;
164 fread((void *)theHeader
, sizeof(char), sizeof(mfPLACEABLEHEADER
), handle
);
165 if (theHeader
->key
!= 0x9AC6CDD7)
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
);
177 bool OutputMetafileHeader(FILE *handle
, bool WXUNUSED(isWinHelp
), int userWidth
, int userHeight
)
180 if (!GetMetafileHeader(handle
, &Width
, &Height
))
187 // Scale to user's dimensions if we have the information
188 if (userWidth
> 0 && userHeight
== 0)
190 double scaleFactor
= ((double)userWidth
/(double)goalW
);
192 goalH
= (int)((goalH
* scaleFactor
) + 0.5);
194 else if (userWidth
== 0 && userHeight
> 0)
196 double scaleFactor
= ((double)userHeight
/(double)goalH
);
198 goalW
= (int)((goalW
* scaleFactor
) + 0.5);
200 else if (userWidth
> 0 && userHeight
> 0)
206 TexOutput(_T("{\\pict"));
207 TexOutput(_T("\\wmetafile8"));
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
);
218 bool OutputMetafileData(FILE *handle
)
226 if (bytesSoFar
== scanLineWidth
)
233 DecToHex(ch
, hexBuf
);
238 TexOutput(_T("\n}\n"));