jpgload.cpp 4.8 KB

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