iotest.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //-----------------------------------------------------------------------------
  2. //
  3. // ImageLib Source Example
  4. // Copyright (C) 2000-2001 by Denton Woods
  5. // Last modified: 09/06/2001 <--Y2K Compliant! =]
  6. //
  7. // Filename: examples/file override/file override.c
  8. //
  9. // Description: An example of overriding the DevIL reading and
  10. // writing functions via ilSetRead and ilSetWrite.
  11. //
  12. //-----------------------------------------------------------------------------
  13. // Required include files.
  14. #ifdef HAVE_CONFIG_H
  15. #include "config.h"
  16. #endif /* HAVE_CONFIG_H */
  17. #include <IL/il.h>
  18. #include <stdio.h>
  19. /* We would need ILU just because of iluErrorString() function... */
  20. /* So make it possible for both with and without ILU! */
  21. #ifdef ILU_ENABLED
  22. #include <IL/ilu.h>
  23. #define PRINT_ERROR_MACRO printf("Error: %s\n", iluErrorString(Error))
  24. #else /* not ILU_ENABLED */
  25. #define PRINT_ERROR_MACRO printf("Error: 0x%X\n", (unsigned int)Error)
  26. #endif /* not ILU_ENABLED */
  27. ILHANDLE ILAPIENTRY iOpenRead(const char *FileName)
  28. {
  29. return (ILHANDLE)fopen(FileName, "rb");
  30. }
  31. void ILAPIENTRY iCloseRead(ILHANDLE Handle)
  32. {
  33. fclose((FILE*)Handle);
  34. return;
  35. }
  36. ILHANDLE ILAPIENTRY iOpenWrite(const char *FileName)
  37. {
  38. return (ILHANDLE)fopen(FileName, "wb");
  39. }
  40. void ILAPIENTRY iCloseWrite(ILHANDLE Handle)
  41. {
  42. fclose((FILE*)Handle);
  43. return;
  44. }
  45. ILboolean ILAPIENTRY iEof(ILHANDLE Handle)
  46. {
  47. return (feof((FILE*)Handle) != 0);
  48. }
  49. ILint ILAPIENTRY iGetc(ILHANDLE Handle)
  50. {
  51. return fgetc((FILE*)Handle);
  52. }
  53. ILint ILAPIENTRY iPutc(ILubyte Char, ILHANDLE Handle)
  54. {
  55. return fputc(Char, (FILE*)Handle);
  56. }
  57. ILint ILAPIENTRY iRead(void *Buffer, ILuint Size, ILuint Number, ILHANDLE Handle)
  58. {
  59. return fread(Buffer, Size, Number, (FILE*)Handle);
  60. }
  61. ILint ILAPIENTRY iWrite(const void *Buffer, ILuint Size, ILuint Number, ILHANDLE Handle)
  62. {
  63. return fwrite(Buffer, Size, Number, (FILE*)Handle);
  64. }
  65. ILint ILAPIENTRY iReadSeek(ILHANDLE Handle, ILint Offset, ILint Mode)
  66. {
  67. return fseek((FILE*)Handle, Offset, Mode);
  68. }
  69. ILint ILAPIENTRY iWriteSeek(ILHANDLE Handle, ILint Offset, ILint Mode)
  70. {
  71. return fseek((FILE*)Handle, Offset, Mode);
  72. }
  73. ILint ILAPIENTRY iReadTell(ILHANDLE Handle)
  74. {
  75. return ftell((FILE*)Handle);
  76. }
  77. ILint ILAPIENTRY iWriteTell(ILHANDLE Handle)
  78. {
  79. return ftell((FILE*)Handle);
  80. }
  81. int main(int argc, char **argv)
  82. {
  83. ILuint ImgId;
  84. ILenum Error;
  85. // We use the filename specified in the first argument of the command-line.
  86. if (argc < 2) {
  87. printf("Please specify a file to open.\n");
  88. return 1;
  89. }
  90. // Check if the shared lib's version matches the executable's version.
  91. if (ilGetInteger(IL_VERSION_NUM) < IL_VERSION) {
  92. printf("DevIL version is different...exiting!\n");
  93. return 2;
  94. }
  95. // Initialize DevIL.
  96. ilInit();
  97. // Generate the main image name to use.
  98. ilGenImages(1, &ImgId);
  99. // Bind this image name.
  100. ilBindImage(ImgId);
  101. // Override the reading functions.
  102. ilSetRead(iOpenRead, iCloseRead, iEof, iGetc, iRead, iReadSeek, iReadTell);
  103. ilSetWrite(iOpenWrite, iCloseWrite, iPutc, iWriteSeek, iWriteTell, iWrite);
  104. // Loads the image specified by File into the image named by ImgId.
  105. if (!ilLoadImage(argv[1])) {
  106. printf("Could not open file...exiting.\n");
  107. return 3;
  108. }
  109. // Display the image's dimensions to the end user.
  110. printf("Width: %d Height: %d Depth: %d Bpp: %d\n", ilGetInteger(IL_IMAGE_WIDTH),
  111. ilGetInteger(IL_IMAGE_HEIGHT), ilGetInteger(IL_IMAGE_DEPTH), ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL));
  112. // Enable this to let us overwrite the destination file if it already exists.
  113. ilEnable(IL_FILE_OVERWRITE);
  114. // If argv[2] is present, we save to this filename, else we save to test.tga.
  115. if (argc > 2)
  116. ilSaveImage(argv[2]);
  117. else
  118. ilSaveImage("test.tga");
  119. // Reset the reading / writing functions when we're done loading specially.
  120. // This isn't required here, since we're exiting, but here's how it's done:
  121. ilResetRead();
  122. ilResetWrite();
  123. // We're done with the image, so let's delete it.
  124. ilDeleteImages(1, &ImgId);
  125. // Simple Error detection loop that displays the Error to the user in a human-readable form.
  126. while ((Error = ilGetError())) {
  127. PRINT_ERROR_MACRO;}
  128. return 0;
  129. }