CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/ext/jpge/jpge.cpp
Views: 1401
// jpge.cpp - C++ class for JPEG compression. Richard Geldreich <[email protected]>1// Supports grayscale, H1V1, H2V1, and H2V2 chroma subsampling factors, one or two pass Huffman table optimization, libjpeg-style quality 1-100 quality factors.2// Also supports using luma quantization tables for chroma.3//4// Released under two licenses. You are free to choose which license you want:5// License 1:6// Public Domain7//8// License 2:9// Licensed under the Apache License, Version 2.0 (the "License");10// you may not use this file except in compliance with the License.11// You may obtain a copy of the License at12//13// http://www.apache.org/licenses/LICENSE-2.014//15// Unless required by applicable law or agreed to in writing, software16// distributed under the License is distributed on an "AS IS" BASIS,17// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.18// See the License for the specific language governing permissions and19// limitations under the License.20//21// v1.01, Dec. 18, 2010 - Initial release22// v1.02, Apr. 6, 2011 - Removed 2x2 ordered dither in H2V1 chroma subsampling method load_block_16_8_8(). (The rounding factor was 2, when it should have been 1. Either way, it wasn't helping.)23// v1.03, Apr. 16, 2011 - Added support for optimized Huffman code tables, optimized dynamic memory allocation down to only 1 alloc.24// Also from Alex Evans: Added RGBA support, linear memory allocator (no longer needed in v1.03).25// v1.04, May. 19, 2012: Forgot to set m_pFile ptr to NULL in cfile_stream::close(). Thanks to Owen Kaluza for reporting this bug.26// Code tweaks to fix VS2008 static code analysis warnings (all looked harmless).27// Code review revealed method load_block_16_8_8() (used for the non-default H2V1 sampling mode to downsample chroma) somehow didn't get the rounding factor fix from v1.02.28// v1.05, March 25, 2020: Added Apache 2.0 alternate license2930#include "jpge.h"3132#include <stdio.h>33#include <stdlib.h>34#include <string.h>3536#define JPGE_MAX(a,b) (((a)>(b))?(a):(b))37#define JPGE_MIN(a,b) (((a)<(b))?(a):(b))3839namespace jpge {4041static inline void* jpge_malloc(size_t nSize) { return malloc(nSize); }42static inline void jpge_free(void* p) { free(p); }4344// Various JPEG enums and tables.45enum { M_SOF0 = 0xC0, M_DHT = 0xC4, M_SOI = 0xD8, M_EOI = 0xD9, M_SOS = 0xDA, M_DQT = 0xDB, M_APP0 = 0xE0 };46enum { DC_LUM_CODES = 12, AC_LUM_CODES = 256, DC_CHROMA_CODES = 12, AC_CHROMA_CODES = 256, MAX_HUFF_SYMBOLS = 257, MAX_HUFF_CODESIZE = 32 };4748static uint8 s_zag[64] = { 0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,35,42,49,56,57,50,43,36,29,22,15,23,30,37,44,51,58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63 };49static int16 s_std_lum_quant[64] = { 16,11,12,14,12,10,16,14,13,14,18,17,16,19,24,40,26,24,22,22,24,49,35,37,29,40,58,51,61,60,57,51,56,55,64,72,92,78,64,68,87,69,55,56,80,109,81,87,95,98,103,104,103,62,77,113,121,112,100,120,92,101,103,99 };50static int16 s_std_croma_quant[64] = { 17,18,18,24,21,24,47,26,26,47,99,66,56,66,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99 };5152// Table from http://www.imagemagick.org/discourse-server/viewtopic.php?f=22&t=20333&p=98008#p9800853// This is mozjpeg's default table, in zag order.54static int16 s_alt_quant[64] = { 16,16,16,16,17,16,18,20,20,18,25,27,24,27,25,37,34,31,31,34,37,56,40,43,40,43,40,56,85,53,62,53,53,62,53,85,75,91,74,69,74,91,75,135,106,94,94,106,135,156,131,124,131,156,189,169,169,189,238,226,238,311,311,418 };5556static uint8 s_dc_lum_bits[17] = { 0,0,1,5,1,1,1,1,1,1,0,0,0,0,0,0,0 };57static uint8 s_dc_lum_val[DC_LUM_CODES] = { 0,1,2,3,4,5,6,7,8,9,10,11 };58static uint8 s_ac_lum_bits[17] = { 0,0,2,1,3,3,2,4,3,5,5,4,4,0,0,1,0x7d };59static uint8 s_ac_lum_val[AC_LUM_CODES] =60{610x01,0x02,0x03,0x00,0x04,0x11,0x05,0x12,0x21,0x31,0x41,0x06,0x13,0x51,0x61,0x07,0x22,0x71,0x14,0x32,0x81,0x91,0xa1,0x08,0x23,0x42,0xb1,0xc1,0x15,0x52,0xd1,0xf0,620x24,0x33,0x62,0x72,0x82,0x09,0x0a,0x16,0x17,0x18,0x19,0x1a,0x25,0x26,0x27,0x28,0x29,0x2a,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x43,0x44,0x45,0x46,0x47,0x48,0x49,630x4a,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x83,0x84,0x85,0x86,0x87,0x88,0x89,640x8a,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xb2,0xb3,0xb4,0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xc2,0xc3,0xc4,0xc5,650xc6,0xc7,0xc8,0xc9,0xca,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xe1,0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,660xf9,0xfa67};68static uint8 s_dc_chroma_bits[17] = { 0,0,3,1,1,1,1,1,1,1,1,1,0,0,0,0,0 };69static uint8 s_dc_chroma_val[DC_CHROMA_CODES] = { 0,1,2,3,4,5,6,7,8,9,10,11 };70static uint8 s_ac_chroma_bits[17] = { 0,0,2,1,2,4,4,3,4,7,5,4,4,0,1,2,0x77 };71static uint8 s_ac_chroma_val[AC_CHROMA_CODES] =72{730x00,0x01,0x02,0x03,0x11,0x04,0x05,0x21,0x31,0x06,0x12,0x41,0x51,0x07,0x61,0x71,0x13,0x22,0x32,0x81,0x08,0x14,0x42,0x91,0xa1,0xb1,0xc1,0x09,0x23,0x33,0x52,0xf0,740x15,0x62,0x72,0xd1,0x0a,0x16,0x24,0x34,0xe1,0x25,0xf1,0x17,0x18,0x19,0x1a,0x26,0x27,0x28,0x29,0x2a,0x35,0x36,0x37,0x38,0x39,0x3a,0x43,0x44,0x45,0x46,0x47,0x48,750x49,0x4a,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x82,0x83,0x84,0x85,0x86,0x87,760x88,0x89,0x8a,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xb2,0xb3,0xb4,0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xc2,0xc3,770xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,780xf9,0xfa79};8081// Low-level helper functions.82template <class T> inline void clear_obj(T& obj) { memset(&obj, 0, sizeof(obj)); }8384const int YR = 19595, YG = 38470, YB = 7471, CB_R = -11059, CB_G = -21709, CB_B = 32768, CR_R = 32768, CR_G = -27439, CR_B = -5329;85static inline uint8 clamp(int i) { if (static_cast<uint>(i) > 255U) { if (i < 0) i = 0; else if (i > 255) i = 255; } return static_cast<uint8>(i); }8687static inline int left_shifti(int val, uint32 bits)88{89return static_cast<int>(static_cast<uint32>(val) << bits);90}9192static void RGB_to_YCC(uint8* pDst, const uint8* pSrc, int num_pixels)93{94for (; num_pixels; pDst += 3, pSrc += 3, num_pixels--)95{96const int r = pSrc[0], g = pSrc[1], b = pSrc[2];97pDst[0] = static_cast<uint8>((r * YR + g * YG + b * YB + 32768) >> 16);98pDst[1] = clamp(128 + ((r * CB_R + g * CB_G + b * CB_B + 32768) >> 16));99pDst[2] = clamp(128 + ((r * CR_R + g * CR_G + b * CR_B + 32768) >> 16));100}101}102103static void RGB_to_Y(uint8* pDst, const uint8* pSrc, int num_pixels)104{105for (; num_pixels; pDst++, pSrc += 3, num_pixels--)106pDst[0] = static_cast<uint8>((pSrc[0] * YR + pSrc[1] * YG + pSrc[2] * YB + 32768) >> 16);107}108109static void RGBA_to_YCC(uint8* pDst, const uint8* pSrc, int num_pixels)110{111for (; num_pixels; pDst += 3, pSrc += 4, num_pixels--)112{113const int r = pSrc[0], g = pSrc[1], b = pSrc[2];114pDst[0] = static_cast<uint8>((r * YR + g * YG + b * YB + 32768) >> 16);115pDst[1] = clamp(128 + ((r * CB_R + g * CB_G + b * CB_B + 32768) >> 16));116pDst[2] = clamp(128 + ((r * CR_R + g * CR_G + b * CR_B + 32768) >> 16));117}118}119120static void RGBA_to_Y(uint8* pDst, const uint8* pSrc, int num_pixels)121{122for (; num_pixels; pDst++, pSrc += 4, num_pixels--)123pDst[0] = static_cast<uint8>((pSrc[0] * YR + pSrc[1] * YG + pSrc[2] * YB + 32768) >> 16);124}125126static void Y_to_YCC(uint8* pDst, const uint8* pSrc, int num_pixels)127{128for (; num_pixels; pDst += 3, pSrc++, num_pixels--) { pDst[0] = pSrc[0]; pDst[1] = 128; pDst[2] = 128; }129}130131// Forward DCT - DCT derived from jfdctint.132enum { CONST_BITS = 13, ROW_BITS = 2 };133#define DCT_DESCALE(x, n) (((x) + (((int32)1) << ((n) - 1))) >> (n))134#define DCT_MUL(var, c) (static_cast<int16>(var) * static_cast<int32>(c))135#define DCT1D(s0, s1, s2, s3, s4, s5, s6, s7) \136int32 t0 = s0 + s7, t7 = s0 - s7, t1 = s1 + s6, t6 = s1 - s6, t2 = s2 + s5, t5 = s2 - s5, t3 = s3 + s4, t4 = s3 - s4; \137int32 t10 = t0 + t3, t13 = t0 - t3, t11 = t1 + t2, t12 = t1 - t2; \138int32 u1 = DCT_MUL(t12 + t13, 4433); \139s2 = u1 + DCT_MUL(t13, 6270); \140s6 = u1 + DCT_MUL(t12, -15137); \141u1 = t4 + t7; \142int32 u2 = t5 + t6, u3 = t4 + t6, u4 = t5 + t7; \143int32 z5 = DCT_MUL(u3 + u4, 9633); \144t4 = DCT_MUL(t4, 2446); t5 = DCT_MUL(t5, 16819); \145t6 = DCT_MUL(t6, 25172); t7 = DCT_MUL(t7, 12299); \146u1 = DCT_MUL(u1, -7373); u2 = DCT_MUL(u2, -20995); \147u3 = DCT_MUL(u3, -16069); u4 = DCT_MUL(u4, -3196); \148u3 += z5; u4 += z5; \149s0 = t10 + t11; s1 = t7 + u1 + u4; s3 = t6 + u2 + u3; s4 = t10 - t11; s5 = t5 + u2 + u4; s7 = t4 + u1 + u3;150151static void DCT2D(int32* p)152{153int32 c, * q = p;154for (c = 7; c >= 0; c--, q += 8)155{156int32 s0 = q[0], s1 = q[1], s2 = q[2], s3 = q[3], s4 = q[4], s5 = q[5], s6 = q[6], s7 = q[7];157DCT1D(s0, s1, s2, s3, s4, s5, s6, s7);158q[0] = left_shifti(s0, ROW_BITS); q[1] = DCT_DESCALE(s1, CONST_BITS - ROW_BITS); q[2] = DCT_DESCALE(s2, CONST_BITS - ROW_BITS); q[3] = DCT_DESCALE(s3, CONST_BITS - ROW_BITS);159q[4] = left_shifti(s4, ROW_BITS); q[5] = DCT_DESCALE(s5, CONST_BITS - ROW_BITS); q[6] = DCT_DESCALE(s6, CONST_BITS - ROW_BITS); q[7] = DCT_DESCALE(s7, CONST_BITS - ROW_BITS);160}161for (q = p, c = 7; c >= 0; c--, q++)162{163int32 s0 = q[0 * 8], s1 = q[1 * 8], s2 = q[2 * 8], s3 = q[3 * 8], s4 = q[4 * 8], s5 = q[5 * 8], s6 = q[6 * 8], s7 = q[7 * 8];164DCT1D(s0, s1, s2, s3, s4, s5, s6, s7);165q[0 * 8] = DCT_DESCALE(s0, ROW_BITS + 3); q[1 * 8] = DCT_DESCALE(s1, CONST_BITS + ROW_BITS + 3); q[2 * 8] = DCT_DESCALE(s2, CONST_BITS + ROW_BITS + 3); q[3 * 8] = DCT_DESCALE(s3, CONST_BITS + ROW_BITS + 3);166q[4 * 8] = DCT_DESCALE(s4, ROW_BITS + 3); q[5 * 8] = DCT_DESCALE(s5, CONST_BITS + ROW_BITS + 3); q[6 * 8] = DCT_DESCALE(s6, CONST_BITS + ROW_BITS + 3); q[7 * 8] = DCT_DESCALE(s7, CONST_BITS + ROW_BITS + 3);167}168}169170struct sym_freq { uint m_key, m_sym_index; };171172// Radix sorts sym_freq[] array by 32-bit key m_key. Returns ptr to sorted values.173static inline sym_freq* radix_sort_syms(uint num_syms, sym_freq* pSyms0, sym_freq* pSyms1)174{175const uint cMaxPasses = 4;176uint32 hist[256 * cMaxPasses]; clear_obj(hist);177for (uint i = 0; i < num_syms; i++) { uint freq = pSyms0[i].m_key; hist[freq & 0xFF]++; hist[256 + ((freq >> 8) & 0xFF)]++; hist[256 * 2 + ((freq >> 16) & 0xFF)]++; hist[256 * 3 + ((freq >> 24) & 0xFF)]++; }178sym_freq* pCur_syms = pSyms0, * pNew_syms = pSyms1;179uint total_passes = cMaxPasses; while ((total_passes > 1) && (num_syms == hist[(total_passes - 1) * 256])) total_passes--;180for (uint pass_shift = 0, pass = 0; pass < total_passes; pass++, pass_shift += 8)181{182const uint32* pHist = &hist[pass << 8];183uint offsets[256], cur_ofs = 0;184for (uint i = 0; i < 256; i++) { offsets[i] = cur_ofs; cur_ofs += pHist[i]; }185for (uint i = 0; i < num_syms; i++)186pNew_syms[offsets[(pCur_syms[i].m_key >> pass_shift) & 0xFF]++] = pCur_syms[i];187sym_freq* t = pCur_syms; pCur_syms = pNew_syms; pNew_syms = t;188}189return pCur_syms;190}191192// calculate_minimum_redundancy() originally written by: Alistair Moffat, [email protected], Jyrki Katajainen, [email protected], November 1996.193static void calculate_minimum_redundancy(sym_freq* A, int n)194{195int root, leaf, next, avbl, used, dpth;196if (n == 0) return; else if (n == 1) { A[0].m_key = 1; return; }197A[0].m_key += A[1].m_key; root = 0; leaf = 2;198for (next = 1; next < n - 1; next++)199{200if (leaf >= n || A[root].m_key < A[leaf].m_key) { A[next].m_key = A[root].m_key; A[root++].m_key = next; }201else A[next].m_key = A[leaf++].m_key;202if (leaf >= n || (root < next && A[root].m_key < A[leaf].m_key)) { A[next].m_key += A[root].m_key; A[root++].m_key = next; }203else A[next].m_key += A[leaf++].m_key;204}205A[n - 2].m_key = 0;206for (next = n - 3; next >= 0; next--) A[next].m_key = A[A[next].m_key].m_key + 1;207avbl = 1; used = dpth = 0; root = n - 2; next = n - 1;208while (avbl > 0)209{210while (root >= 0 && (int)A[root].m_key == dpth) { used++; root--; }211while (avbl > used) { A[next--].m_key = dpth; avbl--; }212avbl = 2 * used; dpth++; used = 0;213}214}215216// Limits canonical Huffman code table's max code size to max_code_size.217static void huffman_enforce_max_code_size(int* pNum_codes, int code_list_len, int max_code_size)218{219if (code_list_len <= 1) return;220221for (int i = max_code_size + 1; i <= MAX_HUFF_CODESIZE; i++) pNum_codes[max_code_size] += pNum_codes[i];222223uint32 total = 0;224for (int i = max_code_size; i > 0; i--)225total += (((uint32)pNum_codes[i]) << (max_code_size - i));226227while (total != (1UL << max_code_size))228{229pNum_codes[max_code_size]--;230for (int i = max_code_size - 1; i > 0; i--)231{232if (pNum_codes[i]) { pNum_codes[i]--; pNum_codes[i + 1] += 2; break; }233}234total--;235}236}237238// Generates an optimized offman table.239void jpeg_encoder::optimize_huffman_table(int table_num, int table_len)240{241sym_freq syms0[MAX_HUFF_SYMBOLS], syms1[MAX_HUFF_SYMBOLS];242syms0[0].m_key = 1; syms0[0].m_sym_index = 0; // dummy symbol, assures that no valid code contains all 1's243int num_used_syms = 1;244const uint32* pSym_count = &m_huff_count[table_num][0];245for (int i = 0; i < table_len; i++)246if (pSym_count[i]) { syms0[num_used_syms].m_key = pSym_count[i]; syms0[num_used_syms++].m_sym_index = i + 1; }247sym_freq* pSyms = radix_sort_syms(num_used_syms, syms0, syms1);248calculate_minimum_redundancy(pSyms, num_used_syms);249250// Count the # of symbols of each code size.251int num_codes[1 + MAX_HUFF_CODESIZE]; clear_obj(num_codes);252for (int i = 0; i < num_used_syms; i++)253num_codes[pSyms[i].m_key]++;254255const uint JPGE_CODE_SIZE_LIMIT = 16; // the maximum possible size of a JPEG Huffman code (valid range is [9,16] - 9 vs. 8 because of the dummy symbol)256huffman_enforce_max_code_size(num_codes, num_used_syms, JPGE_CODE_SIZE_LIMIT);257258// Compute m_huff_bits array, which contains the # of symbols per code size.259clear_obj(m_huff_bits[table_num]);260for (int i = 1; i <= (int)JPGE_CODE_SIZE_LIMIT; i++)261m_huff_bits[table_num][i] = static_cast<uint8>(num_codes[i]);262263// Remove the dummy symbol added above, which must be in largest bucket.264for (int i = JPGE_CODE_SIZE_LIMIT; i >= 1; i--)265{266if (m_huff_bits[table_num][i]) { m_huff_bits[table_num][i]--; break; }267}268269// Compute the m_huff_val array, which contains the symbol indices sorted by code size (smallest to largest).270for (int i = num_used_syms - 1; i >= 1; i--)271m_huff_val[table_num][num_used_syms - 1 - i] = static_cast<uint8>(pSyms[i].m_sym_index - 1);272}273274// JPEG marker generation.275void jpeg_encoder::emit_byte(uint8 i)276{277m_all_stream_writes_succeeded = m_all_stream_writes_succeeded && m_pStream->put_obj(i);278}279280void jpeg_encoder::emit_word(uint i)281{282emit_byte(uint8(i >> 8)); emit_byte(uint8(i & 0xFF));283}284285void jpeg_encoder::emit_marker(int marker)286{287emit_byte(uint8(0xFF)); emit_byte(uint8(marker));288}289290// Emit JFIF marker291void jpeg_encoder::emit_jfif_app0()292{293emit_marker(M_APP0);294emit_word(2 + 4 + 1 + 2 + 1 + 2 + 2 + 1 + 1);295emit_byte(0x4A); emit_byte(0x46); emit_byte(0x49); emit_byte(0x46); /* Identifier: ASCII "JFIF" */296emit_byte(0);297emit_byte(1); /* Major version */298emit_byte(1); /* Minor version */299emit_byte(0); /* Density unit */300emit_word(1);301emit_word(1);302emit_byte(0); /* No thumbnail image */303emit_byte(0);304}305306// Emit quantization tables307void jpeg_encoder::emit_dqt()308{309for (int i = 0; i < ((m_num_components == 3) ? 2 : 1); i++)310{311emit_marker(M_DQT);312emit_word(64 + 1 + 2);313emit_byte(static_cast<uint8>(i));314for (int j = 0; j < 64; j++)315emit_byte(static_cast<uint8>(m_quantization_tables[i][j]));316}317}318319// Emit start of frame marker320void jpeg_encoder::emit_sof()321{322emit_marker(M_SOF0); /* baseline */323emit_word(3 * m_num_components + 2 + 5 + 1);324emit_byte(8); /* precision */325emit_word(m_image_y);326emit_word(m_image_x);327emit_byte(m_num_components);328for (int i = 0; i < m_num_components; i++)329{330emit_byte(static_cast<uint8>(i + 1)); /* component ID */331emit_byte((m_comp_h_samp[i] << 4) + m_comp_v_samp[i]); /* h and v sampling */332emit_byte(i > 0); /* quant. table num */333}334}335336// Emit Huffman table.337void jpeg_encoder::emit_dht(uint8* bits, uint8* val, int index, bool ac_flag)338{339emit_marker(M_DHT);340341int length = 0;342for (int i = 1; i <= 16; i++)343length += bits[i];344345emit_word(length + 2 + 1 + 16);346emit_byte(static_cast<uint8>(index + (ac_flag << 4)));347348for (int i = 1; i <= 16; i++)349emit_byte(bits[i]);350351for (int i = 0; i < length; i++)352emit_byte(val[i]);353}354355// Emit all Huffman tables.356void jpeg_encoder::emit_dhts()357{358emit_dht(m_huff_bits[0 + 0], m_huff_val[0 + 0], 0, false);359emit_dht(m_huff_bits[2 + 0], m_huff_val[2 + 0], 0, true);360if (m_num_components == 3)361{362emit_dht(m_huff_bits[0 + 1], m_huff_val[0 + 1], 1, false);363emit_dht(m_huff_bits[2 + 1], m_huff_val[2 + 1], 1, true);364}365}366367// emit start of scan368void jpeg_encoder::emit_sos()369{370emit_marker(M_SOS);371emit_word(2 * m_num_components + 2 + 1 + 3);372emit_byte(m_num_components);373for (int i = 0; i < m_num_components; i++)374{375emit_byte(static_cast<uint8>(i + 1));376if (i == 0)377emit_byte((0 << 4) + 0);378else379emit_byte((1 << 4) + 1);380}381emit_byte(0); /* spectral selection */382emit_byte(63);383emit_byte(0);384}385386// Emit all markers at beginning of image file.387void jpeg_encoder::emit_markers()388{389emit_marker(M_SOI);390emit_jfif_app0();391emit_dqt();392emit_sof();393emit_dhts();394emit_sos();395}396397// Compute the actual canonical Huffman codes/code sizes given the JPEG huff bits and val arrays.398void jpeg_encoder::compute_huffman_table(uint* codes, uint8* code_sizes, uint8* bits, uint8* val)399{400int i, l, last_p, si;401uint8 huff_size[257];402uint huff_code[257];403uint code;404405int p = 0;406for (l = 1; l <= 16; l++)407for (i = 1; i <= bits[l]; i++)408huff_size[p++] = (char)l;409410huff_size[p] = 0; last_p = p; // write sentinel411412code = 0; si = huff_size[0]; p = 0;413414while (huff_size[p])415{416while (huff_size[p] == si)417huff_code[p++] = code++;418code <<= 1;419si++;420}421422memset(codes, 0, sizeof(codes[0]) * 256);423memset(code_sizes, 0, sizeof(code_sizes[0]) * 256);424for (p = 0; p < last_p; p++)425{426codes[val[p]] = huff_code[p];427code_sizes[val[p]] = huff_size[p];428}429}430431// Quantization table generation.432void jpeg_encoder::compute_quant_table(int32* pDst, int16* pSrc)433{434int32 q;435if (m_params.m_quality < 50)436q = 5000 / m_params.m_quality;437else438q = 200 - m_params.m_quality * 2;439for (int i = 0; i < 64; i++)440{441int32 j = *pSrc++; j = (j * q + 50L) / 100L;442*pDst++ = JPGE_MIN(JPGE_MAX(j, 1), 255);443}444}445446// Higher-level methods.447void jpeg_encoder::first_pass_init()448{449m_bit_buffer = 0; m_bits_in = 0;450memset(m_last_dc_val, 0, 3 * sizeof(m_last_dc_val[0]));451m_mcu_y_ofs = 0;452m_pass_num = 1;453}454455bool jpeg_encoder::second_pass_init()456{457compute_huffman_table(&m_huff_codes[0 + 0][0], &m_huff_code_sizes[0 + 0][0], m_huff_bits[0 + 0], m_huff_val[0 + 0]);458compute_huffman_table(&m_huff_codes[2 + 0][0], &m_huff_code_sizes[2 + 0][0], m_huff_bits[2 + 0], m_huff_val[2 + 0]);459if (m_num_components > 1)460{461compute_huffman_table(&m_huff_codes[0 + 1][0], &m_huff_code_sizes[0 + 1][0], m_huff_bits[0 + 1], m_huff_val[0 + 1]);462compute_huffman_table(&m_huff_codes[2 + 1][0], &m_huff_code_sizes[2 + 1][0], m_huff_bits[2 + 1], m_huff_val[2 + 1]);463}464first_pass_init();465emit_markers();466m_pass_num = 2;467return true;468}469470bool jpeg_encoder::jpg_open(int p_x_res, int p_y_res, int src_channels)471{472m_num_components = 3;473switch (m_params.m_subsampling)474{475case Y_ONLY:476{477m_num_components = 1;478m_comp_h_samp[0] = 1; m_comp_v_samp[0] = 1;479m_mcu_x = 8; m_mcu_y = 8;480break;481}482case H1V1:483{484m_comp_h_samp[0] = 1; m_comp_v_samp[0] = 1;485m_comp_h_samp[1] = 1; m_comp_v_samp[1] = 1;486m_comp_h_samp[2] = 1; m_comp_v_samp[2] = 1;487m_mcu_x = 8; m_mcu_y = 8;488break;489}490case H2V1:491{492m_comp_h_samp[0] = 2; m_comp_v_samp[0] = 1;493m_comp_h_samp[1] = 1; m_comp_v_samp[1] = 1;494m_comp_h_samp[2] = 1; m_comp_v_samp[2] = 1;495m_mcu_x = 16; m_mcu_y = 8;496break;497}498case H2V2:499{500m_comp_h_samp[0] = 2; m_comp_v_samp[0] = 2;501m_comp_h_samp[1] = 1; m_comp_v_samp[1] = 1;502m_comp_h_samp[2] = 1; m_comp_v_samp[2] = 1;503m_mcu_x = 16; m_mcu_y = 16;504}505}506507m_image_x = p_x_res; m_image_y = p_y_res;508m_image_bpp = src_channels;509m_image_bpl = m_image_x * src_channels;510m_image_x_mcu = (m_image_x + m_mcu_x - 1) & (~(m_mcu_x - 1));511m_image_y_mcu = (m_image_y + m_mcu_y - 1) & (~(m_mcu_y - 1));512m_image_bpl_xlt = m_image_x * m_num_components;513m_image_bpl_mcu = m_image_x_mcu * m_num_components;514m_mcus_per_row = m_image_x_mcu / m_mcu_x;515516if ((m_mcu_lines[0] = static_cast<uint8*>(jpge_malloc(m_image_bpl_mcu * m_mcu_y))) == NULL) return false;517for (int i = 1; i < m_mcu_y; i++)518m_mcu_lines[i] = m_mcu_lines[i - 1] + m_image_bpl_mcu;519520if (m_params.m_use_std_tables)521{522compute_quant_table(m_quantization_tables[0], s_std_lum_quant);523compute_quant_table(m_quantization_tables[1], m_params.m_no_chroma_discrim_flag ? s_std_lum_quant : s_std_croma_quant);524}525else526{527compute_quant_table(m_quantization_tables[0], s_alt_quant);528memcpy(m_quantization_tables[1], m_quantization_tables[0], sizeof(m_quantization_tables[1]));529}530531m_out_buf_left = JPGE_OUT_BUF_SIZE;532m_pOut_buf = m_out_buf;533534if (m_params.m_two_pass_flag)535{536clear_obj(m_huff_count);537first_pass_init();538}539else540{541memcpy(m_huff_bits[0 + 0], s_dc_lum_bits, 17); memcpy(m_huff_val[0 + 0], s_dc_lum_val, DC_LUM_CODES);542memcpy(m_huff_bits[2 + 0], s_ac_lum_bits, 17); memcpy(m_huff_val[2 + 0], s_ac_lum_val, AC_LUM_CODES);543memcpy(m_huff_bits[0 + 1], s_dc_chroma_bits, 17); memcpy(m_huff_val[0 + 1], s_dc_chroma_val, DC_CHROMA_CODES);544memcpy(m_huff_bits[2 + 1], s_ac_chroma_bits, 17); memcpy(m_huff_val[2 + 1], s_ac_chroma_val, AC_CHROMA_CODES);545if (!second_pass_init()) return false; // in effect, skip over the first pass546}547return m_all_stream_writes_succeeded;548}549550void jpeg_encoder::load_block_8_8_grey(int x)551{552uint8* pSrc;553sample_array_t* pDst = m_sample_array;554x <<= 3;555for (int i = 0; i < 8; i++, pDst += 8)556{557pSrc = m_mcu_lines[i] + x;558pDst[0] = pSrc[0] - 128; pDst[1] = pSrc[1] - 128; pDst[2] = pSrc[2] - 128; pDst[3] = pSrc[3] - 128;559pDst[4] = pSrc[4] - 128; pDst[5] = pSrc[5] - 128; pDst[6] = pSrc[6] - 128; pDst[7] = pSrc[7] - 128;560}561}562563void jpeg_encoder::load_block_8_8(int x, int y, int c)564{565uint8* pSrc;566sample_array_t* pDst = m_sample_array;567x = (x * (8 * 3)) + c;568y <<= 3;569for (int i = 0; i < 8; i++, pDst += 8)570{571pSrc = m_mcu_lines[y + i] + x;572pDst[0] = pSrc[0 * 3] - 128; pDst[1] = pSrc[1 * 3] - 128; pDst[2] = pSrc[2 * 3] - 128; pDst[3] = pSrc[3 * 3] - 128;573pDst[4] = pSrc[4 * 3] - 128; pDst[5] = pSrc[5 * 3] - 128; pDst[6] = pSrc[6 * 3] - 128; pDst[7] = pSrc[7 * 3] - 128;574}575}576577void jpeg_encoder::load_block_16_8(int x, int c)578{579uint8* pSrc1, * pSrc2;580sample_array_t* pDst = m_sample_array;581x = (x * (16 * 3)) + c;582for (int i = 0; i < 16; i += 2, pDst += 8)583{584pSrc1 = m_mcu_lines[i + 0] + x;585pSrc2 = m_mcu_lines[i + 1] + x;586pDst[0] = ((pSrc1[0 * 3] + pSrc1[1 * 3] + pSrc2[0 * 3] + pSrc2[1 * 3] + 2) >> 2) - 128; pDst[1] = ((pSrc1[2 * 3] + pSrc1[3 * 3] + pSrc2[2 * 3] + pSrc2[3 * 3] + 2) >> 2) - 128;587pDst[2] = ((pSrc1[4 * 3] + pSrc1[5 * 3] + pSrc2[4 * 3] + pSrc2[5 * 3] + 2) >> 2) - 128; pDst[3] = ((pSrc1[6 * 3] + pSrc1[7 * 3] + pSrc2[6 * 3] + pSrc2[7 * 3] + 2) >> 2) - 128;588pDst[4] = ((pSrc1[8 * 3] + pSrc1[9 * 3] + pSrc2[8 * 3] + pSrc2[9 * 3] + 2) >> 2) - 128; pDst[5] = ((pSrc1[10 * 3] + pSrc1[11 * 3] + pSrc2[10 * 3] + pSrc2[11 * 3] + 2) >> 2) - 128;589pDst[6] = ((pSrc1[12 * 3] + pSrc1[13 * 3] + pSrc2[12 * 3] + pSrc2[13 * 3] + 2) >> 2) - 128; pDst[7] = ((pSrc1[14 * 3] + pSrc1[15 * 3] + pSrc2[14 * 3] + pSrc2[15 * 3] + 2) >> 2) - 128;590}591}592593void jpeg_encoder::load_block_16_8_8(int x, int c)594{595uint8* pSrc1;596sample_array_t* pDst = m_sample_array;597x = (x * (16 * 3)) + c;598for (int i = 0; i < 8; i++, pDst += 8)599{600pSrc1 = m_mcu_lines[i + 0] + x;601pDst[0] = ((pSrc1[0 * 3] + pSrc1[1 * 3] + 1) >> 1) - 128; pDst[1] = ((pSrc1[2 * 3] + pSrc1[3 * 3] + 1) >> 1) - 128;602pDst[2] = ((pSrc1[4 * 3] + pSrc1[5 * 3] + 1) >> 1) - 128; pDst[3] = ((pSrc1[6 * 3] + pSrc1[7 * 3] + 1) >> 1) - 128;603pDst[4] = ((pSrc1[8 * 3] + pSrc1[9 * 3] + 1) >> 1) - 128; pDst[5] = ((pSrc1[10 * 3] + pSrc1[11 * 3] + 1) >> 1) - 128;604pDst[6] = ((pSrc1[12 * 3] + pSrc1[13 * 3] + 1) >> 1) - 128; pDst[7] = ((pSrc1[14 * 3] + pSrc1[15 * 3] + 1) >> 1) - 128;605}606}607608void jpeg_encoder::load_quantized_coefficients(int component_num)609{610int32* q = m_quantization_tables[component_num > 0];611int16* pDst = m_coefficient_array;612for (int i = 0; i < 64; i++)613{614sample_array_t j = m_sample_array[s_zag[i]];615if (j < 0)616{617if ((j = -j + (*q >> 1)) < *q)618*pDst++ = 0;619else620*pDst++ = static_cast<int16>(-(j / *q));621}622else623{624if ((j = j + (*q >> 1)) < *q)625*pDst++ = 0;626else627*pDst++ = static_cast<int16>((j / *q));628}629q++;630}631}632633void jpeg_encoder::flush_output_buffer()634{635if (m_out_buf_left != JPGE_OUT_BUF_SIZE)636m_all_stream_writes_succeeded = m_all_stream_writes_succeeded && m_pStream->put_buf(m_out_buf, JPGE_OUT_BUF_SIZE - m_out_buf_left);637m_pOut_buf = m_out_buf;638m_out_buf_left = JPGE_OUT_BUF_SIZE;639}640641void jpeg_encoder::put_bits(uint bits, uint len)642{643m_bit_buffer |= ((uint32)bits << (24 - (m_bits_in += len)));644while (m_bits_in >= 8)645{646uint8 c;647#define JPGE_PUT_BYTE(c) { *m_pOut_buf++ = (c); if (--m_out_buf_left == 0) flush_output_buffer(); }648JPGE_PUT_BYTE(c = (uint8)((m_bit_buffer >> 16) & 0xFF));649if (c == 0xFF) JPGE_PUT_BYTE(0);650m_bit_buffer <<= 8;651m_bits_in -= 8;652}653}654655void jpeg_encoder::code_coefficients_pass_one(int component_num)656{657if (component_num >= 3) return; // just to shut up static analysis658int i, run_len, nbits, temp1;659int16* src = m_coefficient_array;660uint32* dc_count = component_num ? m_huff_count[0 + 1] : m_huff_count[0 + 0], * ac_count = component_num ? m_huff_count[2 + 1] : m_huff_count[2 + 0];661662temp1 = src[0] - m_last_dc_val[component_num];663m_last_dc_val[component_num] = src[0];664if (temp1 < 0) temp1 = -temp1;665666nbits = 0;667while (temp1)668{669nbits++; temp1 >>= 1;670}671672dc_count[nbits]++;673for (run_len = 0, i = 1; i < 64; i++)674{675if ((temp1 = m_coefficient_array[i]) == 0)676run_len++;677else678{679while (run_len >= 16)680{681ac_count[0xF0]++;682run_len -= 16;683}684if (temp1 < 0) temp1 = -temp1;685nbits = 1;686while (temp1 >>= 1) nbits++;687ac_count[(run_len << 4) + nbits]++;688run_len = 0;689}690}691if (run_len) ac_count[0]++;692}693694void jpeg_encoder::code_coefficients_pass_two(int component_num)695{696int i, j, run_len, nbits, temp1, temp2;697int16* pSrc = m_coefficient_array;698uint* codes[2];699uint8* code_sizes[2];700701if (component_num == 0)702{703codes[0] = m_huff_codes[0 + 0]; codes[1] = m_huff_codes[2 + 0];704code_sizes[0] = m_huff_code_sizes[0 + 0]; code_sizes[1] = m_huff_code_sizes[2 + 0];705}706else707{708codes[0] = m_huff_codes[0 + 1]; codes[1] = m_huff_codes[2 + 1];709code_sizes[0] = m_huff_code_sizes[0 + 1]; code_sizes[1] = m_huff_code_sizes[2 + 1];710}711712temp1 = temp2 = pSrc[0] - m_last_dc_val[component_num];713m_last_dc_val[component_num] = pSrc[0];714715if (temp1 < 0)716{717temp1 = -temp1; temp2--;718}719720nbits = 0;721while (temp1)722{723nbits++; temp1 >>= 1;724}725726put_bits(codes[0][nbits], code_sizes[0][nbits]);727if (nbits) put_bits(temp2 & ((1 << nbits) - 1), nbits);728729for (run_len = 0, i = 1; i < 64; i++)730{731if ((temp1 = m_coefficient_array[i]) == 0)732run_len++;733else734{735while (run_len >= 16)736{737put_bits(codes[1][0xF0], code_sizes[1][0xF0]);738run_len -= 16;739}740if ((temp2 = temp1) < 0)741{742temp1 = -temp1;743temp2--;744}745nbits = 1;746while (temp1 >>= 1)747nbits++;748j = (run_len << 4) + nbits;749put_bits(codes[1][j], code_sizes[1][j]);750put_bits(temp2 & ((1 << nbits) - 1), nbits);751run_len = 0;752}753}754if (run_len)755put_bits(codes[1][0], code_sizes[1][0]);756}757758void jpeg_encoder::code_block(int component_num)759{760DCT2D(m_sample_array);761load_quantized_coefficients(component_num);762if (m_pass_num == 1)763code_coefficients_pass_one(component_num);764else765code_coefficients_pass_two(component_num);766}767768void jpeg_encoder::process_mcu_row()769{770if (m_num_components == 1)771{772for (int i = 0; i < m_mcus_per_row; i++)773{774load_block_8_8_grey(i); code_block(0);775}776}777else if ((m_comp_h_samp[0] == 1) && (m_comp_v_samp[0] == 1))778{779for (int i = 0; i < m_mcus_per_row; i++)780{781load_block_8_8(i, 0, 0); code_block(0); load_block_8_8(i, 0, 1); code_block(1); load_block_8_8(i, 0, 2); code_block(2);782}783}784else if ((m_comp_h_samp[0] == 2) && (m_comp_v_samp[0] == 1))785{786for (int i = 0; i < m_mcus_per_row; i++)787{788load_block_8_8(i * 2 + 0, 0, 0); code_block(0); load_block_8_8(i * 2 + 1, 0, 0); code_block(0);789load_block_16_8_8(i, 1); code_block(1); load_block_16_8_8(i, 2); code_block(2);790}791}792else if ((m_comp_h_samp[0] == 2) && (m_comp_v_samp[0] == 2))793{794for (int i = 0; i < m_mcus_per_row; i++)795{796load_block_8_8(i * 2 + 0, 0, 0); code_block(0); load_block_8_8(i * 2 + 1, 0, 0); code_block(0);797load_block_8_8(i * 2 + 0, 1, 0); code_block(0); load_block_8_8(i * 2 + 1, 1, 0); code_block(0);798load_block_16_8(i, 1); code_block(1); load_block_16_8(i, 2); code_block(2);799}800}801}802803bool jpeg_encoder::terminate_pass_one()804{805optimize_huffman_table(0 + 0, DC_LUM_CODES); optimize_huffman_table(2 + 0, AC_LUM_CODES);806if (m_num_components > 1)807{808optimize_huffman_table(0 + 1, DC_CHROMA_CODES); optimize_huffman_table(2 + 1, AC_CHROMA_CODES);809}810return second_pass_init();811}812813bool jpeg_encoder::terminate_pass_two()814{815put_bits(0x7F, 7);816flush_output_buffer();817emit_marker(M_EOI);818m_pass_num++; // purposely bump up m_pass_num, for debugging819return true;820}821822bool jpeg_encoder::process_end_of_image()823{824if (m_mcu_y_ofs)825{826if (m_mcu_y_ofs < 16) // check here just to shut up static analysis827{828for (int i = m_mcu_y_ofs; i < m_mcu_y; i++)829memcpy(m_mcu_lines[i], m_mcu_lines[m_mcu_y_ofs - 1], m_image_bpl_mcu);830}831832process_mcu_row();833}834835if (m_pass_num == 1)836return terminate_pass_one();837else838return terminate_pass_two();839}840841void jpeg_encoder::load_mcu(const void* pSrc)842{843const uint8* Psrc = reinterpret_cast<const uint8*>(pSrc);844845uint8* pDst = m_mcu_lines[m_mcu_y_ofs]; // OK to write up to m_image_bpl_xlt bytes to pDst846847if (m_num_components == 1)848{849if (m_image_bpp == 4)850RGBA_to_Y(pDst, Psrc, m_image_x);851else if (m_image_bpp == 3)852RGB_to_Y(pDst, Psrc, m_image_x);853else854memcpy(pDst, Psrc, m_image_x);855}856else857{858if (m_image_bpp == 4)859RGBA_to_YCC(pDst, Psrc, m_image_x);860else if (m_image_bpp == 3)861RGB_to_YCC(pDst, Psrc, m_image_x);862else863Y_to_YCC(pDst, Psrc, m_image_x);864}865866// Possibly duplicate pixels at end of scanline if not a multiple of 8 or 16867if (m_num_components == 1)868memset(m_mcu_lines[m_mcu_y_ofs] + m_image_bpl_xlt, pDst[m_image_bpl_xlt - 1], m_image_x_mcu - m_image_x);869else870{871const uint8 y = pDst[m_image_bpl_xlt - 3 + 0], cb = pDst[m_image_bpl_xlt - 3 + 1], cr = pDst[m_image_bpl_xlt - 3 + 2];872uint8* q = m_mcu_lines[m_mcu_y_ofs] + m_image_bpl_xlt;873for (int i = m_image_x; i < m_image_x_mcu; i++)874{875*q++ = y; *q++ = cb; *q++ = cr;876}877}878879if (++m_mcu_y_ofs == m_mcu_y)880{881process_mcu_row();882m_mcu_y_ofs = 0;883}884}885886void jpeg_encoder::clear()887{888m_mcu_lines[0] = NULL;889m_pass_num = 0;890m_all_stream_writes_succeeded = true;891}892893jpeg_encoder::jpeg_encoder()894{895clear();896}897898jpeg_encoder::~jpeg_encoder()899{900deinit();901}902903bool jpeg_encoder::init(output_stream* pStream, int width, int height, int src_channels, const params& comp_params)904{905deinit();906if (((!pStream) || (width < 1) || (height < 1)) || ((src_channels != 1) && (src_channels != 3) && (src_channels != 4)) || (!comp_params.check())) return false;907m_pStream = pStream;908m_params = comp_params;909return jpg_open(width, height, src_channels);910}911912void jpeg_encoder::deinit()913{914jpge_free(m_mcu_lines[0]);915clear();916}917918bool jpeg_encoder::process_scanline(const void* pScanline)919{920if ((m_pass_num < 1) || (m_pass_num > 2)) return false;921if (m_all_stream_writes_succeeded)922{923if (!pScanline)924{925if (!process_end_of_image()) return false;926}927else928{929load_mcu(pScanline);930}931}932return m_all_stream_writes_succeeded;933}934935// Higher level wrappers/examples (optional).936937class cfile_stream : public output_stream938{939cfile_stream(const cfile_stream&);940cfile_stream& operator= (const cfile_stream&);941942FILE* m_pFile;943bool m_bStatus;944945public:946cfile_stream() : m_pFile(NULL), m_bStatus(false) { }947948virtual ~cfile_stream()949{950close();951}952953bool open(const char* pFilename)954{955close();956m_pFile = fopen(pFilename, "wb");957m_bStatus = (m_pFile != NULL);958return m_bStatus;959}960961bool close()962{963if (m_pFile)964{965if (fclose(m_pFile) == EOF)966{967m_bStatus = false;968}969m_pFile = NULL;970}971return m_bStatus;972}973974virtual bool put_buf(const void* pBuf, int len)975{976m_bStatus = m_bStatus && (fwrite(pBuf, len, 1, m_pFile) == 1);977return m_bStatus;978}979980uint get_size() const981{982return m_pFile ? ftell(m_pFile) : 0;983}984};985986// Writes JPEG image to file.987bool compress_image_to_jpeg_file(const char* pFilename, int width, int height, int num_channels, const uint8* pImage_data, const params& comp_params)988{989cfile_stream dst_stream;990if (!dst_stream.open(pFilename))991return false;992993jpge::jpeg_encoder dst_image;994if (!dst_image.init(&dst_stream, width, height, num_channels, comp_params))995return false;996997for (uint pass_index = 0; pass_index < dst_image.get_total_passes(); pass_index++)998{999for (int i = 0; i < height; i++)1000{1001const uint8* pBuf = pImage_data + i * width * num_channels;1002if (!dst_image.process_scanline(pBuf))1003return false;1004}1005if (!dst_image.process_scanline(NULL))1006return false;1007}10081009dst_image.deinit();10101011return dst_stream.close();1012}10131014class memory_stream : public output_stream1015{1016memory_stream(const memory_stream&);1017memory_stream& operator= (const memory_stream&);10181019uint8* m_pBuf;1020uint m_buf_size, m_buf_ofs;10211022public:1023memory_stream(void* pBuf, uint buf_size) : m_pBuf(static_cast<uint8*>(pBuf)), m_buf_size(buf_size), m_buf_ofs(0) { }10241025virtual ~memory_stream() { }10261027virtual bool put_buf(const void* pBuf, int len)1028{1029uint buf_remaining = m_buf_size - m_buf_ofs;1030if ((uint)len > buf_remaining)1031return false;1032memcpy(m_pBuf + m_buf_ofs, pBuf, len);1033m_buf_ofs += len;1034return true;1035}10361037uint get_size() const1038{1039return m_buf_ofs;1040}1041};10421043bool compress_image_to_jpeg_file_in_memory(void* pDstBuf, int& buf_size, int width, int height, int num_channels, const uint8* pImage_data, const params& comp_params)1044{1045if ((!pDstBuf) || (!buf_size))1046return false;10471048memory_stream dst_stream(pDstBuf, buf_size);10491050buf_size = 0;10511052jpge::jpeg_encoder dst_image;1053if (!dst_image.init(&dst_stream, width, height, num_channels, comp_params))1054return false;10551056for (uint pass_index = 0; pass_index < dst_image.get_total_passes(); pass_index++)1057{1058for (int i = 0; i < height; i++)1059{1060const uint8* pScanline = pImage_data + i * width * num_channels;1061if (!dst_image.process_scanline(pScanline))1062return false;1063}1064if (!dst_image.process_scanline(NULL))1065return false;1066}10671068dst_image.deinit();10691070buf_size = dst_stream.get_size();1071return true;1072}10731074} // namespace jpge107510761077