glue.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. Copyright (c) 2023 Bruce A Henderson
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions
  5. are met:
  6. 1. Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. 2. Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  12. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  13. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  14. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  15. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  16. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  17. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  18. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  19. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  20. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  21. POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. #include "brl.mod/blitz.mod/blitz.h"
  24. #include "openjpeg.h"
  25. size_t image_jpeg2000_TJpeg2000Image__stream_read(void * p_buffer, size_t p_nb_bytes, void * p_user_data);
  26. int image_jpeg2000_TJpeg2000Image__stream_seek(BBLONG p_nb_bytes, void * p_user_data);
  27. BBLONG image_jpeg2000_TJpeg2000Image__stream_skip(BBLONG p_nb_bytes, void * p_user_data);
  28. size_t image_jpeg2000_TJpeg2000Image__stream_size(void * p_user_data);
  29. BBObject * image_jpeg2000_TJpeg2000Image__NewPixmap(int width, int height);
  30. void * image_jpeg2000_TJpeg2000Image__PixmapPixels(BBObject * pixmap);
  31. BBObject * bmx_jpeg2000_load( BBObject * maxStream ) {
  32. opj_dparameters_t parameters;
  33. opj_image_t* image = NULL;
  34. opj_set_default_decoder_parameters(&parameters);
  35. opj_stream_t* stream = opj_stream_create(8192, OPJ_TRUE);
  36. if (!stream) {
  37. return &bbNullObject;
  38. }
  39. opj_stream_set_user_data(stream, maxStream, NULL);
  40. opj_stream_set_user_data_length(stream, image_jpeg2000_TJpeg2000Image__stream_size(maxStream));
  41. opj_stream_set_read_function(stream, image_jpeg2000_TJpeg2000Image__stream_read);
  42. opj_stream_set_seek_function(stream, image_jpeg2000_TJpeg2000Image__stream_seek);
  43. opj_stream_set_skip_function(stream, image_jpeg2000_TJpeg2000Image__stream_skip);
  44. opj_codec_t* codec = opj_create_decompress(OPJ_CODEC_JP2);
  45. if (!opj_setup_decoder(codec, &parameters)) {
  46. opj_stream_destroy(stream);
  47. opj_destroy_codec(codec);
  48. return &bbNullObject;
  49. }
  50. if (!opj_read_header(stream, codec, &image)) {
  51. opj_stream_destroy(stream);
  52. opj_destroy_codec(codec);
  53. return &bbNullObject;
  54. }
  55. if (!opj_decode(codec, stream, image)) {
  56. opj_stream_destroy(stream);
  57. opj_destroy_codec(codec);
  58. return &bbNullObject;
  59. }
  60. int width = image->comps[0].w;
  61. int height = image->comps[0].h;
  62. int num_components = image->numcomps;
  63. // create a TPixmap
  64. BBObject * pixmap = image_jpeg2000_TJpeg2000Image__NewPixmap(width, height);
  65. // get the pixel data
  66. void * pixels = image_jpeg2000_TJpeg2000Image__PixmapPixels(pixmap);
  67. for (int y = 0; y < height; y++) {
  68. for (int x = 0; x < width; x++) {
  69. // pixel index
  70. int index = y * width + x;
  71. // colour components
  72. uint32_t r = image->comps[0].data[index];
  73. uint32_t g = image->comps[1].data[index];
  74. uint32_t b = image->comps[2].data[index];
  75. uint32_t a = image->numcomps > 3 ? image->comps[3].data[index] : 255; // assuming alpha if it exists
  76. // Convert the components
  77. uint32_t rgba = (a << 24) | (b << 16) | (g << 8) | r;
  78. // Set the pixel in the pixmap
  79. ((uint32_t*)pixels)[index] = rgba;
  80. }
  81. }
  82. opj_image_destroy(image);
  83. opj_stream_destroy(stream);
  84. opj_destroy_codec(codec);
  85. return pixmap;
  86. }