glue.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. Copyright (c) 2021 Bruce A Henderson
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely, subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not
  10. claim that you wrote the original software. If you use this software
  11. in a product, an acknowledgment in the product documentation would be
  12. appreciated but is not required.
  13. 2. Altered source versions must be plainly marked as such, and must not be
  14. misrepresented as being the original software.
  15. 3. This notice may not be removed or altered from any source
  16. distribution.
  17. */
  18. #include "libraw/libraw.h"
  19. #include "brl.mod/blitz.mod/blitz.h"
  20. extern "C" {
  21. int image_raw__stream_seek(BBObject *stream, BBInt64 offset, int origin);
  22. int image_raw__stream_read(BBObject *stream, void * buf, size_t count);
  23. BBInt64 image_raw__stream_tell(BBObject *stream);
  24. BBInt64 image_raw__stream_size(BBObject *stream);
  25. int image_raw__stream_eof(BBObject * stream);
  26. BBObject * image_raw_TRawImage__NewPixmap(BBINT width,BBINT height);
  27. void * image_raw_TRawImage__PixmapPtr(BBObject * pix);
  28. void image_raw_TRawImage__PixmapVar(BBObject* bbt_pix, void** pixels,BBINT* pitch);
  29. BBObject * bmx_image_raw_load(libraw_data_t *lr, BBObject * stream);
  30. }
  31. class LibRaw_Max_datastream : public LibRaw_abstract_datastream
  32. {
  33. private:
  34. BBObject * stream;
  35. public:
  36. LibRaw_Max_datastream(BBObject * _stream) : stream(_stream) {}
  37. ~LibRaw_Max_datastream() {
  38. }
  39. int valid()
  40. {
  41. return stream != 0 && stream != &bbNullObject;
  42. }
  43. int read(void *buffer, size_t size, size_t count)
  44. {
  45. return image_raw__stream_read(stream, buffer, size * count);
  46. }
  47. int seek(INT64 offset, int origin)
  48. {
  49. return image_raw__stream_seek(stream, offset, origin);
  50. }
  51. INT64 tell()
  52. {
  53. return image_raw__stream_tell(stream);
  54. }
  55. INT64 size()
  56. {
  57. return image_raw__stream_size(stream);
  58. }
  59. int get_char()
  60. {
  61. unsigned char c;
  62. int r = read(&c, 1, 1);
  63. return r > 0 ? c : r;
  64. }
  65. char *gets(char *buffer, int length)
  66. {
  67. memset(buffer, 0, length);
  68. for (int i = 0; i < length; i++) {
  69. if (!read(&buffer[i], 1, 1)) {
  70. return NULL;
  71. }
  72. if (buffer[i] == 10) {
  73. break;
  74. }
  75. }
  76. return buffer;
  77. }
  78. int scanf_one(const char *fmt, void* val)
  79. {
  80. std::string buf;
  81. char c = 0;
  82. do {
  83. if (read(&c, 1, 1) == 1) {
  84. if ( c == '0' || c == '\n' || c == ' ' || c == '\t') {
  85. break;
  86. }
  87. buf.append(&c, 1);
  88. } else {
  89. return 0;
  90. }
  91. } while(true);
  92. return sscanf(buf.c_str(), fmt, val);
  93. }
  94. int eof()
  95. {
  96. return image_raw__stream_eof(stream);
  97. }
  98. };
  99. // --------------------------------------------------------
  100. BBObject * bmx_image_raw_load(libraw_data_t *lr, BBObject * stream) {
  101. BBObject * pixmap = &bbNullObject;
  102. LibRaw *ip = (LibRaw *)lr->parent_class;
  103. LibRaw_Max_datastream datastream(stream);
  104. ip->imgdata.rawparams.shot_select = 0;
  105. ip->imgdata.params.use_camera_wb = 1;
  106. ip->imgdata.params.use_camera_matrix = 1;
  107. ip->imgdata.params.half_size = 0;
  108. if (ip->open_datastream(&datastream) != LIBRAW_SUCCESS) {
  109. return pixmap;
  110. }
  111. ip->imgdata.params.output_bps = 8;
  112. ip->imgdata.params.gamm[0] = 1/2.222;
  113. ip->imgdata.params.gamm[1] = 4.5;
  114. ip->imgdata.params.no_auto_bright = 1;
  115. ip->imgdata.params.use_auto_wb = 1;
  116. ip->imgdata.params.user_qual = 3;
  117. if (ip->unpack() != LIBRAW_SUCCESS) {
  118. return pixmap;
  119. }
  120. if (ip->dcraw_process() != LIBRAW_SUCCESS) {
  121. return pixmap;
  122. }
  123. int width, height, cols, bpp;
  124. ip->get_mem_image_format(&width, &height, &cols, &bpp);
  125. pixmap = image_raw_TRawImage__NewPixmap(width, height);
  126. void * ptr;
  127. int pitch;
  128. image_raw_TRawImage__PixmapVar(pixmap, &ptr, &pitch);
  129. if (ip->copy_mem_image(ptr, pitch, 0) != LIBRAW_SUCCESS) {
  130. return &bbNullObject;
  131. }
  132. return pixmap;
  133. }