xref: /plan9/sys/src/cmd/aux/antiword/jpeg2eps.c (revision f5736e95f14e1485b3a0291fa82d86cca323ab61)
1 /*
2  * jpeg2eps.c
3  * Copyright (C) 2000-2002 A.J. van Os; Released under GPL
4  *
5  * Description:
6  * Functions to translate jpeg pictures into eps
7  *
8  */
9 
10 #include <stdio.h>
11 #include "antiword.h"
12 
13 #if defined(DEBUG)
14 static int	iPicCounter = 0;
15 #endif /* DEBUG */
16 
17 
18 #if defined(DEBUG)
19 /*
20  * vCopy2File
21  */
22 static void
vCopy2File(FILE * pFile,ULONG ulFileOffset,size_t tPictureLen)23 vCopy2File(FILE *pFile, ULONG ulFileOffset, size_t tPictureLen)
24 {
25 	FILE	*pOutFile;
26 	size_t	tIndex;
27 	int	iTmp;
28 	char	szFilename[30];
29 
30 	if (!bSetDataOffset(pFile, ulFileOffset)) {
31 		return;
32 	}
33 
34 	sprintf(szFilename, "/tmp/pic/pic%04d.jpg", ++iPicCounter);
35 	pOutFile = fopen(szFilename, "wb");
36 	if (pOutFile == NULL) {
37 		return;
38 	}
39 	for (tIndex = 0; tIndex < tPictureLen; tIndex++) {
40 		iTmp = iNextByte(pFile);
41 		if (putc(iTmp, pOutFile) == EOF) {
42 			break;
43 		}
44 	}
45 	(void)fclose(pOutFile);
46 } /* end of vCopy2File */
47 #endif /* DEBUG */
48 
49 /*
50  * bTranslateJPEG - translate a JPEG picture
51  *
52  * This function translates a picture from jpeg to eps
53  *
54  * return TRUE when sucessful, otherwise FALSE
55  */
56 BOOL
bTranslateJPEG(diagram_type * pDiag,FILE * pFile,ULONG ulFileOffset,size_t tPictureLen,const imagedata_type * pImg)57 bTranslateJPEG(diagram_type *pDiag, FILE *pFile,
58 	ULONG ulFileOffset, size_t tPictureLen, const imagedata_type *pImg)
59 {
60 #if defined(DEBUG)
61 	vCopy2File(pFile, ulFileOffset, tPictureLen);
62 #endif /* DEBUG */
63 
64 	/* Seek to start position of JPEG data */
65 	if (!bSetDataOffset(pFile, ulFileOffset)) {
66 		return FALSE;
67 	}
68 
69 	vImagePrologue(pDiag, pImg);
70 	vASCII85EncodeFile(pFile, pDiag->pOutFile, tPictureLen);
71 	vImageEpilogue(pDiag);
72 
73 	return TRUE;
74 } /* end of bTranslateJPEG */
75