jpeg_helper.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (c) 2012 Andrey Kemka
  3. *
  4. * This software is provided 'as-is', without any express or
  5. * implied warranty. In no event will the authors be held
  6. * liable for any damages arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute
  10. * it freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented;
  13. * you must not claim that you wrote the original software.
  14. * If you use this software in a product, an acknowledgment
  15. * in the product documentation would be appreciated but
  16. * is not required.
  17. *
  18. * 2. Altered source versions must be plainly marked as such,
  19. * and must not be misrepresented as being the original software.
  20. *
  21. * 3. This notice may not be removed or altered from any
  22. * source distribution.
  23. */
  24. #include "stdio.h"
  25. #include <jconfig.h>
  26. #include <jmorecfg.h>
  27. #include <jpeglib.h>
  28. typedef unsigned char* (*getmem_func)( int Size );
  29. typedef struct{
  30. void* Memory;
  31. unsigned int MemSize;
  32. unsigned short Width;
  33. unsigned short Height;
  34. getmem_func GetMem;
  35. } zglTJPGData;
  36. void jpgturbo_Load( zglTJPGData *jpgData, unsigned char **Data )
  37. {
  38. struct jpeg_decompress_struct cinfo;
  39. struct jpeg_error_mgr jerr;
  40. int scanline_width;
  41. JSAMPARRAY buffer;
  42. int i, j, iscan, grayscale;
  43. cinfo.err = jpeg_std_error( &jerr );
  44. jpeg_create_decompress( &cinfo );
  45. jpeg_mem_src( &cinfo, jpgData->Memory, jpgData->MemSize );
  46. jpeg_read_header( &cinfo, TRUE );
  47. jpeg_calc_output_dimensions( &cinfo );
  48. scanline_width = cinfo.output_width * cinfo.output_components;
  49. buffer = cinfo.mem->alloc_sarray( (j_common_ptr)&cinfo, JPOOL_IMAGE, scanline_width, 1 );
  50. jpeg_start_decompress( &cinfo );
  51. if ( cinfo.out_color_space == JCS_GRAYSCALE )
  52. grayscale = TRUE;
  53. else if ( cinfo.out_color_space == JCS_RGB )
  54. {
  55. if ( cinfo.quantize_colors )
  56. grayscale = TRUE;
  57. else
  58. grayscale = FALSE;
  59. } else return;
  60. jpgData->Width = cinfo.output_width;
  61. jpgData->Height = cinfo.output_height;
  62. (*Data) = jpgData->GetMem( cinfo.output_width * cinfo.output_height * 4 );
  63. if ( !grayscale )
  64. {
  65. while ( cinfo.output_scanline < cinfo.output_height )
  66. {
  67. iscan = ( cinfo.output_height - cinfo.output_scanline - 1 ) * cinfo.output_width * 4;
  68. jpeg_read_scanlines( &cinfo, buffer, 1 );
  69. for ( i = 0, j = 0; i < cinfo.output_width * 3; i += 3, j += 4 )
  70. {
  71. (*Data)[ j + 0 + iscan ] = (*buffer)[ i + 0 ];
  72. (*Data)[ j + 1 + iscan ] = (*buffer)[ i + 1 ];
  73. (*Data)[ j + 2 + iscan ] = (*buffer)[ i + 2 ];
  74. (*Data)[ j + 3 + iscan ] = 255;
  75. }
  76. }
  77. }
  78. else
  79. {
  80. while ( cinfo.output_scanline < cinfo.output_height )
  81. {
  82. iscan = ( cinfo.output_height - cinfo.output_scanline - 1 ) * cinfo.output_width * 4;
  83. jpeg_read_scanlines( &cinfo, buffer, 1 );
  84. for ( i = 0, j = 0; i < cinfo.output_width; i++, j += 4 )
  85. {
  86. (*Data)[ j + 0 + iscan ] = (*buffer)[ i ];
  87. (*Data)[ j + 1 + iscan ] = (*buffer)[ i ];
  88. (*Data)[ j + 2 + iscan ] = (*buffer)[ i ];
  89. (*Data)[ j + 3 + iscan ] = 255;
  90. }
  91. }
  92. }
  93. jpeg_finish_decompress( &cinfo );
  94. jpeg_destroy_decompress( &cinfo );
  95. }