jload.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include "../Shared/Shared.h"
  2. #include "..\Common\Common.h"
  3. /*
  4. * Include file for users of JPEG library.
  5. * You will need to have included system headers that define at least
  6. * the typedefs FILE and size_t before you can include jpeglib.h.
  7. * (stdio.h is sufficient on ANSI-conforming systems.)
  8. * You may also wish to include "jerror.h".
  9. */
  10. #include "jpeglib.h"
  11. int LoadJPG( const char * filename, unsigned char ** pic, int * width, int * height ) {
  12. /* This struct contains the JPEG decompression parameters and pointers to
  13. * working space (which is allocated as needed by the JPEG library).
  14. */
  15. struct jpeg_decompress_struct cinfo;
  16. /* We use our private extension JPEG error handler.
  17. * Note that this struct must live as long as the main JPEG parameter
  18. * struct, to avoid dangling-pointer problems.
  19. */
  20. /* This struct represents a JPEG error handler. It is declared separately
  21. * because applications often want to supply a specialized error handler
  22. * (see the second half of this file for an example). But here we just
  23. * take the easy way out and use the standard error handler, which will
  24. * print a message on stderr and call exit() if compression fails.
  25. * Note that this struct must live as long as the main JPEG parameter
  26. * struct, to avoid dangling-pointer problems.
  27. */
  28. struct jpeg_error_mgr jerr;
  29. /* More stuff */
  30. fileHandle_t infile; /* source file */
  31. JSAMPARRAY buffer; /* Output row buffer */
  32. int row_stride; /* physical row width in output buffer */
  33. unsigned char * out;
  34. /* In this example we want to open the input file before doing anything else,
  35. * so that the setjmp() error recovery below can assume the file is open.
  36. * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
  37. * requires it in order to read binary files.
  38. */
  39. FS_FOpenFileRead( filename, &infile, qfalse );
  40. if ( infile == 0 ) {
  41. return 0;
  42. }
  43. /* Step 1: allocate and initialize JPEG decompression object */
  44. /* We have to set up the error handler first, in case the initialization
  45. * step fails. (Unlikely, but it could happen if you are out of memory.)
  46. * This routine fills in the contents of struct jerr, and returns jerr's
  47. * address which we place into the link field in cinfo.
  48. */
  49. cinfo.err = jpeg_std_error( &jerr );
  50. /* Now we can initialize the JPEG decompression object. */
  51. jpeg_create_decompress( &cinfo );
  52. /* Step 2: specify data source (eg, a file) */
  53. jpeg_stdio_src( &cinfo, infile );
  54. /* Step 3: read file parameters with jpeg_read_header() */
  55. (void) jpeg_read_header( &cinfo, TRUE );
  56. /* We can ignore the return value from jpeg_read_header since
  57. * (a) suspension is not possible with the stdio data source, and
  58. * (b) we passed TRUE to reject a tables-only JPEG file as an error.
  59. * See libjpeg.doc for more info.
  60. */
  61. /* Step 4: set parameters for decompression */
  62. /* In this example, we don't need to change any of the defaults set by
  63. * jpeg_read_header(), so we do nothing here.
  64. */
  65. /* Step 5: Start decompressor */
  66. (void) jpeg_start_decompress( &cinfo );
  67. /* We can ignore the return value since suspension is not possible
  68. * with the stdio data source.
  69. */
  70. /* We may need to do some setup of our own at this point before reading
  71. * the data. After jpeg_start_decompress() we have the correct scaled
  72. * output image dimensions available, as well as the output colormap
  73. * if we asked for color quantization.
  74. * In this example, we need to make an output work buffer of the right size.
  75. */
  76. /* JSAMPLEs per row in output buffer */
  77. row_stride = cinfo.output_width * cinfo.output_components;
  78. out = Z_Malloc( cinfo.output_width * cinfo.output_height * cinfo.output_components );
  79. *pic = out;
  80. *width = cinfo.output_width;
  81. *height = cinfo.output_height;
  82. /* Step 6: while (scan lines remain to be read) */
  83. /* jpeg_read_scanlines(...); */
  84. /* Here we use the library's state variable cinfo.output_scanline as the
  85. * loop counter, so that we don't have to keep track ourselves.
  86. */
  87. while ( cinfo.output_scanline < cinfo.output_height ) {
  88. /* jpeg_read_scanlines expects an array of pointers to scanlines.
  89. * Here the array is only one element long, but you could ask for
  90. * more than one scanline at a time if that's more convenient.
  91. */
  92. buffer = (JSAMPARRAY)out + ( row_stride * cinfo.output_scanline );
  93. (void) jpeg_read_scanlines( &cinfo, buffer, 1 );
  94. }
  95. /* Step 7: Finish decompression */
  96. (void) jpeg_finish_decompress( &cinfo );
  97. /* We can ignore the return value since suspension is not possible
  98. * with the stdio data source.
  99. */
  100. /* Step 8: Release JPEG decompression object */
  101. /* This is an important step since it will release a good deal of memory. */
  102. jpeg_destroy_decompress( &cinfo );
  103. /* After finish_decompress, we can close the input file.
  104. * Here we postpone it until after no more JPEG errors are possible,
  105. * so as to simplify the setjmp error logic above. (Actually, I don't
  106. * think that jpeg_destroy can do an error exit, but why assume anything...)
  107. */
  108. FS_FCloseFile( infile );
  109. /* At this point you may want to check to see whether any corrupt-data
  110. * warnings occurred (test whether jerr.pub.num_warnings is nonzero).
  111. */
  112. /* And we're done! */
  113. return 1;
  114. }