]>
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
, char *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 unsigned long bfSize
, bfOffBits
, biSize
, biWidth
, biHeight
, biPlanes
;
45 unsigned long biBitCount
, biCompression
, biSizeImage
, biXPelsPerMeter
;
46 unsigned long biYPelsPerMeter
, biClrUsed
, biClrImportant
;
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
; }
53 getshort(fp
); /* reserved and ignored */
55 bfOffBits
= 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
);
69 *Width
= (int)biWidth
;
70 *Height
= (int)biHeight
;
71 *Planes
= (int)biPlanes
;
72 *BitsPerPixel
= (int)biBitCount
;
74 // fseek(fp, bfOffBits, SEEK_SET);
79 static int scanLineWidth
= 0;
81 bool OutputBitmapHeader(FILE *fd
, bool isWinHelp
= FALSE
)
83 int Width
, Height
, Planes
, BitsPerPixel
;
84 if (!GetBMPHeader(fd
, &Width
, &Height
, &Planes
, &BitsPerPixel
))
87 scanLineWidth
= (int)((float)Width
/(8.0/(float)BitsPerPixel
));
88 if ((float)((int)(scanLineWidth
/2.0)) != (float)(scanLineWidth
/2.0))
92 int goalH
= 15*Height
;
95 if (isWinHelp
) TexOutput("\\wbitmap0");
96 else TexOutput("\\dibitmap");
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
);
111 bool OutputBitmapData(FILE *fd
)
113 fseek(fd
, 14, SEEK_SET
);
119 if (bytesSoFar
== scanLineWidth
)
124 DecToHex(ch
, hexBuf
);
134 struct mfPLACEABLEHEADER
{
143 // Returns size in TWIPS
144 bool GetMetafileHeader(FILE *handle
, int *width
, int *height
)
147 mfPLACEABLEHEADER
*theHeader
= (mfPLACEABLEHEADER
*)&buffer
;
148 fread((void *)theHeader
, sizeof(char), sizeof(mfPLACEABLEHEADER
), handle
);
149 if (theHeader
->key
!= 0x9AC6CDD7)
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
);
161 bool OutputMetafileHeader(FILE *handle
, bool isWinHelp
, int userWidth
, int userHeight
)
164 if (!GetMetafileHeader(handle
, &Width
, &Height
))
171 // Scale to user's dimensions if we have the information
172 if (userWidth
> 0 && userHeight
== 0)
174 double scaleFactor
= ((double)userWidth
/(double)goalW
);
176 goalH
= (int)((goalH
* scaleFactor
) + 0.5);
178 else if (userWidth
== 0 && userHeight
> 0)
180 double scaleFactor
= ((double)userHeight
/(double)goalH
);
182 goalW
= (int)((goalW
* scaleFactor
) + 0.5);
184 else if (userWidth
> 0 && userHeight
> 0)
190 TexOutput("{\\pict");
191 TexOutput("\\wmetafile8");
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
);
202 bool OutputMetafileData(FILE *handle
)
210 if (bytesSoFar
== scanLineWidth
)
217 DecToHex(ch
, hexBuf
);