| 105 | // ---------------------------------------------------------------------------------------- |
| 106 | |
| 107 | void jxl_loader::decode(unsigned char* out, const size_t out_size) const |
| 108 | { |
| 109 | auto runner = JxlResizableParallelRunnerMake(nullptr); |
| 110 | auto dec = JxlDecoderMake(nullptr); |
| 111 | if (JXL_DEC_SUCCESS != JxlDecoderSubscribeEvents(dec.get(), JXL_DEC_FULL_IMAGE)) |
| 112 | { |
| 113 | throw image_load_error("jxl_loader: JxlDecoderSubscribeEvents failed"); |
| 114 | } |
| 115 | |
| 116 | if (JXL_DEC_SUCCESS != JxlDecoderSetParallelRunner(dec.get(), JxlResizableParallelRunner, runner.get())) |
| 117 | { |
| 118 | throw image_load_error("jxl_loader: JxlDecoderSetParallelRunner failed"); |
| 119 | } |
| 120 | |
| 121 | if (JXL_DEC_SUCCESS != JxlDecoderSetInput(dec.get(), data.data(), data.size())) |
| 122 | { |
| 123 | throw image_load_error("jxl_loader: JxlDecoderSetInput failed"); |
| 124 | } |
| 125 | JxlDecoderCloseInput(dec.get()); |
| 126 | |
| 127 | JxlPixelFormat format = { |
| 128 | .num_channels = depth, |
| 129 | .data_type = JXL_TYPE_UINT8, |
| 130 | .endianness = JXL_NATIVE_ENDIAN, |
| 131 | .align=0 |
| 132 | }; |
| 133 | for (;;) |
| 134 | { |
| 135 | JxlDecoderStatus status = JxlDecoderProcessInput(dec.get()); |
| 136 | if (status == JXL_DEC_ERROR) |
| 137 | { |
| 138 | throw image_load_error("jxl_loader: JxlDecoderProcessInput failed"); |
| 139 | } |
| 140 | else if (status == JXL_DEC_NEED_MORE_INPUT) |
| 141 | { |
| 142 | throw image_load_error("jxl_loader: Error, expected more input"); |
| 143 | } |
| 144 | else if (status == JXL_DEC_NEED_IMAGE_OUT_BUFFER) |
| 145 | { |
| 146 | JxlResizableParallelRunnerSetThreads(runner.get(), JxlResizableParallelRunnerSuggestThreads(width, height)); |
| 147 | size_t buffer_size; |
| 148 | if (JXL_DEC_SUCCESS != JxlDecoderImageOutBufferSize(dec.get(), &format, &buffer_size)) |
| 149 | { |
| 150 | throw image_load_error("jxl_loader: JxlDecoderImageOutBufferSize failed"); |
| 151 | } |
| 152 | if (buffer_size != width * height * depth) |
| 153 | { |
| 154 | throw image_load_error("jxl_loader: invalid output buffer size"); |
| 155 | } |
| 156 | if (JXL_DEC_SUCCESS != JxlDecoderSetImageOutBuffer(dec.get(), &format, out, out_size)) |
| 157 | { |
| 158 | throw image_load_error("jxl_loader: JxlDecoderSetImageOutBuffer failed"); |
| 159 | } |
| 160 | } |
| 161 | else if (status == JXL_DEC_FULL_IMAGE) |
| 162 | { |
| 163 | // If the image is an animation, more full frames may be decoded. |
| 164 | // This loader only decodes the first one. |
no test coverage detected