xref: /plan9/sys/src/cmd/aux/antiword/jpeg2sprt.c (revision f5736e95f14e1485b3a0291fa82d86cca323ab61)
1 /*
2  * jpeg2sprt.c
3  * Copyright (C) 2000-2002 A.J. van Os; Released under GPL
4  *
5  * Description:
6  * Functions to translate jpeg pictures into sprites
7  */
8 
9 #include <stdio.h>
10 #include "antiword.h"
11 
12 #if 0 /* defined(DEBUG) */
13 static int iPicCounter = 0;
14 #endif /* DEBUG */
15 
16 
17 #if 0 /* defined(DEBUG) */
18 static void
19 vCopy2File(UCHAR *pucJpeg, size_t tJpegSize)
20 {
21 	FILE	*pOutFile;
22 	size_t	tIndex;
23 	char	szFilename[30];
24 
25 	sprintf(szFilename, "<Wimp$ScrapDir>.jpeg%04d", ++iPicCounter);
26 	pOutFile = fopen(szFilename, "wb");
27 	if (pOutFile == NULL) {
28 		return;
29 	}
30 	DBG_MSG(szFilename);
31 	for (tIndex = 0; tIndex < tJpegSize; tIndex++) {
32 		if (putc(pucJpeg[tIndex], pOutFile) == EOF) {
33 			break;
34 		}
35 	}
36 	(void)fclose(pOutFile);
37 	vSetFiletype(szFilename, FILETYPE_JPEG);
38 } /* end of vCopy2File */
39 #endif /* DEBUG */
40 
41 /*
42  * bSave2Draw - save the JPEG picture to the Draw file
43  *
44  * This function puts a JPEG picture in a Draw file
45  *
46  * return TRUE when sucessful, otherwise FALSE
47  */
48 BOOL
bSave2Draw(diagram_type * pDiag,FILE * pFile,size_t tJpegSize,const imagedata_type * pImg)49 bSave2Draw(diagram_type *pDiag, FILE *pFile,
50 	size_t tJpegSize, const imagedata_type *pImg)
51 {
52 	UCHAR	*pucJpeg, *pucTmp;
53 	size_t	tLen;
54 	int	iByte;
55 
56 	pucJpeg = xmalloc(tJpegSize);
57 	for (pucTmp = pucJpeg, tLen = 0; tLen < tJpegSize; pucTmp++, tLen++) {
58 		iByte = iNextByte(pFile);
59 		if (iByte == EOF) {
60 			return FALSE;
61 		}
62 		*pucTmp = (UCHAR)iByte;
63 	}
64 
65 #if 0 /* defined(DEBUG) */
66 	vCopy2File(pucJpeg, tJpegSize);
67 #endif /* DEBUG */
68 
69 	/* Add the JPEG to the Draw file */
70 	vImage2Diagram(pDiag, pImg, pucJpeg, tJpegSize);
71 
72 	xfree(pucJpeg);
73 	return TRUE;
74 } /* end of bSave2Draw */
75 
76 /*
77  * bTranslateJPEG - translate a JPEG picture
78  *
79  * This function translates a picture from jpeg to sprite
80  *
81  * return TRUE when sucessful, otherwise FALSE
82  */
83 BOOL
bTranslateJPEG(diagram_type * pDiag,FILE * pFile,ULONG ulFileOffset,size_t tPictureLen,const imagedata_type * pImg)84 bTranslateJPEG(diagram_type *pDiag, FILE *pFile,
85 	ULONG ulFileOffset, size_t tPictureLen, const imagedata_type *pImg)
86 {
87   	/* Seek to start position of JPEG data */
88 	if (!bSetDataOffset(pFile, ulFileOffset)) {
89 		return FALSE;
90 	}
91 
92 	if (iGetRiscOsVersion() >= 360) {
93 		return bSave2Draw(pDiag, pFile, tPictureLen, pImg);
94 	}
95   	/* JPEG is not supported until RISC OS 3.6 */
96 	return bAddDummyImage(pDiag, pImg);
97 } /* end of bTranslateJPEG */
98