amg.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "amg.h"
  2. #include "api__jpeg.h"
  3. #include <setjmp.h>
  4. extern "C"
  5. {
  6. #undef FAR
  7. #include "jpeglib.h"
  8. // our reader...
  9. static void init_source( j_decompress_ptr cinfo )
  10. {}
  11. static const JOCTET jpeg_eof[] = { (JOCTET)0xFF, (JOCTET)JPEG_EOI };
  12. static boolean fill_input_buffer( j_decompress_ptr cinfo )
  13. {
  14. cinfo->src->next_input_byte = jpeg_eof;
  15. cinfo->src->bytes_in_buffer = 2;
  16. return TRUE;
  17. }
  18. static void skip_input_data( j_decompress_ptr cinfo, long num_bytes )
  19. {
  20. //my_src_ptr src = (my_src_ptr) cinfo->src;
  21. if ( num_bytes > 0 )
  22. {
  23. if ( num_bytes > (long)cinfo->src->bytes_in_buffer )
  24. {
  25. fill_input_buffer( cinfo );
  26. }
  27. else
  28. {
  29. cinfo->src->next_input_byte += (size_t)num_bytes;
  30. cinfo->src->bytes_in_buffer -= (size_t)num_bytes;
  31. }
  32. }
  33. }
  34. static void term_source( j_decompress_ptr cinfo )
  35. {}
  36. typedef struct
  37. {
  38. jpeg_destination_mgr pub;
  39. uint8_t *buf;
  40. int len;
  41. } my_destination_mgr;
  42. void init_destination( j_compress_ptr cinfo );
  43. boolean empty_output_buffer( j_compress_ptr cinfo );
  44. void term_destination( j_compress_ptr cinfo );
  45. static void wasabi_jpgload_error_exit( j_common_ptr cinfo )
  46. {
  47. jmp_buf *stack_env = (jmp_buf *)cinfo->client_data;
  48. longjmp( *stack_env, 1 );
  49. }
  50. };
  51. int AMGSucks::WriteAlbumArt(const void *data, size_t data_len, void **out, int *out_len)
  52. {
  53. *out = 0;
  54. jmp_buf stack_env;
  55. jpeg_decompress_struct cinfo_dec={0};
  56. jpeg_compress_struct cinfo={0};
  57. cinfo.client_data=&stack_env;
  58. cinfo_dec.client_data=&stack_env;
  59. if (setjmp(stack_env))
  60. {
  61. // longjmp will goto here
  62. jpeg_destroy_decompress(&cinfo_dec);
  63. jpeg_destroy_compress(&cinfo);
  64. return 1;
  65. }
  66. jpeg_error_mgr jerr;
  67. jpeg_source_mgr src = {(const JOCTET *)data,data_len,init_source,fill_input_buffer,skip_input_data,jpeg_resync_to_restart,term_source};
  68. cinfo_dec.err = jpeg_std_error(&jerr);
  69. jpeg_create_decompress(&cinfo_dec);
  70. cinfo_dec.err->error_exit = wasabi_jpgload_error_exit;
  71. cinfo_dec.src = &src;
  72. if (jpeg_read_header(&cinfo_dec, TRUE) == JPEG_HEADER_OK)
  73. {
  74. cinfo.err = cinfo_dec.err;
  75. jpeg_create_compress(&cinfo);
  76. cinfo.err->error_exit = wasabi_jpgload_error_exit;
  77. my_destination_mgr dest = {{0,65536,init_destination,empty_output_buffer,term_destination},0,65536};
  78. dest.buf = (uint8_t *)WASABI_API_MEMMGR->sysMalloc(65536);
  79. dest.pub.next_output_byte = dest.buf;
  80. cinfo.dest = (jpeg_destination_mgr*)&dest;
  81. cinfo.image_width = cinfo_dec.image_width;
  82. cinfo.image_height = cinfo_dec.image_height;
  83. cinfo.input_components = 4;
  84. cinfo.in_color_space = JCS_RGB;
  85. jpeg_copy_critical_parameters(&cinfo_dec, &cinfo);
  86. jvirt_barray_ptr *stuff = jpeg_read_coefficients(&cinfo_dec);
  87. jpeg_write_coefficients(&cinfo, stuff);
  88. const char *blah = "AMG/AOL";
  89. jpeg_write_marker(&cinfo, JPEG_COM, (JOCTET *)blah, (unsigned int)strlen(blah));
  90. jpeg_finish_compress(&cinfo);
  91. *out = dest.buf;
  92. *out_len = dest.len - (int)dest.pub.free_in_buffer;
  93. jpeg_destroy_decompress(&cinfo_dec);
  94. jpeg_destroy_compress(&cinfo);
  95. return 0;
  96. }
  97. return 1;
  98. }
  99. #define CBCLASS AMGSucks
  100. START_DISPATCH;
  101. CB(WRITEALBUMART, WriteAlbumArt);
  102. END_DISPATCH;
  103. #undef CBCLASS