readtest.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //-----------------------------------------------------------------------------
  2. //
  3. // ImageLib Source Example
  4. // Copyright (C) 2000-2001 by Denton Woods
  5. // Last modified: 09/07/2001 <--Y2K Compliant! =]
  6. //
  7. // Filename: examples/register read/register read.c
  8. //
  9. // Description: An example of creating your own loading routine
  10. // to use with DevIL -- uses a hypothetical format.
  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. ILboolean ILAPIENTRY LoadFunction(const char *FileName)
  28. {
  29. ILuint Width, Height;
  30. FILE *f = fopen(FileName, "rb");
  31. if (f == NULL)
  32. return IL_FALSE;
  33. fread(&Width, 1, 4, f);
  34. fread(&Height, 1, 4, f);
  35. if (!ilTexImage(Width, Height, 1, 3, IL_RGB, IL_UNSIGNED_BYTE, NULL)) {
  36. fclose(f);
  37. return IL_FALSE;
  38. }
  39. // Set the origin via the register functions.
  40. ilRegisterOrigin(IL_ORIGIN_UPPER_LEFT);
  41. fread(ilGetData(), 1, Width * Height * 3, f);
  42. fclose(f);
  43. return IL_TRUE;
  44. }
  45. int main(int argc, char **argv)
  46. {
  47. ILuint ImgId;
  48. ILenum Error;
  49. // We use the filename specified in the first argument of the command-line.
  50. if (argc < 2) {
  51. printf("Please specify a file to open.\n");
  52. return 1;
  53. }
  54. // Check if the shared lib's version matches the executable's version.
  55. if (ilGetInteger(IL_VERSION_NUM) < IL_VERSION) {
  56. printf("DevIL version is different...exiting!\n");
  57. return 2;
  58. }
  59. // Initialize DevIL.
  60. ilInit();
  61. // Set the loading function here.
  62. ilRegisterLoad("xxx", LoadFunction);
  63. // Generate the main image name to use.
  64. ilGenImages(1, &ImgId);
  65. // Bind this image name.
  66. ilBindImage(ImgId);
  67. // Loads the image specified by File into the image named by ImgId.
  68. if (!ilLoadImage(argv[1])) {
  69. printf("Could not open file...exiting.\n");
  70. return 3;
  71. }
  72. // Display the image's dimensions to the end user.
  73. printf("Width: %d Height: %d Depth: %d Bpp: %d\n", ilGetInteger(IL_IMAGE_WIDTH),
  74. ilGetInteger(IL_IMAGE_HEIGHT), ilGetInteger(IL_IMAGE_DEPTH), ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL));
  75. // Enable this to let us overwrite the destination file if it already exists.
  76. ilEnable(IL_FILE_OVERWRITE);
  77. // If argv[2] is present, we save to this filename, else we save to test.tga.
  78. if (argc > 2)
  79. ilSaveImage(argv[2]);
  80. else
  81. ilSaveImage("test.tga");
  82. // Remove the loading function when we're done using it or want to change it.
  83. // This isn't required here, since we're exiting, but here's how it's done:
  84. ilRemoveLoad("xxx");
  85. // We're done with the image, so let's delete it.
  86. ilDeleteImages(1, &ImgId);
  87. // Simple Error detection loop that displays the Error to the user in a human-readable form.
  88. while ((Error = ilGetError())) {
  89. PRINT_ERROR_MACRO;
  90. }
  91. return 0;
  92. }