1 //-----------------------------------------------------------------------------
2 //
3 // ImageLib Sources
4 // Copyright (C) 2000-2009 by Denton Woods
5 // Last modified: 03/07/2009
6 //
7 // Filename: src-IL/src/rawdata.c
8 //
9 // Description: "Raw" file functions
10 //
11 //-----------------------------------------------------------------------------
12 
13 
14 #include "il_internal.h"
15 //#ifndef IL_NO_DATA
16 #include "il_manip.h"
17 
18 
19 ILboolean iLoadDataInternal(ILuint Width, ILuint Height, ILuint Depth, ILubyte Bpp);
20 
21 
22 //! Reads a raw data file
ilLoadData(ILconst_string FileName,ILuint Width,ILuint Height,ILuint Depth,ILubyte Bpp)23 ILboolean ILAPIENTRY ilLoadData(ILconst_string FileName, ILuint Width, ILuint Height, ILuint Depth, ILubyte Bpp)
24 {
25 	ILHANDLE	RawFile;
26 	ILboolean	bRaw = IL_FALSE;
27 
28 	// No need to check for raw data
29 	/*if (!iCheckExtension(FileName, "raw")) {
30 		ilSetError(IL_INVALID_EXTENSION);
31 		return bRaw;
32 	}*/
33 
34 	RawFile = iopenr(FileName);
35 	if (RawFile == NULL) {
36 		ilSetError(IL_COULD_NOT_OPEN_FILE);
37 		return bRaw;
38 	}
39 
40 	bRaw = ilLoadDataF(RawFile, Width, Height, Depth, Bpp);
41 	icloser(RawFile);
42 
43 	return bRaw;
44 }
45 
46 
47 //! Reads an already-opened raw data file
ilLoadDataF(ILHANDLE File,ILuint Width,ILuint Height,ILuint Depth,ILubyte Bpp)48 ILboolean ILAPIENTRY ilLoadDataF(ILHANDLE File, ILuint Width, ILuint Height, ILuint Depth, ILubyte Bpp)
49 {
50 	ILuint		FirstPos;
51 	ILboolean	bRet;
52 
53 	iSetInputFile(File);
54 	FirstPos = itell();
55 	bRet = iLoadDataInternal(Width, Height, Depth, Bpp);
56 	iseek(FirstPos, IL_SEEK_SET);
57 
58 	return bRet;
59 }
60 
61 
62 //! Reads from a raw data memory "lump"
ilLoadDataL(void * Lump,ILuint Size,ILuint Width,ILuint Height,ILuint Depth,ILubyte Bpp)63 ILboolean ILAPIENTRY ilLoadDataL(void *Lump, ILuint Size, ILuint Width, ILuint Height, ILuint Depth, ILubyte Bpp)
64 {
65 	iSetInputLump(Lump, Size);
66 	return iLoadDataInternal(Width, Height, Depth, Bpp);
67 }
68 
69 
70 // Internal function to load a raw data image
iLoadDataInternal(ILuint Width,ILuint Height,ILuint Depth,ILubyte Bpp)71 ILboolean iLoadDataInternal(ILuint Width, ILuint Height, ILuint Depth, ILubyte Bpp)
72 {
73 	if (iCurImage == NULL || ((Bpp != 1) && (Bpp != 3) && (Bpp != 4))) {
74 		ilSetError(IL_ILLEGAL_OPERATION);
75 		return IL_FALSE;
76 	}
77 
78 	if (!ilTexImage(Width, Height, Depth, Bpp, 0, IL_UNSIGNED_BYTE, NULL)) {
79 		return IL_FALSE;
80 	}
81 	iCurImage->Origin = IL_ORIGIN_UPPER_LEFT;
82 
83 	// Tries to read the correct amount of data
84 	if (iread(iCurImage->Data, Width * Height * Depth * Bpp, 1) != 1)
85 		return IL_FALSE;
86 
87 	if (iCurImage->Bpp == 1)
88 		iCurImage->Format = IL_LUMINANCE;
89 	else if (iCurImage->Bpp == 3)
90 		iCurImage->Format = IL_RGB;
91 	else  // 4
92 		iCurImage->Format = IL_RGBA;
93 
94 	return ilFixImage();
95 }
96 
97 
98 //! Save the current image to FileName as raw data
ilSaveData(ILconst_string FileName)99 ILboolean ILAPIENTRY ilSaveData(ILconst_string FileName)
100 {
101 	ILHANDLE DataFile;
102 
103 	if (iCurImage == NULL) {
104 		ilSetError(IL_ILLEGAL_OPERATION);
105 		return IL_FALSE;
106 	}
107 
108 	DataFile = iopenr(FileName);
109 	if (DataFile == NULL) {
110 		ilSetError(IL_COULD_NOT_OPEN_FILE);
111 		return IL_FALSE;
112 	}
113 
114 	iwrite(iCurImage->Data, 1, iCurImage->SizeOfData);
115 	icloser(DataFile);
116 
117 	return IL_TRUE;
118 }
119 
120 
121 //#endif//IL_NO_DATA
122