merge the formfield patch from ooo-build
[ooovba.git] / hwpfilter / source / himgutil.cpp
blob93246320335a9d8ebd916b70365e700a78389463
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: himgutil.cpp,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "precompile.h"
33 #include <stdio.h>
34 #include <ctype.h>
35 #include <string.h>
37 #include "hwplib.h"
38 #include "htags.h"
39 #include "himgutil.h"
42 static int ImageMagicType(const uchar * magicno)
44 int rv = RFT_UNKNOWN;
46 if (strncmp((char *) magicno, "GIF87a", 6) == 0 ||
47 strncmp((char *) magicno, "GIF89a", 6) == 0)
48 rv = RFT_GIF;
49 else if (strncmp((char *) magicno, "VIEW", 4) == 0 ||
50 strncmp((char *) magicno, "WEIV", 4) == 0)
51 rv = RFT_PM;
52 else if (magicno[0] == 'P' && magicno[1] >= '1' && magicno[1] <= '6')
53 rv = RFT_PBM;
54 /* note: have to check XPM before XBM, as first 2 chars are the same */
55 else if (strncmp((char *) magicno, "/* XPM */", 9) == 0)
56 rv = RFT_XPM;
57 else if (strncmp((char *) magicno, "#define", 7) == 0 ||
58 (magicno[0] == '/' && magicno[1] == '*'))
59 rv = RFT_XBM;
60 else if (magicno[0] == 0x59 && (magicno[1] & 0x7f) == 0x26 &&
61 magicno[2] == 0x6a && (magicno[3] & 0x7f) == 0x15)
62 rv = RFT_SUNRAS;
63 else if (magicno[0] == 'B' && magicno[1] == 'M')
64 rv = RFT_BMP;
65 else if (magicno[0] == 0x52 && magicno[1] == 0xcc)
66 rv = RFT_UTAHRLE;
67 else if ((magicno[0] == 0x01 && magicno[1] == 0xda) ||
68 (magicno[0] == 0xda && magicno[1] == 0x01))
69 rv = RFT_IRIS;
70 else if (magicno[0] == 0x1f && magicno[1] == 0x9d)
71 rv = RFT_COMPRESS;
72 else if (magicno[0] == 0x0a && magicno[1] <= 5)
73 rv = RFT_PCX;
74 else if (strncmp((char *) magicno, "FORM", 4) == 0 &&
75 strncmp((char *) magicno + 8, "ILBM", 4) == 0)
76 rv = RFT_IFF;
77 else if (magicno[0] == 0 && magicno[1] == 0 &&
78 magicno[2] == 2 && magicno[3] == 0 &&
79 magicno[4] == 0 && magicno[5] == 0 &&
80 magicno[6] == 0 && magicno[7] == 0)
81 rv = RFT_TARGA;
82 else if (magicno[4] == 0x00 && magicno[5] == 0x00 &&
83 magicno[6] == 0x00 && magicno[7] == 0x07)
84 rv = RFT_XWD;
85 else if (strncmp((char *) magicno, "SIMPLE ", 8) == 0 &&
86 magicno[29] == 'T')
87 rv = RFT_FITS;
88 else if (magicno[0] == 0xff && magicno[1] == 0xd8 && magicno[2] == 0xff)
89 rv = RFT_JFIF;
90 else if ((magicno[0] == 'M' && magicno[1] == 'M') ||
91 (magicno[0] == 'I' && magicno[1] == 'I'))
92 rv = RFT_TIFF;
93 else if (strncmp((char *) magicno, "%!", 2) == 0 ||
94 strncmp((char *) magicno, "\004%!", 3) == 0)
95 rv = RFT_PS;
96 else if (strncmp((char *) magicno, "\xd7\xcd\xc6\x9a", 4) == 0 ||
97 strncmp((char *) magicno, "\x01\x00\x09\x00\x00\x03", 6) == 0)
98 rv = RFT_WMF;
100 return rv;
103 const char *GetEmbImgname(const EmPicture * empic)
105 static char fname[256];
106 char *ptr;
107 const char *ext;
109 tmpnam(fname);
110 if (!empic || !empic->name[0] || (0 == (ptr = strrchr(fname, DIRSEP))))
111 return NULL;
112 switch (ImageMagicType((uchar *) empic->data))
114 case RFT_GIF:
115 ext = "gif";
116 break;
117 case RFT_PM:
118 ext = "pm";
119 break;
120 case RFT_PBM:
121 ext = "pbm";
122 break;
123 case RFT_XBM:
124 ext = "xbm";
125 break;
126 case RFT_SUNRAS:
127 ext = "ras";
128 break;
129 case RFT_BMP:
130 ext = "bmp";
131 break;
132 case RFT_UTAHRLE:
133 ext = "rle";
134 break;
135 case RFT_PCX:
136 ext = "pcx";
137 break;
138 case RFT_JFIF:
139 ext = "jpg";
140 break;
141 case RFT_TIFF:
142 ext = "tif";
143 break;
144 case RFT_PDSVICAR:
145 ext = "pds";
146 break;
147 case RFT_PS:
148 ext = "ps";
149 break;
150 case RFT_IFF:
151 ext = "iff";
152 break;
153 case RFT_TARGA:
154 ext = "tga";
155 break;
156 case RFT_XPM:
157 ext = "xpm";
158 break;
159 case RFT_XWD:
160 ext = "xwd";
161 break;
162 case RFT_WMF:
163 ext = "wmf";
164 break;
165 default:
166 ext = "666";
167 break;
169 sprintf(ptr + 1, "mz_%s.%s", empic->name,ext);
170 return fname;