📄 jpegimagedecoder.cpp
字号:
m_state = (m_info.buffered_image) ? JPEG_DECOMPRESS_PROGRESSIVE : JPEG_DECOMPRESS_SEQUENTIAL; } case JPEG_DECOMPRESS_SEQUENTIAL: { if (m_state == JPEG_DECOMPRESS_SEQUENTIAL) { if (!m_decoder->outputScanlines()) return true; /* I/O suspension */ /* If we've completed image output ... */ assert(m_info.output_scanline == m_info.output_height); m_state = JPEG_DONE; } } case JPEG_DECOMPRESS_PROGRESSIVE: { if (m_state == JPEG_DECOMPRESS_PROGRESSIVE) { int status; do { status = jpeg_consume_input(&m_info); } while ((status != JPEG_SUSPENDED) && (status != JPEG_REACHED_EOI)); for (;;) { if (m_info.output_scanline == 0) { int scan = m_info.input_scan_number; /* if we haven't displayed anything yet (output_scan_number==0) and we have enough data for a complete scan, force output of the last full scan */ if ((m_info.output_scan_number == 0) && (scan > 1) && (status != JPEG_REACHED_EOI)) scan--; if (!jpeg_start_output(&m_info, scan)) return true; /* I/O suspension */ } if (m_info.output_scanline == 0xffffff) m_info.output_scanline = 0; if (!m_decoder->outputScanlines()) { if (m_info.output_scanline == 0) /* didn't manage to read any lines - flag so we don't call jpeg_start_output() multiple times for the same scan */ m_info.output_scanline = 0xffffff; return true; /* I/O suspension */ } if (m_info.output_scanline == m_info.output_height) { if (!jpeg_finish_output(&m_info)) return true; /* I/O suspension */ if (jpeg_input_complete(&m_info) && (m_info.input_scan_number == m_info.output_scan_number)) break; m_info.output_scanline = 0; } } m_state = JPEG_DONE; } } case JPEG_DONE: { /* Finish decompression */ if (!jpeg_finish_decompress(&m_info)) return true; /* I/O suspension */ m_state = JPEG_SINK_NON_JPEG_TRAILER; /* we're done */ break; } case JPEG_SINK_NON_JPEG_TRAILER: break; case JPEG_ERROR: break; } return true; } jpeg_decompress_struct* info() { return &m_info; } JSAMPARRAY samples() const { return m_samples; } JPEGImageDecoder* decoder() { return m_decoder; }private: JPEGImageDecoder* m_decoder; unsigned m_bufferLength; int m_bytesToSkip; bool m_decodingSizeOnly; bool m_initialized; jpeg_decompress_struct m_info; decoder_error_mgr m_err; jstate m_state; JSAMPARRAY m_samples;};/* Override the standard error method in the IJG JPEG decoder code. */void error_exit(j_common_ptr cinfo){ /* Return control to the setjmp point. */ decoder_error_mgr *err = (decoder_error_mgr *) cinfo->err; longjmp(err->setjmp_buffer, -1);}void init_source(j_decompress_ptr jd){}void skip_input_data(j_decompress_ptr jd, long num_bytes){ decoder_source_mgr *src = (decoder_source_mgr *)jd->src; src->decoder->skipBytes(num_bytes);}boolean fill_input_buffer(j_decompress_ptr jd){ // Our decode step always sets things up properly, so if this method is ever // called, then we have hit the end of the buffer. A return value of FALSE indicates // that we have no data to supply yet. return false;}void term_source (j_decompress_ptr jd){ decoder_source_mgr *src = (decoder_source_mgr *)jd->src; src->decoder->decoder()->jpegComplete();}JPEGImageDecoder::JPEGImageDecoder(): m_reader(0){}JPEGImageDecoder::~JPEGImageDecoder(){ delete m_reader;}// Take the data and store it.void JPEGImageDecoder::setData(SharedBuffer* data, bool allDataReceived){ if (m_failed) return; // Cache our new data. ImageDecoder::setData(data, allDataReceived); // Create the JPEG reader. if (!m_reader && !m_failed) m_reader = new JPEGImageReader(this);}// Whether or not the size information has been decoded yet.bool JPEGImageDecoder::isSizeAvailable() const{ // If we have pending data to decode, send it to the JPEG reader now. if (!m_sizeAvailable && m_reader) { if (m_failed) return false; // The decoder will go ahead and aggressively consume everything up until the // size is encountered. decode(true); } return m_sizeAvailable;}RGBA32Buffer* JPEGImageDecoder::frameBufferAtIndex(size_t index){ if (index) return 0; if (m_frameBufferCache.isEmpty()) m_frameBufferCache.resize(1); RGBA32Buffer& frame = m_frameBufferCache[0]; if (frame.status() != RGBA32Buffer::FrameComplete && m_reader) // Decode this frame. decode(); return &frame;}// Feed data to the JPEG reader.void JPEGImageDecoder::decode(bool sizeOnly) const{ if (m_failed) return; m_failed = !m_reader->decode(m_data->buffer(), sizeOnly); if (m_failed || (!m_frameBufferCache.isEmpty() && m_frameBufferCache[0].status() == RGBA32Buffer::FrameComplete)) { delete m_reader; m_reader = 0; }}bool JPEGImageDecoder::outputScanlines(){ if (m_frameBufferCache.isEmpty()) return false; // Resize to the width and height of the image. RGBA32Buffer& buffer = m_frameBufferCache[0]; if (buffer.status() == RGBA32Buffer::FrameEmpty) { // Let's resize our buffer now to the correct width/height. RGBA32Array& bytes = buffer.bytes(); bytes.resize(m_size.width() * m_size.height()); // Update our status to be partially complete. buffer.setStatus(RGBA32Buffer::FramePartial); // For JPEGs, the frame always fills the entire image. buffer.setRect(IntRect(0, 0, m_size.width(), m_size.height())); // We don't have alpha (this is the default when the buffer is constructed). } jpeg_decompress_struct* info = m_reader->info(); JSAMPARRAY samples = m_reader->samples(); unsigned* dst = buffer.bytes().data() + info->output_scanline * m_size.width(); while (info->output_scanline < info->output_height) { /* Request one scanline. Returns 0 or 1 scanlines. */ if (jpeg_read_scanlines(info, samples, 1) != 1) return false; JSAMPLE *j1 = samples[0]; for (unsigned i = 0; i < info->output_width; ++i) { unsigned r = *j1++; unsigned g = *j1++; unsigned b = *j1++; RGBA32Buffer::setRGBA(*dst++, r, g, b, 0xFF); } buffer.ensureHeight(info->output_scanline); } return true;}void JPEGImageDecoder::jpegComplete(){ if (m_frameBufferCache.isEmpty()) return; // Hand back an appropriately sized buffer, even if the image ended up being empty. RGBA32Buffer& buffer = m_frameBufferCache[0]; buffer.setStatus(RGBA32Buffer::FrameComplete);}}#endif // PLATFORM(CAIRO)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -