- Main Page
- Data Structures
- Files

- File List

# E:/DevIL/src-IL/src/il_header.c

    00001 //-----------------------------------------------------------------------------
    00002 //
    00003 // ImageLib Sources
    00004 // Copyright (C) 2000-2008 by Denton Woods
    00005 // Last modified: 10/10/2006
    00006 //
    00007 // Filename: src-IL/src/il_header.c
    00008 //
    00009 // Description: Generates a C-style header file for the current image.
    00010 //
    00011 //-----------------------------------------------------------------------------
    00012
    00013 #ifndef IL_NO_CHEAD
    00014
    00015 #include "il_internal.h"
    00016
    00017 // Just a guess...let's see what's purty!
    00018 #define MAX_LINE_WIDTH 14
    00019
    00021 ILboolean ilSaveCHeader(ILconst_string FileName, char *InternalName)
    00022 {
    00023         FILE            *HeadFile;
    00024         ILuint          i = 0, j;
    00025         ILimage         *TempImage;
    00026         const char      *Name;
    00027
    00028         if (iCurImage == NULL) {
    00029                 ilSetError(IL_ILLEGAL_OPERATION);
    00030                 return IL_FALSE;
    00031         }
    00032
    00033         Name = iGetString(IL_CHEAD_HEADER_STRING);
    00034         if (Name == NULL)
    00035                 Name = InternalName;
    00036
    00037         if (FileName == NULL || Name == NULL ||
    00038                 ilStrLen(FileName) < 1 || ilCharStrLen(Name) < 1) {
    00039                 ilSetError(IL_INVALID_VALUE);
    00040                 return IL_FALSE;
    00041         }
    00042
    00043         if (!iCheckExtension(FileName, IL_TEXT("h"))) {
    00044                 ilSetError(IL_INVALID_EXTENSION);
    00045                 return IL_FALSE;
    00046         }
    00047
    00048         if (ilGetBoolean(IL_FILE_MODE) == IL_FALSE) {
    00049                 if (iFileExists(FileName)) {
    00050                         ilSetError(IL_FILE_ALREADY_EXISTS);
    00051             return IL_FALSE;
    00052                 }
    00053         }
    00054
    00055         if (iCurImage->Bpc > 1) {
    00056                 TempImage = iConvertImage(iCurImage, iCurImage->Format, IL_UNSIGNED_BYTE);
    00057                 if (TempImage == NULL)
    00058            return IL_FALSE;
    00059         } else {
    00060                 TempImage = iCurImage;
    00061         }
    00062
    00063 #ifndef _UNICODE
    00064         HeadFile = fopen(FileName, "wb");
    00065 #else
    00066         #ifdef _WIN32
    00067                 HeadFile = _wfopen(FileName, L"rb");
    00068         #else
    00069                 HeadFile = fopen((char*)FileName, "rb");
    00070         #endif
    00071
    00072 #endif//_UNICODE
    00073
    00074         if (HeadFile == NULL) {
    00075                 ilSetError(IL_COULD_NOT_OPEN_FILE);
    00076         return IL_FALSE;
    00077         }
    00078
    00079         fprintf(HeadFile, "//#include <il/il.h>\n");
    00080         fprintf(HeadFile, "// C Image Header:\n\n\n");
    00081         fprintf(HeadFile, "// IMAGE_BPP is in bytes per pixel, *not* bits\n");
    00082     fprintf(HeadFile, "#define IMAGE_BPP %d\n",iCurImage->Bpp);
    00083         fprintf(HeadFile, "#define IMAGE_WIDTH   %d\n", iCurImage->Width);
    00084         fprintf(HeadFile, "#define IMAGE_HEIGHT  %d\n", iCurImage->Height);
    00085         fprintf(HeadFile, "#define IMAGE_DEPTH   %d\n\n\n", iCurImage->Depth);
    00086         fprintf(HeadFile, "#define IMAGE_TYPE    0x%X\n", iCurImage->Type);
    00087         fprintf(HeadFile, "#define IMAGE_FORMAT  0x%X\n\n\n", iCurImage->Format);
    00088     fprintf(HeadFile, "ILubyte %s[] = {\n", Name);
    00089
    00090
    00091         for (; i < TempImage->SizeOfData; i += MAX_LINE_WIDTH) {
    00092                 fprintf(HeadFile, "\t");
    00093                 for (j = 0; j < MAX_LINE_WIDTH; j++) {
    00094                         if (i + j >= TempImage->SizeOfData - 1) {
    00095                                 fprintf(HeadFile, "%4d", TempImage->Data[i+j]);
    00096                                 break;
    00097                         }
    00098                         else
    00099                                 fprintf(HeadFile, "%4d,", TempImage->Data[i+j]);
    00100                 }
    00101                 fprintf(HeadFile, "\n");
    00102         }
    00103         if (TempImage != iCurImage)
    00104                 ilCloseImage(TempImage);
    00105
    00106         fprintf(HeadFile, "};\n");
    00107
    00108
    00109         if (iCurImage->Pal.Palette && iCurImage->Pal.PalSize && iCurImage->Pal.PalType != IL_PAL_NONE) {
    00110                 fprintf(HeadFile, "\n\n");
    00111                 fprintf(HeadFile, "#define IMAGE_PALSIZE %u\n\n", iCurImage->Pal.PalSize);
    00112                 fprintf(HeadFile, "#define IMAGE_PALTYPE 0x%X\n\n", iCurImage->Pal.PalType);
    00113         fprintf(HeadFile, "ILubyte %sPal[] = {\n", Name);
    00114                 for (i = 0; i < iCurImage->Pal.PalSize; i += MAX_LINE_WIDTH) {
    00115                         fprintf(HeadFile, "\t");
    00116                         for (j = 0; j < MAX_LINE_WIDTH; j++) {
    00117                                 if (i + j >= iCurImage->Pal.PalSize - 1) {
    00118                                         fprintf(HeadFile, " %4d", iCurImage->Pal.Palette[i+j]);
    00119                                         break;
    00120                                 }
    00121                                 else
    00122                                         fprintf(HeadFile, " %4d,", iCurImage->Pal.Palette[i+j]);
    00123                         }
    00124                         fprintf(HeadFile, "\n");
    00125                 }
    00126
    00127                 fprintf(HeadFile, "};\n");
    00128         }
    00129         fclose(HeadFile);
    00130         return IL_TRUE;
    00131 }
    00132
    00133
    00134
    00135 #endif//IL_NO_CHEAD