- Main Page
- Data Structures
- Files

- File List

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

    00001 //-----------------------------------------------------------------------------
    00002 //
    00003 // ImageLib Sources
    00004 // Copyright (C) 2000-2002 by Denton Woods
    00005 // Last modified: 05/21/2002 <--Y2K Compliant! =]
    00006 //
    00007 // Filename: src-IL/src/il_pnm.c
    00008 //
    00009 // Description: Reads/writes to/from pbm/pgm/ppm formats (enough slashes? =)
    00010 //
    00011 //-----------------------------------------------------------------------------
    00012
    00013
    00014
    00015 #include "il_internal.h"
    00016 #ifndef IL_NO_PNM
    00017 #include "il_pnm.h"
    00018 #include <limits.h>  // for maximum values
    00019 #include <ctype.h>
    00020 #include "il_manip.h"
    00021 #include "il_bits.h"
    00022
    00023 // According to the ppm specs, it's 70, but PSP
    00024 //  likes to output longer lines.
    00025 #define MAX_BUFFER 180
    00026 static ILbyte LineBuffer[MAX_BUFFER];
    00027 static ILbyte SmallBuff[MAX_BUFFER];
    00028
    00029 // Can't read direct bits from a lump yet
    00030 ILboolean IsLump = IL_FALSE;
    00031
    00032
    00034 ILboolean ilIsValidPnm(ILconst_string FileName)
    00035 {
    00036         ILHANDLE        PnmFile;
    00037         ILboolean       bPnm = IL_FALSE;
    00038
    00039         if (   !iCheckExtension(FileName, IL_TEXT("pbm"))
    00040                 && !iCheckExtension(FileName, IL_TEXT("pgm"))
    00041                 && !iCheckExtension(FileName, IL_TEXT("ppm"))
    00042                 && !iCheckExtension(FileName, IL_TEXT("pnm"))) {
    00043                 ilSetError(IL_INVALID_EXTENSION);
    00044                 return bPnm;
    00045         }
    00046
    00047         PnmFile = iopenr(FileName);
    00048         if (PnmFile == NULL) {
    00049                 ilSetError(IL_COULD_NOT_OPEN_FILE);
    00050                 return bPnm;
    00051         }
    00052
    00053         bPnm = ilIsValidPnmF(PnmFile);
    00054         icloser(PnmFile);
    00055
    00056         return bPnm;
    00057 }
    00058
    00059
    00061 ILboolean ilIsValidPnmF(ILHANDLE File)
    00062 {
    00063         ILuint          FirstPos;
    00064         ILboolean       bRet;
    00065
    00066         iSetInputFile(File);
    00067         FirstPos = itell();
    00068         bRet = iIsValidPnm();
    00069         iseek(FirstPos, IL_SEEK_SET);
    00070
    00071         return bRet;
    00072 }
    00073
    00074
    00076 ILboolean ilIsValidPnmL(const void *Lump, ILuint Size)
    00077 {
    00078         iSetInputLump(Lump, Size);
    00079         return iIsValidPnm();
    00080 }
    00081
    00082
    00083 // Internal function to get the header and check it.
    00084 ILboolean iIsValidPnm()
    00085 {
    00086         char    Head[2];
    00087         ILint   Read;
    00088
    00089         Read = iread(Head, 1, 2);
    00090         iseek(-Read, IL_SEEK_CUR);  // Go ahead and restore to previous state
    00091         if (Read != 2)
    00092                 return IL_FALSE;
    00093
    00094         return iCheckPnm(Head);
    00095 }
    00096
    00097
    00098 // Internal function used to check if the HEADER is a valid .pnm header.
    00099 ILboolean iCheckPnm(char Header[2])
    00100 {
    00101         if (Header[0] != 'P')
    00102                 return IL_FALSE;
    00103         switch (Header[1])
    00104         {
    00105                 case '1':
    00106                 case '2':
    00107                 case '3':
    00108                 case '4':
    00109                 case '5':
    00110                 case '6':
    00111                         return IL_TRUE;
    00112         }
    00113
    00114         return IL_FALSE;
    00115 }
    00116
    00117
    00118 // Reads a file
    00119 ILboolean ilLoadPnm(ILconst_string FileName)
    00120 {
    00121         ILHANDLE        PnmFile;
    00122         ILboolean       bPnm = IL_FALSE;
    00123
    00124         PnmFile = iopenr(FileName);
    00125         if (PnmFile == NULL) {
    00126                 ilSetError(IL_COULD_NOT_OPEN_FILE);
    00127                 return bPnm;
    00128         }
    00129
    00130         bPnm = ilLoadPnmF(PnmFile);
    00131         icloser(PnmFile);
    00132
    00133         return bPnm;
    00134 }
    00135
    00136
    00137 // Reads an already-opened file
    00138 ILboolean ilLoadPnmF(ILHANDLE File)
    00139 {
    00140         ILuint          FirstPos;
    00141         ILboolean       bRet;
    00142
    00143         iSetInputFile(File);
    00144         FirstPos = itell();
    00145         bRet = iLoadPnmInternal();
    00146         iseek(FirstPos, IL_SEEK_SET);
    00147
    00148         return bRet;
    00149 }
    00150
    00151
    00152 // Reads from a memory "lump"
    00153 ILboolean ilLoadPnmL(const void *Lump, ILuint Size)
    00154 {
    00155         iSetInputLump(Lump, Size);
    00156         return iLoadPnmInternal();
    00157 }
    00158
    00159
    00160 // Load either a pgm or a ppm
    00161 ILboolean iLoadPnmInternal()
    00162 {
    00163         ILimage         *PmImage = NULL;
    00164         PPMINFO         Info;
    00165 //      ILuint          LineInc = 0, SmallInc = 0;
    00166
    00167         Info.Type = 0;
    00168
    00169         if (iCurImage == NULL) {
    00170                 ilSetError(IL_ILLEGAL_OPERATION);
    00171                 return IL_FALSE;
    00172         }
    00173
    00174         // Find out what type of pgm/ppm this is
    00175         if (iGetWord(IL_FALSE) == IL_FALSE)
    00176                 return IL_FALSE;
    00177
    00178         if (SmallBuff[0] != 'P') {
    00179                 ilSetError(IL_INVALID_FILE_HEADER);
    00180                 return IL_FALSE;
    00181         }
    00182
    00183         switch( SmallBuff[1] ) {
    00184                 case '1':
    00185                         Info.Type = IL_PBM_ASCII;
    00186                         break;
    00187                 case '2':
    00188                         Info.Type = IL_PGM_ASCII;
    00189                         break;
    00190                 case '3':
    00191                         Info.Type = IL_PPM_ASCII;
    00192                         break;
    00193                 case '4':
    00194                         Info.Type = IL_PBM_BINARY;
    00195                         if (IsLump) {
    00196                                 ilSetError(IL_FORMAT_NOT_SUPPORTED);
    00197                                 return IL_FALSE;
    00198                         }
    00199                         break;
    00200                 case '5':
    00201                         Info.Type = IL_PGM_BINARY;
    00202                         break;
    00203                 case '6':
    00204                         Info.Type = IL_PPM_BINARY;
    00205                         break;
    00206                 default:
    00207                         ilSetError(IL_INVALID_FILE_HEADER);
    00208                         return IL_FALSE;
    00209         }
    00210
    00211         // Retrieve the width and height
    00212         if (iGetWord(IL_FALSE) == IL_FALSE)
    00213                 return IL_FALSE;
    00214         Info.Width = atoi((const char*)SmallBuff);
    00215         if (Info.Width == 0) {
    00216                 ilSetError(IL_INVALID_FILE_HEADER);
    00217                 return IL_FALSE;
    00218         }
    00219
    00220         if (iGetWord(IL_FALSE) == IL_FALSE)
    00221                 return IL_FALSE;
    00222         Info.Height = atoi((const char*)SmallBuff);
    00223         if (Info.Height == 0) {
    00224                 ilSetError(IL_INVALID_FILE_HEADER);
    00225                 return IL_FALSE;
    00226         }
    00227
    00228         // Retrieve the maximum colour component value
    00229         if (Info.Type != IL_PBM_ASCII && Info.Type != IL_PBM_BINARY) {
    00230                 if (iGetWord(IL_TRUE) == IL_FALSE)
    00231                         return IL_FALSE;
    00232                 if ((Info.MaxColour = atoi((const char*)SmallBuff)) == 0) {
    00233                         ilSetError(IL_INVALID_FILE_HEADER);
    00234                         return IL_FALSE;
    00235                 }
    00236         } else {
    00237                 Info.MaxColour = 1;
    00238         }
    00239
    00240         if (Info.Type == IL_PBM_ASCII || Info.Type == IL_PBM_BINARY ||
    00241                 Info.Type == IL_PGM_ASCII || Info.Type == IL_PGM_BINARY) {
    00242                 if (Info.Type == IL_PGM_ASCII) {
    00243                         Info.Bpp = Info.MaxColour < 256 ? 1 : 2;
    00244                 } else {
    00245                         Info.Bpp = 1;
    00246                 }
    00247         } else {
    00248                 Info.Bpp = 3;
    00249         }
    00250
    00251         switch (Info.Type) {
    00252                 case IL_PBM_ASCII:
    00253                 case IL_PGM_ASCII:
    00254                 case IL_PPM_ASCII:
    00255                         PmImage = ilReadAsciiPpm(&Info);
    00256                         break;
    00257                 case IL_PBM_BINARY:
    00258                         PmImage = ilReadBitPbm(&Info);
    00259                         break;
    00260                 case IL_PGM_BINARY:
    00261                 case IL_PPM_BINARY:
    00262                         PmImage = ilReadBinaryPpm(&Info);
    00263                         break;
    00264                 default:
    00265                         return IL_FALSE;
    00266         }
    00267
    00268         if (PmImage == NULL) {
    00269             iCurImage->Format = ilGetFormatBpp(iCurImage->Bpp);
    00270             ilSetError(IL_FILE_READ_ERROR);
    00271             return IL_FALSE;
    00272         }
    00273
    00274         // Is this conversion needed?  Just 0's and 1's shows up as all black
    00275         if (Info.Type == IL_PBM_ASCII) {
    00276                 PbmMaximize(PmImage);
    00277         }
    00278
    00279         if (Info.MaxColour > 255)
    00280                 PmImage->Type = IL_UNSIGNED_SHORT;
    00281         PmImage->Origin = IL_ORIGIN_UPPER_LEFT;
    00282         if (Info.Type == IL_PBM_ASCII || Info.Type == IL_PBM_BINARY ||
    00283                 Info.Type == IL_PGM_ASCII || Info.Type == IL_PGM_BINARY)
    00284                 PmImage->Format = IL_LUMINANCE;
    00285         else
    00286                 PmImage->Format = IL_RGB;
    00287         PmImage->Origin = IL_ORIGIN_UPPER_LEFT;
    00288
    00289         ilFixImage();
    00290
    00291         if (PmImage == NULL)
    00292                 return IL_FALSE;
    00293
    00294         return IL_TRUE;
    00295 }
    00296
    00297
    00298
    00299 ILimage *ilReadAsciiPpm(PPMINFO *Info)
    00300 {
    00301         ILint   LineInc = 0, SmallInc = 0, DataInc = 0, Size;
    00302 //      ILint   BytesRead = 0;
    00303
    00304         if (Info->MaxColour > 255)
    00305                 Info->Bpp *= 2;
    00306
    00307         Size = Info->Width * Info->Height * Info->Bpp;
    00308
    00309         if (!ilTexImage(Info->Width, Info->Height, 1, (ILubyte)(Info->Bpp), 0, IL_UNSIGNED_BYTE, NULL)) {
    00310                 return IL_FALSE;
    00311         }
    00312         iCurImage->Origin = IL_ORIGIN_UPPER_LEFT;
    00313         if (Info->MaxColour > 255)
    00314                 iCurImage->Type = IL_UNSIGNED_SHORT;
    00315
    00316         while (DataInc < Size) {  // && !feof(File)) {
    00317                 LineInc = 0;
    00318
    00319                 if (iFgets((char *)LineBuffer, MAX_BUFFER) == NULL) {
    00320                         //ilSetError(IL_ILLEGAL_FILE_VALUE);
    00321                         //return NULL;
    00322                         //return iCurImage;
    00323                         break;
    00324                 }
    00325                 if (LineBuffer[0] == '#') {  // Comment
    00326                         continue;
    00327                 }
    00328
    00329                 while ((LineBuffer[LineInc] != NUL) && (LineBuffer[LineInc] != '\n')) {
    00330
    00331                         SmallInc = 0;
    00332                         while (!isalnum(LineBuffer[LineInc])) {  // Skip any whitespace
    00333                                 LineInc++;
    00334                         }
    00335                         while (isalnum(LineBuffer[LineInc])) {
    00336                                 SmallBuff[SmallInc] = LineBuffer[LineInc];
    00337                                 SmallInc++;
    00338                                 LineInc++;
    00339                         }
    00340                         SmallBuff[SmallInc] = NUL;
    00341                         iCurImage->Data[DataInc] = atoi((const char*)SmallBuff);  // Convert from string to colour
    00342
    00343                         // PSP likes to put whitespace at the end of lines...figures. =/
    00344                         while (!isalnum(LineBuffer[LineInc]) && LineBuffer[LineInc] != NUL) {  // Skip any whitespace
    00345                                 LineInc++;
    00346                         }
    00347
    00348                         // We should set some kind of state flag that enables this
    00349                         //Image->Data[DataInc] *= (ILubyte)(255 / Info->MaxColour);  // Scales to 0-255
    00350                         if (Info->MaxColour > 255)
    00351                                 DataInc++;
    00352                         DataInc++;
    00353                 }
    00354         }
    00355
    00356         // If we read less than what we should have...
    00357         if (DataInc < Size) {
    00358                 //ilCloseImage(iCurImage);
    00359                 //ilSetCurImage(NULL);
    00360                 ilSetError(IL_ILLEGAL_FILE_VALUE);
    00361                 return NULL;
    00362         }
    00363
    00364         return iCurImage;
    00365 }
    00366
    00367
    00368 ILimage *ilReadBinaryPpm(PPMINFO *Info)
    00369 {
    00370         ILuint  Size;
    00371
    00372         Size = Info->Width * Info->Height * Info->Bpp;
    00373
    00374         if (!ilTexImage(Info->Width, Info->Height, 1, (ILubyte)(Info->Bpp), 0, IL_UNSIGNED_BYTE, NULL)) {
    00375                 return IL_FALSE;
    00376         }
    00377         iCurImage->Origin = IL_ORIGIN_UPPER_LEFT;
    00378
    00379         /* 4/3/2007 Dario Meloni
    00380          Here it seems we have eaten too much bytes and it is needed to fix
    00381          the starting point
    00382          works well on various images
    00383
    00384         No more need of this workaround. fixed iGetWord
    00385         iseek(0,IL_SEEK_END);
    00386         ILuint size = itell();
    00387         iseek(size-Size,IL_SEEK_SET);
    00388         */
    00389         if (iread(iCurImage->Data, 1, Size ) != Size) {
    00390                 ilCloseImage(iCurImage);
    00391                 return NULL;
    00392         }
    00393         return iCurImage;
    00394 }
    00395
    00396
    00397 ILimage *ilReadBitPbm(PPMINFO *Info)
    00398 {
    00399         ILuint  m, j, x, CurrByte;
    00400
    00401         if (!ilTexImage(Info->Width, Info->Height, 1, (ILubyte)(Info->Bpp), 0, IL_UNSIGNED_BYTE, NULL)) {
    00402                 return IL_FALSE;
    00403         }
    00404         iCurImage->Origin = IL_ORIGIN_UPPER_LEFT;
    00405
    00406         x = 0;
    00407         for (j = 0; j < iCurImage->SizeOfData;) {
    00408                 CurrByte = igetc();
    00409                 for (m = 128; m > 0 && x < Info->Width; m >>= 1, ++x, ++j) {
    00410                         iCurImage->Data[j] = (CurrByte & m)?255:0;
    00411                 }
    00412                 if (x == Info->Width)
    00413                         x = 0;
    00414         }
    00415
    00416         return iCurImage;
    00417 }
    00418
    00419
    00420 ILboolean iGetWord(ILboolean final)
    00421 {
    00422         ILint WordPos = 0;
    00423         ILint Current = 0;
    00424         ILboolean Started = IL_FALSE;
    00425         ILboolean Looping = IL_TRUE;
    00426
    00427         if (ieof())
    00428                 return IL_FALSE;
    00429
    00430         while (Looping) {
    00431                 while ((Current = igetc()) != IL_EOF && Current != '\n' && Current != '#' && Current != ' ') {
    00432                         if (WordPos >= MAX_BUFFER)  // We have hit the maximum line length.
    00433                                 return IL_FALSE;
    00434
    00435                         if (!isalnum(Current)) {
    00436                                 if (Started) {
    00437                                         Looping = IL_FALSE;
    00438                                         break;
    00439                                 }
    00440                                 continue;
    00441                         }
    00442
    00443                         if (Looping)
    00444                                 SmallBuff[WordPos++] = Current;
    00445                 }
    00446                 if (Current == IL_EOF)
    00447                         return IL_FALSE;
    00448                 if (final == IL_TRUE)
    00449                 break;
    00450                 SmallBuff[WordPos] = 0; // 08-17-2008 - was NULL, changed to avoid warning
    00451
    00452                 if (!Looping)
    00453                         break;
    00454
    00455                 if (Current == '#') {  // '#' is a comment...read until end of line
    00456                         while ((Current = igetc()) != IL_EOF && Current != '\n');
    00457                 }
    00458
    00459                 // Get rid of any erroneous spaces
    00460                 while ((Current = igetc()) != IL_EOF) {
    00461                         if (Current != ' ')
    00462                                 break;
    00463                 }
    00464                 iseek(-1, IL_SEEK_CUR);
    00465
    00466                 if (WordPos > 0)
    00467                         break;
    00468         }
    00469
    00470         if (Current == -1 || WordPos == 0) {
    00471                 ilSetError(IL_INVALID_FILE_HEADER);
    00472                 return IL_FALSE;
    00473         }
    00474
    00475         return IL_TRUE;
    00476 }
    00477
    00478
    00479 ILstring FName = NULL;
    00480
    00482 ILboolean ilSavePnm(const ILstring FileName)
    00483 {
    00484         ILHANDLE        PnmFile;
    00485         ILuint          PnmSize;
    00486
    00487         if (ilGetBoolean(IL_FILE_MODE) == IL_FALSE) {
    00488                 if (iFileExists(FileName)) {
    00489                         ilSetError(IL_FILE_ALREADY_EXISTS);
    00490                         return IL_FALSE;
    00491                 }
    00492         }
    00493
    00494         PnmFile = iopenw(FileName);
    00495         if (PnmFile == NULL) {
    00496                 ilSetError(IL_COULD_NOT_OPEN_FILE);
    00497                 return IL_FALSE;
    00498         }
    00499
    00500         PnmSize = ilSavePnmF(PnmFile);
    00501         iclosew(PnmFile);
    00502
    00503         if (PnmSize == 0)
    00504                 return IL_FALSE;
    00505         return IL_TRUE;
    00506 }
    00507
    00508
    00510 ILuint ilSavePnmF(ILHANDLE File)
    00511 {
    00512         ILuint Pos;
    00513         iSetOutputFile(File);
    00514         Pos = itellw();
    00515         if (iSavePnmInternal() == IL_FALSE)
    00516                 return 0;  // Error occurred
    00517         return itellw() - Pos;  // Return the number of bytes written.
    00518 }
    00519
    00520
    00522 ILuint ilSavePnmL(void *Lump, ILuint Size)
    00523 {
    00524         ILuint Pos = itellw();
    00525         FName = NULL;
    00526         iSetOutputLump(Lump, Size);
    00527         if (iSavePnmInternal() == IL_FALSE)
    00528                 return 0;  // Error occurred
    00529         return itellw() - Pos;  // Return the number of bytes written.
    00530 }
    00531
    00532
    00533 // Internal function used to save the Pnm.
    00534 ILboolean iSavePnmInternal()
    00535 {
    00536         ILuint          Bpp, MaxVal = UCHAR_MAX, i = 0, j, k;
    00537         ILenum          Type = 0;
    00538         ILuint          LinePos = 0;  // Cannot exceed 70 for pnm's!
    00539         ILboolean       Binary;
    00540         ILimage         *TempImage;
    00541         ILubyte         *TempData;
    00542
    00543         if (iCurImage == NULL) {
    00544                 ilSetError(IL_ILLEGAL_OPERATION);
    00545                 return IL_FALSE;
    00546         }
    00547
    00548         if (iCheckExtension(FName, IL_TEXT("pbm")))
    00549                 Type = IL_PBM_ASCII;
    00550         else if (iCheckExtension(FName, IL_TEXT("pgm")))
    00551                 Type = IL_PGM_ASCII;
    00552         else if (iCheckExtension(FName, IL_TEXT("ppm")))
    00553                 Type = IL_PPM_ASCII;
    00554         else
    00555                 Type = IL_PPM_ASCII;
    00556
    00557         /*if (!Type) {
    00558                 ilSetError(IL_INVALID_EXTENSION);
    00559                 return IL_FALSE;
    00560         }*/
    00561
    00562         if (iGetHint(IL_COMPRESSION_HINT) == IL_USE_COMPRESSION) {
    00563                 Type += 3;
    00564                 Binary = IL_TRUE;
    00565         }
    00566         else {
    00567                 Binary = IL_FALSE;
    00568         }
    00569
    00570         if (iCurImage->Type == IL_UNSIGNED_BYTE) {
    00571                 MaxVal = UCHAR_MAX;
    00572         }
    00573         else if (iCurImage->Type == IL_UNSIGNED_SHORT) {
    00574                 MaxVal = USHRT_MAX;
    00575         }
    00576         else {
    00577                 ilSetError(IL_FORMAT_NOT_SUPPORTED);
    00578                 return IL_FALSE;
    00579         }
    00580         if (MaxVal > UCHAR_MAX && Type >= IL_PBM_BINARY) {  // binary cannot be higher than 255
    00581                 ilSetError(IL_FORMAT_NOT_SUPPORTED);
    00582                 return IL_FALSE;
    00583         }
    00584
    00585         switch (Type)
    00586         {
    00587                 case IL_PBM_ASCII:
    00588                         Bpp = 1;
    00589                         ilprintf("P1\n");
    00590                         TempImage = iConvertImage(iCurImage, IL_LUMINANCE, IL_UNSIGNED_BYTE);
    00591                         break;
    00592                 //case IL_PBM_BINARY:  // Don't want to mess with saving bits just yet...
    00593                         //Bpp = 1;
    00594                         //ilprintf("P4\n");
    00595                         //break;
    00596                 case IL_PBM_BINARY:
    00597                         ilSetError(IL_FORMAT_NOT_SUPPORTED);
    00598                         return IL_FALSE;
    00599                 case IL_PGM_ASCII:
    00600                         Bpp = 1;
    00601                         ilprintf("P2\n");
    00602                         TempImage = iConvertImage(iCurImage, IL_COLOUR_INDEX, IL_UNSIGNED_BYTE);
    00603                         break;
    00604                 case IL_PGM_BINARY:
    00605                         Bpp = 1;
    00606                         ilprintf("P5\n");
    00607                         TempImage = iConvertImage(iCurImage, IL_COLOUR_INDEX, IL_UNSIGNED_BYTE);
    00608                         break;
    00609                 case IL_PPM_ASCII:
    00610                         Bpp = 3;
    00611                         ilprintf("P3\n");
    00612                         TempImage = iConvertImage(iCurImage, IL_RGB, IL_UNSIGNED_BYTE);
    00613                         break;
    00614                 case IL_PPM_BINARY:
    00615                         Bpp = 3;
    00616                         ilprintf("P6\n");
    00617                         TempImage = iConvertImage(iCurImage, IL_RGB, IL_UNSIGNED_BYTE);
    00618                         break;
    00619                 default:
    00620                         ilSetError(IL_INTERNAL_ERROR);
    00621                         return IL_FALSE;
    00622         }
    00623
    00624         if (TempImage == NULL)
    00625                 return IL_FALSE;
    00626
    00627         if (Bpp != TempImage->Bpp) {
    00628                 ilSetError(IL_INVALID_CONVERSION);
    00629                 return IL_FALSE;
    00630         }
    00631
    00632         if (TempImage->Origin != IL_ORIGIN_UPPER_LEFT) {
    00633                 TempData = iGetFlipped(TempImage);
    00634                 if (TempData == NULL) {
    00635                         ilCloseImage(TempImage);
    00636                         return IL_FALSE;
    00637                 }
    00638         }
    00639         else {
    00640                 TempData = TempImage->Data;
    00641         }
    00642
    00643         ilprintf("%d %d\n", TempImage->Width, TempImage->Height);
    00644         if (Type != IL_PBM_BINARY && Type != IL_PBM_ASCII)  // not needed for .pbm's (only 0 and 1)
    00645                 ilprintf("%d\n", MaxVal);
    00646
    00647         while (i < TempImage->SizeOfPlane) {
    00648                 for (j = 0; j < Bpp; j++) {
    00649                         if (Binary) {
    00650                                 if (Type == IL_PBM_BINARY) {
    00651                                         iputc((ILubyte)(TempData[i] > 127 ? 1 : 0));
    00652                                 }
    00653                                 else {
    00654                                         iputc(TempData[i]);
    00655                                 }
    00656                         }
    00657                         else {
    00658                                 if (TempImage->Type == IL_UNSIGNED_BYTE)
    00659                                         k = TempData[i];
    00660                                 else  // IL_UNSIGNED_SHORT
    00661                                         k = *((ILushort*)TempData + i);
    00662                                 if (Type == IL_PBM_ASCII) {
    00663                                         LinePos += ilprintf("%d ", TempData[i] > 127 ? 1 : 0);
    00664                                 }
    00665                                 else {
    00666                                         LinePos += ilprintf("%d ", TempData[i]);
    00667                                 }
    00668                         }
    00669
    00670                         if (TempImage->Type == IL_UNSIGNED_SHORT)
    00671                                 i++;
    00672                         i++;
    00673                 }
    00674
    00675                 if (LinePos > 65) {  // Just a good number =]
    00676                         ilprintf("\n");
    00677                         LinePos = 0;
    00678                 }
    00679         }
    00680
    00681         if (TempImage->Origin != IL_ORIGIN_UPPER_LEFT)
    00682                 ifree(TempData);
    00683         ilCloseImage(TempImage);
    00684
    00685         return IL_TRUE;
    00686 }
    00687
    00688
    00689 // Converts a .pbm to something viewable.
    00690 void PbmMaximize(ILimage *Image)
    00691 {
    00692         ILuint i = 0;
    00693         for (i = 0; i < Image->SizeOfPlane; i++)
    00694                 if (Image->Data[i] == 1)
    00695                         Image->Data[i] = 0xFF;
    00696         return;
    00697 }
    00698
    00699
    00700 #endif//IL_NO_PNM