iotest.c
Upload User: wmy0603
Upload Date: 2022-05-02
Package Size: 1808k
Code Size: 4k
Development Platform:

Visual C++

  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. #include <locale.h>
  20. ILHANDLE ILAPIENTRY iOpenRead(const char *FileName)
  21. {
  22. return (ILHANDLE)fopen(FileName, "rb");
  23. }
  24. void ILAPIENTRY iCloseRead(ILHANDLE Handle)
  25. {
  26. fclose((FILE*)Handle);
  27. return;
  28. }
  29. ILHANDLE ILAPIENTRY iOpenWrite(const char *FileName)
  30. {
  31. return (ILHANDLE)fopen(FileName, "wb");
  32. }
  33. void ILAPIENTRY iCloseWrite(ILHANDLE Handle)
  34. {
  35. fclose((FILE*)Handle);
  36. return;
  37. }
  38. ILboolean ILAPIENTRY iEof(ILHANDLE Handle)
  39. {
  40. return (feof((FILE*)Handle) != 0);
  41. }
  42. ILint ILAPIENTRY iGetc(ILHANDLE Handle)
  43. {
  44. return fgetc((FILE*)Handle);
  45. }
  46. ILint ILAPIENTRY iPutc(ILubyte Char, ILHANDLE Handle)
  47. {
  48. return fputc(Char, (FILE*)Handle);
  49. }
  50. ILint ILAPIENTRY iRead(void *Buffer, ILuint Size, ILuint Number, ILHANDLE Handle)
  51. {
  52. return fread(Buffer, Size, Number, (FILE*)Handle);
  53. }
  54. ILint ILAPIENTRY iWrite(const void *Buffer, ILuint Size, ILuint Number, ILHANDLE Handle)
  55. {
  56. return fwrite(Buffer, Size, Number, (FILE*)Handle);
  57. }
  58. ILint ILAPIENTRY iReadSeek(ILHANDLE Handle, ILint Offset, ILint Mode)
  59. {
  60. return fseek((FILE*)Handle, Offset, Mode);
  61. }
  62. ILint ILAPIENTRY iWriteSeek(ILHANDLE Handle, ILint Offset, ILint Mode)
  63. {
  64. return fseek((FILE*)Handle, Offset, Mode);
  65. }
  66. ILint ILAPIENTRY iReadTell(ILHANDLE Handle)
  67. {
  68. return ftell((FILE*)Handle);
  69. }
  70. ILint ILAPIENTRY iWriteTell(ILHANDLE Handle)
  71. {
  72. return ftell((FILE*)Handle);
  73. }
  74. int main(int argc, char **argv)
  75. {
  76. ILuint ImgId;
  77. ILenum Error;
  78. setlocale(LC_ALL, "");
  79. // We use the filename specified in the first argument of the command-line.
  80. if (argc < 2) {
  81. printf("Please specify a file to open.n");
  82. return 1;
  83. }
  84. // Check if the shared lib's version matches the executable's version.
  85. if (ilGetInteger(IL_VERSION_NUM) < IL_VERSION) {
  86. printf("DevIL version is different...exiting!n");
  87. return 2;
  88. }
  89. // Initialize DevIL.
  90. ilInit();
  91. // Generate the main image name to use.
  92. ilGenImages(1, &ImgId);
  93. // Bind this image name.
  94. ilBindImage(ImgId);
  95. // Override the reading functions.
  96. ilSetRead(iOpenRead, iCloseRead, iEof, iGetc, iRead, iReadSeek, iReadTell);
  97. ilSetWrite(iOpenWrite, iCloseWrite, iPutc, iWriteSeek, iWriteTell, iWrite);
  98. // Loads the image specified by File into the image named by ImgId.
  99. if (!ilLoadImage(argv[1])) {
  100. printf("Could not open file...exiting.n");
  101. return 3;
  102. }
  103. // Display the image's dimensions to the end user.
  104. printf("Width: %d  Height: %d  Depth: %d  Bpp: %dn", ilGetInteger(IL_IMAGE_WIDTH),
  105. ilGetInteger(IL_IMAGE_HEIGHT), ilGetInteger(IL_IMAGE_DEPTH), ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL));
  106. // Enable this to let us overwrite the destination file if it already exists.
  107. ilEnable(IL_FILE_OVERWRITE);
  108. // If argv[2] is present, we save to this filename, else we save to test.tga.
  109. if (argc > 2)
  110. ilSaveImage(argv[2]);
  111. else
  112. ilSaveImage("test.tga");
  113. // Reset the reading / writing functions when we're done loading specially.
  114. //  This isn't required here, since we're exiting, but here's how it's done:
  115. ilResetRead();
  116. ilResetWrite();
  117. // We're done with the image, so let's delete it.
  118. ilDeleteImages(1, &ImgId);
  119. // Simple Error detection loop that displays the Error to the user in a human-readable form.
  120. while ((Error = ilGetError())) {
  121. printf("Error: %sn", ilErrorString(Error));}
  122. return 0;
  123. }