summaryrefslogtreecommitdiff
path: root/DevIL/src-IL/src/il_scitex.cpp
blob: c2a863d904004864734bf9db2735105e74364e19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
//-----------------------------------------------------------------------------
//
// ImageLib Sources
// Copyright (C) 2000-2017 by Denton Woods
// Last modified: 01/21/2017
//
// Filename: src-IL/src/il_scitex.cpp
//
// Description: Reads from a Scitex Continuous Tone (.ct) file.
//                Specifications were found at http://fileformats.archiveteam.org/wiki/Scitex_CT
//				  and http://web.archive.org/web/20130506073138/http://electricmessiah.org/press/the-scitex-ct-file-format/
//
//-----------------------------------------------------------------------------


#include "il_internal.h"
#ifndef IL_NO_SCITEX
//#include "il_scitex.h"


typedef struct SCITEXHEAD
{
	ILubyte	Comment[80];
	ILubyte	Sig[2];			// Signature, should be "CT"
	ILubyte	Units;			// 0 for mm, 1 for inches
	ILubyte	NumChans;		// Number of channels
	ILushort ColorModel;	// Color model (7=RGB, 8=GREYSCALE, 15=CMYK)
	ILfloat	HeightUnits;	// Physical height
	ILfloat	WidthUnits;		// Physical width
	ILuint	HeightPixels;
	ILuint	WidthPixels;
} SCITEXHEAD;

ILboolean iIsValidScitex(void);
ILboolean iLoadScitexInternal(void);
ILboolean iCheckScitex(SCITEXHEAD *Header);


// Internal function used to get the Scitex header from the current file.
ILboolean iGetScitexHead(SCITEXHEAD *Header)
{
	char HeightUnits[15], WidthUnits[15], HeightPixels[13], WidthPixels[13];

	if (iread(Header->Comment, 1, 80) != 80)
		return IL_FALSE;
	if (iread(Header->Sig, 1, 2) != 2)
		return IL_FALSE;
	if (iseek(942, IL_SEEK_CUR) == -1)
		return IL_FALSE;
	Header->Units = igetc();
	Header->NumChans = igetc();
	Header->ColorModel = GetBigUShort();
	if (iread(&HeightUnits, 1, 14) != 14)
		return IL_FALSE;
	if (iread(&WidthUnits, 1, 14) != 14)
		return IL_FALSE;
	if (iread(&HeightPixels, 1, 12) != 12)
		return IL_FALSE;
	if (iread(&WidthPixels, 1, 12) != 12)
		return IL_FALSE;

	HeightUnits[14] = NULL;
	WidthUnits[14] = NULL;
	HeightPixels[12] = NULL;
	WidthPixels[12] = NULL;

	Header->HeightUnits = (ILfloat)atof(HeightUnits);
	Header->WidthUnits = (ILfloat)atof(WidthUnits);
	Header->HeightPixels = (ILint)atoi(HeightPixels);
	Header->WidthPixels = (ILint)atof(WidthPixels);

	switch (Header->ColorModel)
	{
		case 7:
			if (Header->NumChans != 3)
				return IL_FALSE;
			break;
		case 8:
			if (Header->NumChans != 1)
				return IL_FALSE;
			break;
		//case 15:
		//default:
	}

	return IL_TRUE;
}


//! Checks if the file specified in FileName is a valid BLP file.
ILboolean ilIsValidScitex(ILconst_string FileName)
{
	ILHANDLE	ScitexFile;
	ILboolean	bScitex = IL_FALSE;

	if (!iCheckExtension(FileName, IL_TEXT("ct"))) {
		ilSetError(IL_INVALID_EXTENSION);
		return bScitex;
	}

	ScitexFile = iopenr(FileName);
	if (ScitexFile == NULL) {
		ilSetError(IL_COULD_NOT_OPEN_FILE);
		return bScitex;
	}

	bScitex = ilIsValidScitexF(ScitexFile);
	icloser(ScitexFile);

	return bScitex;
}


//! Checks if the ILHANDLE contains a valid Scitex file at the current position.
ILboolean ilIsValidScitexF(ILHANDLE File)
{
	ILuint		FirstPos;
	ILboolean	bRet;

	iSetInputFile(File);
	FirstPos = itell();
	bRet = iIsValidScitex();
	iseek(FirstPos, IL_SEEK_SET);

	return bRet;
}


//! Checks if Lump is a valid BLP lump.
ILboolean ilIsValidScitexL(const void *Lump, ILuint Size)
{
	iSetInputLump(Lump, Size);
	return iIsValidScitex();
}


// Internal function to get the header and check it.
ILboolean iIsValidScitex(void)
{
	SCITEXHEAD Header;

	if (!iGetScitexHead(&Header))
		return IL_FALSE;
	iseek(-2048, IL_SEEK_CUR);

	return iCheckScitex(&Header);
}


// Internal function used to check if the HEADER is a valid BLP header.
ILboolean iCheckScitex(SCITEXHEAD *Header)
{
	// The file signature is 'CT'.
	if (Header->Sig[0] != 'C' || Header->Sig[1] != 'T')
		return IL_FALSE;
	if (Header->Units != 0 && Header->Units != 1)
		return IL_FALSE;
	//@TODO: Handle CMYK version
	if (Header->ColorModel != 7 && Header->ColorModel != 8 /*&& Header->ColorModel != 15*/)
		return IL_FALSE;
	if (Header->HeightUnits <= 0 || Header->WidthUnits <= 0)
		return IL_FALSE;
	if (Header->HeightPixels <= 0 || Header->WidthPixels <= 0)
		return IL_FALSE;

	return IL_TRUE;
}



//! Reads a BLP file
ILboolean ilLoadScitex(ILconst_string FileName)
{
	ILHANDLE	ScitexFile;
	ILboolean	bScitex = IL_FALSE;

	ScitexFile = iopenr(FileName);
	if (ScitexFile == NULL) {
		ilSetError(IL_COULD_NOT_OPEN_FILE);
		return bScitex;
	}

	bScitex = ilLoadScitexF(ScitexFile);
	icloser(ScitexFile);

	return bScitex;
}


//! Reads an already-opened BLP file
ILboolean ilLoadScitexF(ILHANDLE File)
{
	ILuint		FirstPos;
	ILboolean	bRet;

	iSetInputFile(File);
	FirstPos = itell();
	bRet = iLoadScitexInternal();
	iseek(FirstPos, IL_SEEK_SET);

	return bRet;
}


//! Reads from a memory "lump" that contains a BLP
ILboolean ilLoadScitexL(const void *Lump, ILuint Size)
{
	iSetInputLump(Lump, Size);
	return iLoadScitexInternal();
}


// Internal function used to load the BLP.
ILboolean iLoadScitexInternal(void)
{
	SCITEXHEAD Header;
	ILuint Pos = 0;
	ILubyte *RowData;

	if (iCurImage == NULL) {
		ilSetError(IL_ILLEGAL_OPERATION);
		return IL_FALSE;
	}

	if (!iGetScitexHead(&Header)) {
		ilSetError(IL_INVALID_FILE_HEADER);
		return IL_FALSE;
	}
	if (!iCheckScitex(&Header)) {
		return IL_FALSE;
	}

	iseek(2048, IL_SEEK_SET);
	RowData = (ILubyte*)ialloc(Header.WidthPixels);
	if (RowData == NULL)
		return IL_FALSE;

	switch (Header.ColorModel)
	{
		case 7:
			if (!ilTexImage(Header.WidthPixels, Header.HeightPixels, 1, Header.NumChans, 0, IL_UNSIGNED_BYTE, NULL))
			{
				ifree(RowData);
				return IL_FALSE;
			}
			iCurImage->Format = IL_RGB;

			for (ILuint h = 0; h < iCurImage->Height; h++)
			{
				for (ILuint c = 0; c < iCurImage->Bpp; c++)
				{
					if (iread(RowData, 1, Header.WidthPixels) != Header.WidthPixels)
					{
						ifree(RowData);
						return IL_FALSE;
					}
					for (ILuint w = 0; w < iCurImage->Width; w++)
					{
						iCurImage->Data[Pos + w * 3 + c] = RowData[w];
					}
					if (iCurImage->Width % 2 != 0)  // Pads to even width
						igetc();
				}
				Pos += Header.WidthPixels * 3;
			}
			break;

		case 8:
			if (!ilTexImage(Header.WidthPixels, Header.HeightPixels, 1, Header.NumChans, 0, IL_UNSIGNED_BYTE, NULL))
			{
				ifree(RowData);
				return IL_FALSE;
			}
			iCurImage->Format = IL_LUMINANCE;

			for (ILuint h = 0; h < iCurImage->Height; h++)
			{
				iread(iCurImage->Data + Pos, 1, iCurImage->Width);
				if (iCurImage->Width % 2 != 0)  // Pads to even width
					igetc();
				Pos += Header.WidthPixels;
			}

			break;

		//case 15:
		//default:
	}
	iCurImage->Origin = IL_ORIGIN_UPPER_LEFT;

	return ilFixImage();
};



#endif//IL_NO_SCITEX