simple.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //-----------------------------------------------------------------------------
  2. //
  3. // DevIL Source Example
  4. // Copyright (C) 2000-2002 by Denton Woods
  5. // Last modified: 4/22/2002
  6. //
  7. // Filename: examples/Simple Example/simple.c
  8. //
  9. // Description: Simplest implementation of an DevIL application.
  10. // Loads an image and saves it to a new image.
  11. // The images can be of any format and can be different.
  12. //
  13. //-----------------------------------------------------------------------------
  14. // Required include files.
  15. #ifdef HAVE_CONFIG_H
  16. #include "config.h"
  17. #endif /* HAVE_CONFIG_H */
  18. #include <IL/il.h>
  19. #include <stdio.h>
  20. /* We would need ILU just because of iluErrorString() function... */
  21. /* So make it possible for both with and without ILU! */
  22. #ifdef ILU_ENABLED
  23. #include <IL/ilu.h>
  24. #define PRINT_ERROR_MACRO printf("Error: %s\n", iluErrorString(Error))
  25. #else /* not ILU_ENABLED */
  26. #define PRINT_ERROR_MACRO printf("Error: 0x%X\n", (unsigned int)Error)
  27. #endif /* not ILU_ENABLED */
  28. int main(int argc, char **argv)
  29. {
  30. ILuint ImgId;
  31. ILenum Error;
  32. // We use the filename specified in the first argument of the command-line.
  33. if (argc < 2) {
  34. fprintf(stderr, "DevIL_test : DevIL simple command line application.\n");
  35. fprintf(stderr, "Usage : DevIL_test <file> [output]\n");
  36. fprintf(stderr, "Default output is test.tga\n");
  37. return 1;
  38. }
  39. // Check if the shared lib's version matches the executable's version.
  40. if (ilGetInteger(IL_VERSION_NUM) < IL_VERSION) {
  41. printf("DevIL version is different...exiting!\n");
  42. return 2;
  43. }
  44. // Initialize DevIL.
  45. ilInit();
  46. #ifdef ILU_ENABLED
  47. iluInit();
  48. #endif
  49. // Generate the main image name to use.
  50. ilGenImages(1, &ImgId);
  51. // Bind this image name.
  52. ilBindImage(ImgId);
  53. // Loads the image specified by File into the image named by ImgId.
  54. if (!ilLoadImage(argv[1])) {
  55. printf("Could not open file...exiting.\n");
  56. return 3;
  57. }
  58. // Display the image's dimensions to the end user.
  59. printf("Width: %d Height: %d Depth: %d Bpp: %d\n",
  60. ilGetInteger(IL_IMAGE_WIDTH),
  61. ilGetInteger(IL_IMAGE_HEIGHT),
  62. ilGetInteger(IL_IMAGE_DEPTH),
  63. ilGetInteger(IL_IMAGE_BITS_PER_PIXEL));
  64. // Enable this to let us overwrite the destination file if it already exists.
  65. ilEnable(IL_FILE_OVERWRITE);
  66. // If argv[2] is present, we save to this filename, else we save to test.tga.
  67. if (argc > 2)
  68. ilSaveImage(argv[2]);
  69. else
  70. ilSaveImage("test.tga");
  71. // We're done with the image, so let's delete it.
  72. ilDeleteImages(1, &ImgId);
  73. // Simple Error detection loop that displays the Error to the user in a human-readable form.
  74. while ((Error = ilGetError())) {
  75. PRINT_ERROR_MACRO;}
  76. return 0;
  77. }