add jpg library, export as jpg, export animation frames, add pressure support on mac
This commit is contained in:
3172
libs/jpeg/jpgd.cpp
Normal file
3172
libs/jpeg/jpgd.cpp
Normal file
File diff suppressed because it is too large
Load Diff
319
libs/jpeg/jpgd.h
Normal file
319
libs/jpeg/jpgd.h
Normal file
@@ -0,0 +1,319 @@
|
||||
// jpgd.h - C++ class for JPEG decompression.
|
||||
// Public domain, Rich Geldreich <richgel99@gmail.com>
|
||||
#ifndef JPEG_DECODER_H
|
||||
#define JPEG_DECODER_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <setjmp.h>
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#define JPGD_NORETURN __declspec(noreturn)
|
||||
#elif defined(__GNUC__)
|
||||
#define JPGD_NORETURN __attribute__ ((noreturn))
|
||||
#else
|
||||
#define JPGD_NORETURN
|
||||
#endif
|
||||
|
||||
namespace jpgd
|
||||
{
|
||||
typedef unsigned char uint8;
|
||||
typedef signed short int16;
|
||||
typedef unsigned short uint16;
|
||||
typedef unsigned int uint;
|
||||
typedef signed int int32;
|
||||
|
||||
// Loads a JPEG image from a memory buffer or a file.
|
||||
// req_comps can be 1 (grayscale), 3 (RGB), or 4 (RGBA).
|
||||
// On return, width/height will be set to the image's dimensions, and actual_comps will be set to the either 1 (grayscale) or 3 (RGB).
|
||||
// Notes: For more control over where and how the source data is read, see the decompress_jpeg_image_from_stream() function below, or call the jpeg_decoder class directly.
|
||||
// Requesting a 8 or 32bpp image is currently a little faster than 24bpp because the jpeg_decoder class itself currently always unpacks to either 8 or 32bpp.
|
||||
unsigned char *decompress_jpeg_image_from_memory(const unsigned char *pSrc_data, int src_data_size, int *width, int *height, int *actual_comps, int req_comps);
|
||||
unsigned char *decompress_jpeg_image_from_file(const char *pSrc_filename, int *width, int *height, int *actual_comps, int req_comps);
|
||||
|
||||
// Success/failure error codes.
|
||||
enum jpgd_status
|
||||
{
|
||||
JPGD_SUCCESS = 0, JPGD_FAILED = -1, JPGD_DONE = 1,
|
||||
JPGD_BAD_DHT_COUNTS = -256, JPGD_BAD_DHT_INDEX, JPGD_BAD_DHT_MARKER, JPGD_BAD_DQT_MARKER, JPGD_BAD_DQT_TABLE,
|
||||
JPGD_BAD_PRECISION, JPGD_BAD_HEIGHT, JPGD_BAD_WIDTH, JPGD_TOO_MANY_COMPONENTS,
|
||||
JPGD_BAD_SOF_LENGTH, JPGD_BAD_VARIABLE_MARKER, JPGD_BAD_DRI_LENGTH, JPGD_BAD_SOS_LENGTH,
|
||||
JPGD_BAD_SOS_COMP_ID, JPGD_W_EXTRA_BYTES_BEFORE_MARKER, JPGD_NO_ARITHMITIC_SUPPORT, JPGD_UNEXPECTED_MARKER,
|
||||
JPGD_NOT_JPEG, JPGD_UNSUPPORTED_MARKER, JPGD_BAD_DQT_LENGTH, JPGD_TOO_MANY_BLOCKS,
|
||||
JPGD_UNDEFINED_QUANT_TABLE, JPGD_UNDEFINED_HUFF_TABLE, JPGD_NOT_SINGLE_SCAN, JPGD_UNSUPPORTED_COLORSPACE,
|
||||
JPGD_UNSUPPORTED_SAMP_FACTORS, JPGD_DECODE_ERROR, JPGD_BAD_RESTART_MARKER, JPGD_ASSERTION_ERROR,
|
||||
JPGD_BAD_SOS_SPECTRAL, JPGD_BAD_SOS_SUCCESSIVE, JPGD_STREAM_READ, JPGD_NOTENOUGHMEM
|
||||
};
|
||||
|
||||
// Input stream interface.
|
||||
// Derive from this class to read input data from sources other than files or memory. Set m_eof_flag to true when no more data is available.
|
||||
// The decoder is rather greedy: it will keep on calling this method until its internal input buffer is full, or until the EOF flag is set.
|
||||
// It the input stream contains data after the JPEG stream's EOI (end of image) marker it will probably be pulled into the internal buffer.
|
||||
// Call the get_total_bytes_read() method to determine the actual size of the JPEG stream after successful decoding.
|
||||
class jpeg_decoder_stream
|
||||
{
|
||||
public:
|
||||
jpeg_decoder_stream() { }
|
||||
virtual ~jpeg_decoder_stream() { }
|
||||
|
||||
// The read() method is called when the internal input buffer is empty.
|
||||
// Parameters:
|
||||
// pBuf - input buffer
|
||||
// max_bytes_to_read - maximum bytes that can be written to pBuf
|
||||
// pEOF_flag - set this to true if at end of stream (no more bytes remaining)
|
||||
// Returns -1 on error, otherwise return the number of bytes actually written to the buffer (which may be 0).
|
||||
// Notes: This method will be called in a loop until you set *pEOF_flag to true or the internal buffer is full.
|
||||
virtual int read(uint8 *pBuf, int max_bytes_to_read, bool *pEOF_flag) = 0;
|
||||
};
|
||||
|
||||
// stdio FILE stream class.
|
||||
class jpeg_decoder_file_stream : public jpeg_decoder_stream
|
||||
{
|
||||
jpeg_decoder_file_stream(const jpeg_decoder_file_stream &);
|
||||
jpeg_decoder_file_stream &operator =(const jpeg_decoder_file_stream &);
|
||||
|
||||
FILE *m_pFile;
|
||||
bool m_eof_flag, m_error_flag;
|
||||
|
||||
public:
|
||||
jpeg_decoder_file_stream();
|
||||
virtual ~jpeg_decoder_file_stream();
|
||||
|
||||
bool open(const char *Pfilename);
|
||||
void close();
|
||||
|
||||
virtual int read(uint8 *pBuf, int max_bytes_to_read, bool *pEOF_flag);
|
||||
};
|
||||
|
||||
// Memory stream class.
|
||||
class jpeg_decoder_mem_stream : public jpeg_decoder_stream
|
||||
{
|
||||
const uint8 *m_pSrc_data;
|
||||
uint m_ofs, m_size;
|
||||
|
||||
public:
|
||||
jpeg_decoder_mem_stream() : m_pSrc_data(NULL), m_ofs(0), m_size(0) { }
|
||||
jpeg_decoder_mem_stream(const uint8 *pSrc_data, uint size) : m_pSrc_data(pSrc_data), m_ofs(0), m_size(size) { }
|
||||
|
||||
virtual ~jpeg_decoder_mem_stream() { }
|
||||
|
||||
bool open(const uint8 *pSrc_data, uint size);
|
||||
void close() { m_pSrc_data = NULL; m_ofs = 0; m_size = 0; }
|
||||
|
||||
virtual int read(uint8 *pBuf, int max_bytes_to_read, bool *pEOF_flag);
|
||||
};
|
||||
|
||||
// Loads JPEG file from a jpeg_decoder_stream.
|
||||
unsigned char *decompress_jpeg_image_from_stream(jpeg_decoder_stream *pStream, int *width, int *height, int *actual_comps, int req_comps);
|
||||
|
||||
enum
|
||||
{
|
||||
JPGD_IN_BUF_SIZE = 8192, JPGD_MAX_BLOCKS_PER_MCU = 10, JPGD_MAX_HUFF_TABLES = 8, JPGD_MAX_QUANT_TABLES = 4,
|
||||
JPGD_MAX_COMPONENTS = 4, JPGD_MAX_COMPS_IN_SCAN = 4, JPGD_MAX_BLOCKS_PER_ROW = 8192, JPGD_MAX_HEIGHT = 16384, JPGD_MAX_WIDTH = 16384
|
||||
};
|
||||
|
||||
typedef int16 jpgd_quant_t;
|
||||
typedef int16 jpgd_block_t;
|
||||
|
||||
class jpeg_decoder
|
||||
{
|
||||
public:
|
||||
// Call get_error_code() after constructing to determine if the stream is valid or not. You may call the get_width(), get_height(), etc.
|
||||
// methods after the constructor is called. You may then either destruct the object, or begin decoding the image by calling begin_decoding(), then decode() on each scanline.
|
||||
jpeg_decoder(jpeg_decoder_stream *pStream);
|
||||
|
||||
~jpeg_decoder();
|
||||
|
||||
// Call this method after constructing the object to begin decompression.
|
||||
// If JPGD_SUCCESS is returned you may then call decode() on each scanline.
|
||||
int begin_decoding();
|
||||
|
||||
// Returns the next scan line.
|
||||
// For grayscale images, pScan_line will point to a buffer containing 8-bit pixels (get_bytes_per_pixel() will return 1).
|
||||
// Otherwise, it will always point to a buffer containing 32-bit RGBA pixels (A will always be 255, and get_bytes_per_pixel() will return 4).
|
||||
// Returns JPGD_SUCCESS if a scan line has been returned.
|
||||
// Returns JPGD_DONE if all scan lines have been returned.
|
||||
// Returns JPGD_FAILED if an error occurred. Call get_error_code() for a more info.
|
||||
int decode(const void** pScan_line, uint* pScan_line_len);
|
||||
|
||||
inline jpgd_status get_error_code() const { return m_error_code; }
|
||||
|
||||
inline int get_width() const { return m_image_x_size; }
|
||||
inline int get_height() const { return m_image_y_size; }
|
||||
|
||||
inline int get_num_components() const { return m_comps_in_frame; }
|
||||
|
||||
inline int get_bytes_per_pixel() const { return m_dest_bytes_per_pixel; }
|
||||
inline int get_bytes_per_scan_line() const { return m_image_x_size * get_bytes_per_pixel(); }
|
||||
|
||||
// Returns the total number of bytes actually consumed by the decoder (which should equal the actual size of the JPEG file).
|
||||
inline int get_total_bytes_read() const { return m_total_bytes_read; }
|
||||
|
||||
private:
|
||||
jpeg_decoder(const jpeg_decoder &);
|
||||
jpeg_decoder &operator =(const jpeg_decoder &);
|
||||
|
||||
typedef void (*pDecode_block_func)(jpeg_decoder *, int, int, int);
|
||||
|
||||
struct huff_tables
|
||||
{
|
||||
bool ac_table;
|
||||
uint look_up[256];
|
||||
uint look_up2[256];
|
||||
uint8 code_size[256];
|
||||
uint tree[512];
|
||||
};
|
||||
|
||||
struct coeff_buf
|
||||
{
|
||||
uint8 *pData;
|
||||
int block_num_x, block_num_y;
|
||||
int block_len_x, block_len_y;
|
||||
int block_size;
|
||||
};
|
||||
|
||||
struct mem_block
|
||||
{
|
||||
mem_block *m_pNext;
|
||||
size_t m_used_count;
|
||||
size_t m_size;
|
||||
char m_data[1];
|
||||
};
|
||||
|
||||
jmp_buf m_jmp_state;
|
||||
mem_block *m_pMem_blocks;
|
||||
int m_image_x_size;
|
||||
int m_image_y_size;
|
||||
jpeg_decoder_stream *m_pStream;
|
||||
int m_progressive_flag;
|
||||
uint8 m_huff_ac[JPGD_MAX_HUFF_TABLES];
|
||||
uint8* m_huff_num[JPGD_MAX_HUFF_TABLES]; // pointer to number of Huffman codes per bit size
|
||||
uint8* m_huff_val[JPGD_MAX_HUFF_TABLES]; // pointer to Huffman codes per bit size
|
||||
jpgd_quant_t* m_quant[JPGD_MAX_QUANT_TABLES]; // pointer to quantization tables
|
||||
int m_scan_type; // Gray, Yh1v1, Yh1v2, Yh2v1, Yh2v2 (CMYK111, CMYK4114 no longer supported)
|
||||
int m_comps_in_frame; // # of components in frame
|
||||
int m_comp_h_samp[JPGD_MAX_COMPONENTS]; // component's horizontal sampling factor
|
||||
int m_comp_v_samp[JPGD_MAX_COMPONENTS]; // component's vertical sampling factor
|
||||
int m_comp_quant[JPGD_MAX_COMPONENTS]; // component's quantization table selector
|
||||
int m_comp_ident[JPGD_MAX_COMPONENTS]; // component's ID
|
||||
int m_comp_h_blocks[JPGD_MAX_COMPONENTS];
|
||||
int m_comp_v_blocks[JPGD_MAX_COMPONENTS];
|
||||
int m_comps_in_scan; // # of components in scan
|
||||
int m_comp_list[JPGD_MAX_COMPS_IN_SCAN]; // components in this scan
|
||||
int m_comp_dc_tab[JPGD_MAX_COMPONENTS]; // component's DC Huffman coding table selector
|
||||
int m_comp_ac_tab[JPGD_MAX_COMPONENTS]; // component's AC Huffman coding table selector
|
||||
int m_spectral_start; // spectral selection start
|
||||
int m_spectral_end; // spectral selection end
|
||||
int m_successive_low; // successive approximation low
|
||||
int m_successive_high; // successive approximation high
|
||||
int m_max_mcu_x_size; // MCU's max. X size in pixels
|
||||
int m_max_mcu_y_size; // MCU's max. Y size in pixels
|
||||
int m_blocks_per_mcu;
|
||||
int m_max_blocks_per_row;
|
||||
int m_mcus_per_row, m_mcus_per_col;
|
||||
int m_mcu_org[JPGD_MAX_BLOCKS_PER_MCU];
|
||||
int m_total_lines_left; // total # lines left in image
|
||||
int m_mcu_lines_left; // total # lines left in this MCU
|
||||
int m_real_dest_bytes_per_scan_line;
|
||||
int m_dest_bytes_per_scan_line; // rounded up
|
||||
int m_dest_bytes_per_pixel; // 4 (RGB) or 1 (Y)
|
||||
huff_tables* m_pHuff_tabs[JPGD_MAX_HUFF_TABLES];
|
||||
coeff_buf* m_dc_coeffs[JPGD_MAX_COMPONENTS];
|
||||
coeff_buf* m_ac_coeffs[JPGD_MAX_COMPONENTS];
|
||||
int m_eob_run;
|
||||
int m_block_y_mcu[JPGD_MAX_COMPONENTS];
|
||||
uint8* m_pIn_buf_ofs;
|
||||
int m_in_buf_left;
|
||||
int m_tem_flag;
|
||||
bool m_eof_flag;
|
||||
uint8 m_in_buf_pad_start[128];
|
||||
uint8 m_in_buf[JPGD_IN_BUF_SIZE + 128];
|
||||
uint8 m_in_buf_pad_end[128];
|
||||
int m_bits_left;
|
||||
uint m_bit_buf;
|
||||
int m_restart_interval;
|
||||
int m_restarts_left;
|
||||
int m_next_restart_num;
|
||||
int m_max_mcus_per_row;
|
||||
int m_max_blocks_per_mcu;
|
||||
int m_expanded_blocks_per_mcu;
|
||||
int m_expanded_blocks_per_row;
|
||||
int m_expanded_blocks_per_component;
|
||||
bool m_freq_domain_chroma_upsample;
|
||||
int m_max_mcus_per_col;
|
||||
uint m_last_dc_val[JPGD_MAX_COMPONENTS];
|
||||
jpgd_block_t* m_pMCU_coefficients;
|
||||
int m_mcu_block_max_zag[JPGD_MAX_BLOCKS_PER_MCU];
|
||||
uint8* m_pSample_buf;
|
||||
int m_crr[256];
|
||||
int m_cbb[256];
|
||||
int m_crg[256];
|
||||
int m_cbg[256];
|
||||
uint8* m_pScan_line_0;
|
||||
uint8* m_pScan_line_1;
|
||||
jpgd_status m_error_code;
|
||||
bool m_ready_flag;
|
||||
int m_total_bytes_read;
|
||||
|
||||
void free_all_blocks();
|
||||
JPGD_NORETURN void stop_decoding(jpgd_status status);
|
||||
void *alloc(size_t n, bool zero = false);
|
||||
void word_clear(void *p, uint16 c, uint n);
|
||||
void prep_in_buffer();
|
||||
void read_dht_marker();
|
||||
void read_dqt_marker();
|
||||
void read_sof_marker();
|
||||
void skip_variable_marker();
|
||||
void read_dri_marker();
|
||||
void read_sos_marker();
|
||||
int next_marker();
|
||||
int process_markers();
|
||||
void locate_soi_marker();
|
||||
void locate_sof_marker();
|
||||
int locate_sos_marker();
|
||||
void init(jpeg_decoder_stream * pStream);
|
||||
void create_look_ups();
|
||||
void fix_in_buffer();
|
||||
void transform_mcu(int mcu_row);
|
||||
void transform_mcu_expand(int mcu_row);
|
||||
coeff_buf* coeff_buf_open(int block_num_x, int block_num_y, int block_len_x, int block_len_y);
|
||||
inline jpgd_block_t *coeff_buf_getp(coeff_buf *cb, int block_x, int block_y);
|
||||
void load_next_row();
|
||||
void decode_next_row();
|
||||
void make_huff_table(int index, huff_tables *pH);
|
||||
void check_quant_tables();
|
||||
void check_huff_tables();
|
||||
void calc_mcu_block_order();
|
||||
int init_scan();
|
||||
void init_frame();
|
||||
void process_restart();
|
||||
void decode_scan(pDecode_block_func decode_block_func);
|
||||
void init_progressive();
|
||||
void init_sequential();
|
||||
void decode_start();
|
||||
void decode_init(jpeg_decoder_stream * pStream);
|
||||
void H2V2Convert();
|
||||
void H2V1Convert();
|
||||
void H1V2Convert();
|
||||
void H1V1Convert();
|
||||
void gray_convert();
|
||||
void expanded_convert();
|
||||
void find_eoi();
|
||||
inline uint get_char();
|
||||
inline uint get_char(bool *pPadding_flag);
|
||||
inline void stuff_char(uint8 q);
|
||||
inline uint8 get_octet();
|
||||
inline uint get_bits(int num_bits);
|
||||
inline uint get_bits_no_markers(int numbits);
|
||||
inline int huff_decode(huff_tables *pH);
|
||||
inline int huff_decode(huff_tables *pH, int& extrabits);
|
||||
static inline uint8 clamp(int i);
|
||||
static void decode_block_dc_first(jpeg_decoder *pD, int component_id, int block_x, int block_y);
|
||||
static void decode_block_dc_refine(jpeg_decoder *pD, int component_id, int block_x, int block_y);
|
||||
static void decode_block_ac_first(jpeg_decoder *pD, int component_id, int block_x, int block_y);
|
||||
static void decode_block_ac_refine(jpeg_decoder *pD, int component_id, int block_x, int block_y);
|
||||
};
|
||||
|
||||
} // namespace jpgd
|
||||
|
||||
#endif // JPEG_DECODER_H
|
||||
53
libs/jpeg/jpge.cbp
Normal file
53
libs/jpeg/jpge.cbp
Normal file
@@ -0,0 +1,53 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||
<CodeBlocks_project_file>
|
||||
<FileVersion major="1" minor="6" />
|
||||
<Project>
|
||||
<Option title="jpge" />
|
||||
<Option pch_mode="2" />
|
||||
<Option compiler="gcc" />
|
||||
<Build>
|
||||
<Target title="Debug">
|
||||
<Option output="bin_mingw\jpgeD" prefix_auto="1" extension_auto="1" />
|
||||
<Option working_dir="bin" />
|
||||
<Option object_output="obj\Debug\" />
|
||||
<Option type="1" />
|
||||
<Option compiler="gcc" />
|
||||
<Option parameters="-x z_kodim07.png" />
|
||||
<Compiler>
|
||||
<Add option="-Wall" />
|
||||
<Add option="-g" />
|
||||
</Compiler>
|
||||
</Target>
|
||||
<Target title="Release">
|
||||
<Option output="bin_mingw\jpge" prefix_auto="1" extension_auto="1" />
|
||||
<Option working_dir="bin" />
|
||||
<Option object_output="obj\Release\" />
|
||||
<Option type="1" />
|
||||
<Option compiler="gcc" />
|
||||
<Option parameters="-x z_kodim07.png" />
|
||||
<Compiler>
|
||||
<Add option="-O2" />
|
||||
<Add option="-Wall" />
|
||||
</Compiler>
|
||||
<Linker>
|
||||
<Add option="-s" />
|
||||
</Linker>
|
||||
</Target>
|
||||
</Build>
|
||||
<Compiler>
|
||||
<Add option="-Wall" />
|
||||
<Add option="-fexceptions" />
|
||||
</Compiler>
|
||||
<Unit filename="jpgd.cpp" />
|
||||
<Unit filename="jpgd.h" />
|
||||
<Unit filename="jpge.cpp" />
|
||||
<Unit filename="jpge.h" />
|
||||
<Unit filename="tga2jpg.cpp" />
|
||||
<Unit filename="timer.cpp" />
|
||||
<Unit filename="timer.h" />
|
||||
<Extensions>
|
||||
<code_completion />
|
||||
<debugger />
|
||||
</Extensions>
|
||||
</Project>
|
||||
</CodeBlocks_project_file>
|
||||
1038
libs/jpeg/jpge.cpp
Normal file
1038
libs/jpeg/jpge.cpp
Normal file
File diff suppressed because it is too large
Load Diff
169
libs/jpeg/jpge.h
Normal file
169
libs/jpeg/jpge.h
Normal file
@@ -0,0 +1,169 @@
|
||||
// jpge.h - C++ class for JPEG compression.
|
||||
// Public domain, Rich Geldreich <richgel99@gmail.com>
|
||||
// Alex Evans: Added RGBA support, linear memory allocator.
|
||||
#ifndef JPEG_ENCODER_H
|
||||
#define JPEG_ENCODER_H
|
||||
|
||||
namespace jpge
|
||||
{
|
||||
typedef unsigned char uint8;
|
||||
typedef signed short int16;
|
||||
typedef signed int int32;
|
||||
typedef unsigned short uint16;
|
||||
typedef unsigned int uint32;
|
||||
typedef unsigned int uint;
|
||||
|
||||
// JPEG chroma subsampling factors. Y_ONLY (grayscale images) and H2V2 (color images) are the most common.
|
||||
enum subsampling_t { Y_ONLY = 0, H1V1 = 1, H2V1 = 2, H2V2 = 3 };
|
||||
|
||||
// JPEG compression parameters structure.
|
||||
struct params
|
||||
{
|
||||
inline params() : m_quality(85), m_subsampling(H2V2), m_no_chroma_discrim_flag(false), m_two_pass_flag(false) { }
|
||||
|
||||
inline bool check() const
|
||||
{
|
||||
if ((m_quality < 1) || (m_quality > 100)) return false;
|
||||
if ((uint)m_subsampling > (uint)H2V2) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Quality: 1-100, higher is better. Typical values are around 50-95.
|
||||
int m_quality;
|
||||
|
||||
// m_subsampling:
|
||||
// 0 = Y (grayscale) only
|
||||
// 1 = YCbCr, no subsampling (H1V1, YCbCr 1x1x1, 3 blocks per MCU)
|
||||
// 2 = YCbCr, H2V1 subsampling (YCbCr 2x1x1, 4 blocks per MCU)
|
||||
// 3 = YCbCr, H2V2 subsampling (YCbCr 4x1x1, 6 blocks per MCU-- very common)
|
||||
subsampling_t m_subsampling;
|
||||
|
||||
// Disables CbCr discrimination - only intended for testing.
|
||||
// If true, the Y quantization table is also used for the CbCr channels.
|
||||
bool m_no_chroma_discrim_flag;
|
||||
|
||||
bool m_two_pass_flag;
|
||||
};
|
||||
|
||||
// Writes JPEG image to a file.
|
||||
// num_channels must be 1 (Y) or 3 (RGB), image pitch must be width*num_channels.
|
||||
bool compress_image_to_jpeg_file(const char *pFilename, int width, int height, int num_channels, const uint8 *pImage_data, const params &comp_params = params());
|
||||
|
||||
// Writes JPEG image to memory buffer.
|
||||
// On entry, buf_size is the size of the output buffer pointed at by pBuf, which should be at least ~1024 bytes.
|
||||
// If return value is true, buf_size will be set to the size of the compressed data.
|
||||
bool compress_image_to_jpeg_file_in_memory(void *pBuf, int &buf_size, int width, int height, int num_channels, const uint8 *pImage_data, const params &comp_params = params());
|
||||
|
||||
// Output stream abstract class - used by the jpeg_encoder class to write to the output stream.
|
||||
// put_buf() is generally called with len==JPGE_OUT_BUF_SIZE bytes, but for headers it'll be called with smaller amounts.
|
||||
class output_stream
|
||||
{
|
||||
public:
|
||||
virtual ~output_stream() { };
|
||||
virtual bool put_buf(const void* Pbuf, int len) = 0;
|
||||
template<class T> inline bool put_obj(const T& obj) { return put_buf(&obj, sizeof(T)); }
|
||||
};
|
||||
|
||||
// Lower level jpeg_encoder class - useful if more control is needed than the above helper functions.
|
||||
class jpeg_encoder
|
||||
{
|
||||
public:
|
||||
jpeg_encoder();
|
||||
~jpeg_encoder();
|
||||
|
||||
// Initializes the compressor.
|
||||
// pStream: The stream object to use for writing compressed data.
|
||||
// params - Compression parameters structure, defined above.
|
||||
// width, height - Image dimensions.
|
||||
// channels - May be 1, or 3. 1 indicates grayscale, 3 indicates RGB source data.
|
||||
// Returns false on out of memory or if a stream write fails.
|
||||
bool init(output_stream *pStream, int width, int height, int src_channels, const params &comp_params = params());
|
||||
|
||||
const params &get_params() const { return m_params; }
|
||||
|
||||
// Deinitializes the compressor, freeing any allocated memory. May be called at any time.
|
||||
void deinit();
|
||||
|
||||
uint get_total_passes() const { return m_params.m_two_pass_flag ? 2 : 1; }
|
||||
inline uint get_cur_pass() { return m_pass_num; }
|
||||
|
||||
// Call this method with each source scanline.
|
||||
// width * src_channels bytes per scanline is expected (RGB or Y format).
|
||||
// You must call with NULL after all scanlines are processed to finish compression.
|
||||
// Returns false on out of memory or if a stream write fails.
|
||||
bool process_scanline(const void* pScanline);
|
||||
|
||||
private:
|
||||
jpeg_encoder(const jpeg_encoder &);
|
||||
jpeg_encoder &operator =(const jpeg_encoder &);
|
||||
|
||||
typedef int32 sample_array_t;
|
||||
|
||||
output_stream *m_pStream;
|
||||
params m_params;
|
||||
uint8 m_num_components;
|
||||
uint8 m_comp_h_samp[3], m_comp_v_samp[3];
|
||||
int m_image_x, m_image_y, m_image_bpp, m_image_bpl;
|
||||
int m_image_x_mcu, m_image_y_mcu;
|
||||
int m_image_bpl_xlt, m_image_bpl_mcu;
|
||||
int m_mcus_per_row;
|
||||
int m_mcu_x, m_mcu_y;
|
||||
uint8 *m_mcu_lines[16];
|
||||
uint8 m_mcu_y_ofs;
|
||||
sample_array_t m_sample_array[64];
|
||||
int16 m_coefficient_array[64];
|
||||
int32 m_quantization_tables[2][64];
|
||||
uint m_huff_codes[4][256];
|
||||
uint8 m_huff_code_sizes[4][256];
|
||||
uint8 m_huff_bits[4][17];
|
||||
uint8 m_huff_val[4][256];
|
||||
uint32 m_huff_count[4][256];
|
||||
int m_last_dc_val[3];
|
||||
enum { JPGE_OUT_BUF_SIZE = 2048 };
|
||||
uint8 m_out_buf[JPGE_OUT_BUF_SIZE];
|
||||
uint8 *m_pOut_buf;
|
||||
uint m_out_buf_left;
|
||||
uint32 m_bit_buffer;
|
||||
uint m_bits_in;
|
||||
uint8 m_pass_num;
|
||||
bool m_all_stream_writes_succeeded;
|
||||
|
||||
void optimize_huffman_table(int table_num, int table_len);
|
||||
void emit_byte(uint8 i);
|
||||
void emit_word(uint i);
|
||||
void emit_marker(int marker);
|
||||
void emit_jfif_app0();
|
||||
void emit_dqt();
|
||||
void emit_sof();
|
||||
void emit_dht(uint8 *bits, uint8 *val, int index, bool ac_flag);
|
||||
void emit_dhts();
|
||||
void emit_sos();
|
||||
void emit_markers();
|
||||
void compute_huffman_table(uint *codes, uint8 *code_sizes, uint8 *bits, uint8 *val);
|
||||
void compute_quant_table(int32 *dst, int16 *src);
|
||||
void adjust_quant_table(int32 *dst, int32 *src);
|
||||
void first_pass_init();
|
||||
bool second_pass_init();
|
||||
bool jpg_open(int p_x_res, int p_y_res, int src_channels);
|
||||
void load_block_8_8_grey(int x);
|
||||
void load_block_8_8(int x, int y, int c);
|
||||
void load_block_16_8(int x, int c);
|
||||
void load_block_16_8_8(int x, int c);
|
||||
void load_quantized_coefficients(int component_num);
|
||||
void flush_output_buffer();
|
||||
void put_bits(uint bits, uint len);
|
||||
void code_coefficients_pass_one(int component_num);
|
||||
void code_coefficients_pass_two(int component_num);
|
||||
void code_block(int component_num);
|
||||
void process_mcu_row();
|
||||
bool terminate_pass_one();
|
||||
bool terminate_pass_two();
|
||||
bool process_end_of_image();
|
||||
void load_mcu(const void* src);
|
||||
void clear();
|
||||
void init();
|
||||
};
|
||||
|
||||
} // namespace jpge
|
||||
|
||||
#endif // JPEG_ENCODER
|
||||
26
libs/jpeg/jpge.sln
Normal file
26
libs/jpeg/jpge.sln
Normal file
@@ -0,0 +1,26 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 10.00
|
||||
# Visual Studio 2008
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "jpge", "jpge.vcproj", "{DE273522-92D8-4B60-95C7-C3AEE10A303E}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Win32 = Debug|Win32
|
||||
Debug|x64 = Debug|x64
|
||||
Release|Win32 = Release|Win32
|
||||
Release|x64 = Release|x64
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{DE273522-92D8-4B60-95C7-C3AEE10A303E}.Debug|Win32.ActiveCfg = Debug|Win32
|
||||
{DE273522-92D8-4B60-95C7-C3AEE10A303E}.Debug|Win32.Build.0 = Debug|Win32
|
||||
{DE273522-92D8-4B60-95C7-C3AEE10A303E}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{DE273522-92D8-4B60-95C7-C3AEE10A303E}.Debug|x64.Build.0 = Debug|x64
|
||||
{DE273522-92D8-4B60-95C7-C3AEE10A303E}.Release|Win32.ActiveCfg = Release|Win32
|
||||
{DE273522-92D8-4B60-95C7-C3AEE10A303E}.Release|Win32.Build.0 = Release|Win32
|
||||
{DE273522-92D8-4B60-95C7-C3AEE10A303E}.Release|x64.ActiveCfg = Release|x64
|
||||
{DE273522-92D8-4B60-95C7-C3AEE10A303E}.Release|x64.Build.0 = Release|x64
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
376
libs/jpeg/jpge.vcproj
Normal file
376
libs/jpeg/jpge.vcproj
Normal file
@@ -0,0 +1,376 @@
|
||||
<?xml version="1.0" encoding="Windows-1252"?>
|
||||
<VisualStudioProject
|
||||
ProjectType="Visual C++"
|
||||
Version="9.00"
|
||||
Name="jpge"
|
||||
ProjectGUID="{DE273522-92D8-4B60-95C7-C3AEE10A303E}"
|
||||
RootNamespace="jpge"
|
||||
Keyword="Win32Proj"
|
||||
TargetFrameworkVersion="196613"
|
||||
>
|
||||
<Platforms>
|
||||
<Platform
|
||||
Name="Win32"
|
||||
/>
|
||||
<Platform
|
||||
Name="x64"
|
||||
/>
|
||||
</Platforms>
|
||||
<ToolFiles>
|
||||
</ToolFiles>
|
||||
<Configurations>
|
||||
<Configuration
|
||||
Name="Debug|Win32"
|
||||
OutputDirectory="$(SolutionDir)bin"
|
||||
IntermediateDirectory="$(ConfigurationName)"
|
||||
ConfigurationType="1"
|
||||
CharacterSet="1"
|
||||
>
|
||||
<Tool
|
||||
Name="VCPreBuildEventTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCCustomBuildTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCXMLDataGeneratorTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCWebServiceProxyGeneratorTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCMIDLTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCCLCompilerTool"
|
||||
Optimization="0"
|
||||
PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS"
|
||||
MinimalRebuild="true"
|
||||
BasicRuntimeChecks="3"
|
||||
RuntimeLibrary="1"
|
||||
UsePrecompiledHeader="0"
|
||||
WarningLevel="4"
|
||||
DebugInformationFormat="4"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCManagedResourceCompilerTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCResourceCompilerTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCPreLinkEventTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCLinkerTool"
|
||||
OutputFile="$(OutDir)\$(ProjectName)D.exe"
|
||||
LinkIncremental="2"
|
||||
GenerateDebugInformation="true"
|
||||
SubSystem="1"
|
||||
TargetMachine="1"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCALinkTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCManifestTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCXDCMakeTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCBscMakeTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCFxCopTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCAppVerifierTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCPostBuildEventTool"
|
||||
/>
|
||||
</Configuration>
|
||||
<Configuration
|
||||
Name="Debug|x64"
|
||||
OutputDirectory="$(SolutionDir)bin"
|
||||
IntermediateDirectory="$(PlatformName)\$(ConfigurationName)"
|
||||
ConfigurationType="1"
|
||||
CharacterSet="1"
|
||||
>
|
||||
<Tool
|
||||
Name="VCPreBuildEventTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCCustomBuildTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCXMLDataGeneratorTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCWebServiceProxyGeneratorTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCMIDLTool"
|
||||
TargetEnvironment="3"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCCLCompilerTool"
|
||||
Optimization="0"
|
||||
PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS"
|
||||
MinimalRebuild="true"
|
||||
BasicRuntimeChecks="3"
|
||||
RuntimeLibrary="1"
|
||||
UsePrecompiledHeader="0"
|
||||
WarningLevel="4"
|
||||
DebugInformationFormat="3"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCManagedResourceCompilerTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCResourceCompilerTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCPreLinkEventTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCLinkerTool"
|
||||
OutputFile="$(OutDir)\$(ProjectName)_x64D.exe"
|
||||
LinkIncremental="2"
|
||||
GenerateDebugInformation="true"
|
||||
SubSystem="1"
|
||||
TargetMachine="17"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCALinkTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCManifestTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCXDCMakeTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCBscMakeTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCFxCopTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCAppVerifierTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCPostBuildEventTool"
|
||||
/>
|
||||
</Configuration>
|
||||
<Configuration
|
||||
Name="Release|Win32"
|
||||
OutputDirectory="$(SolutionDir)bin"
|
||||
IntermediateDirectory="$(ConfigurationName)"
|
||||
ConfigurationType="1"
|
||||
CharacterSet="1"
|
||||
WholeProgramOptimization="1"
|
||||
>
|
||||
<Tool
|
||||
Name="VCPreBuildEventTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCCustomBuildTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCXMLDataGeneratorTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCWebServiceProxyGeneratorTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCMIDLTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCCLCompilerTool"
|
||||
Optimization="3"
|
||||
InlineFunctionExpansion="2"
|
||||
EnableIntrinsicFunctions="true"
|
||||
FavorSizeOrSpeed="1"
|
||||
OmitFramePointers="true"
|
||||
PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS"
|
||||
RuntimeLibrary="0"
|
||||
EnableFunctionLevelLinking="true"
|
||||
UsePrecompiledHeader="0"
|
||||
WarningLevel="4"
|
||||
DebugInformationFormat="3"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCManagedResourceCompilerTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCResourceCompilerTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCPreLinkEventTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCLinkerTool"
|
||||
LinkIncremental="1"
|
||||
GenerateDebugInformation="true"
|
||||
SubSystem="1"
|
||||
OptimizeReferences="2"
|
||||
EnableCOMDATFolding="2"
|
||||
TargetMachine="1"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCALinkTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCManifestTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCXDCMakeTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCBscMakeTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCFxCopTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCAppVerifierTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCPostBuildEventTool"
|
||||
/>
|
||||
</Configuration>
|
||||
<Configuration
|
||||
Name="Release|x64"
|
||||
OutputDirectory="$(SolutionDir)bin"
|
||||
IntermediateDirectory="$(PlatformName)\$(ConfigurationName)"
|
||||
ConfigurationType="1"
|
||||
CharacterSet="1"
|
||||
WholeProgramOptimization="1"
|
||||
>
|
||||
<Tool
|
||||
Name="VCPreBuildEventTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCCustomBuildTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCXMLDataGeneratorTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCWebServiceProxyGeneratorTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCMIDLTool"
|
||||
TargetEnvironment="3"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCCLCompilerTool"
|
||||
Optimization="3"
|
||||
InlineFunctionExpansion="2"
|
||||
EnableIntrinsicFunctions="true"
|
||||
FavorSizeOrSpeed="1"
|
||||
OmitFramePointers="true"
|
||||
PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS"
|
||||
RuntimeLibrary="0"
|
||||
EnableFunctionLevelLinking="true"
|
||||
UsePrecompiledHeader="0"
|
||||
WarningLevel="4"
|
||||
DebugInformationFormat="3"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCManagedResourceCompilerTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCResourceCompilerTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCPreLinkEventTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCLinkerTool"
|
||||
OutputFile="$(OutDir)\$(ProjectName)_x64.exe"
|
||||
LinkIncremental="1"
|
||||
GenerateDebugInformation="true"
|
||||
SubSystem="1"
|
||||
OptimizeReferences="2"
|
||||
EnableCOMDATFolding="2"
|
||||
TargetMachine="17"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCALinkTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCManifestTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCXDCMakeTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCBscMakeTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCFxCopTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCAppVerifierTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCPostBuildEventTool"
|
||||
/>
|
||||
</Configuration>
|
||||
</Configurations>
|
||||
<References>
|
||||
</References>
|
||||
<Files>
|
||||
<Filter
|
||||
Name="Source Files"
|
||||
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
|
||||
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
|
||||
>
|
||||
<File
|
||||
RelativePath=".\jpgd.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\jpge.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\tga2jpg.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\timer.cpp"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
<Filter
|
||||
Name="Header Files"
|
||||
Filter="h;hpp;hxx;hm;inl;inc;xsd"
|
||||
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
|
||||
>
|
||||
<File
|
||||
RelativePath=".\jpgd.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\jpge.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\timer.h"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
<Filter
|
||||
Name="Resource Files"
|
||||
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
|
||||
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
|
||||
>
|
||||
</Filter>
|
||||
</Files>
|
||||
<Globals>
|
||||
</Globals>
|
||||
</VisualStudioProject>
|
||||
6
libs/jpeg/jpge.workspace
Normal file
6
libs/jpeg/jpge.workspace
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||
<CodeBlocks_workspace_file>
|
||||
<Workspace title="Workspace">
|
||||
<Project filename="jpge.cbp" active="1" />
|
||||
</Workspace>
|
||||
</CodeBlocks_workspace_file>
|
||||
5055
libs/jpeg/stb_image.c
Normal file
5055
libs/jpeg/stb_image.c
Normal file
File diff suppressed because it is too large
Load Diff
533
libs/jpeg/tga2jpg.cpp
Normal file
533
libs/jpeg/tga2jpg.cpp
Normal file
@@ -0,0 +1,533 @@
|
||||
// tga2jpg.cpp - jpge/jpgd example command line app.
|
||||
// Public domain, Rich Geldreich <richgel99@gmail.com>
|
||||
// Last updated May. 19, 2012
|
||||
|
||||
// Note: jpge.cpp/h and jpgd.cpp/h are completely standalone, i.e. they do not have any dependencies to each other.
|
||||
#include "jpge.h"
|
||||
#include "jpgd.h"
|
||||
#include "stb_image.c"
|
||||
#include "timer.h"
|
||||
#include <ctype.h>
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#define strcasecmp _stricmp
|
||||
#else
|
||||
#define strcpy_s(d, c, s) strcpy(d, s)
|
||||
#endif
|
||||
|
||||
static int print_usage()
|
||||
{
|
||||
printf("Usage: jpge [options] <source_file> <dest_file> <quality_factor>\n");
|
||||
printf("\nRequired parameters (must follow options):\n");
|
||||
printf("source_file: Source image file, in any format stb_image.c supports.\n");
|
||||
printf("dest_file: Destination JPEG file.\n");
|
||||
printf("quality_factor: 1-100, higher=better (only needed in compression mode)\n");
|
||||
printf("\nDefault mode compresses source_file to dest_file. Alternate modes:\n");
|
||||
printf("-x: Exhaustive compression test (only needs source_file)\n");
|
||||
printf("-d: Test jpgd.h. source_file must be JPEG, and dest_file must be .TGA\n");
|
||||
printf("\nOptions supported in all modes:\n");
|
||||
printf("-glogfilename.txt: Append output to log file\n");
|
||||
printf("\nOptions supported in compression mode (the default):\n");
|
||||
printf("-o: Enable optimized Huffman tables (slower, but smaller files)\n");
|
||||
printf("-luma: Output Y-only image\n");
|
||||
printf("-h1v1, -h2v1, -h2v2: Chroma subsampling (default is either Y-only or H2V2)\n");
|
||||
printf("-m: Test mem to mem compression (instead of mem to file)\n");
|
||||
printf("-wfilename.tga: Write decompressed image to filename.tga\n");
|
||||
printf("-s: Use stb_image.c to decompress JPEG image, instead of jpgd.cpp\n");
|
||||
printf("\nExample usages:\n");
|
||||
printf("Test compression: jpge orig.png comp.jpg 90\n");
|
||||
printf("Test decompression: jpge -d comp.jpg uncomp.tga\n");
|
||||
printf("Exhaustively test compressor: jpge -x orig.png\n");
|
||||
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
static char s_log_filename[256];
|
||||
|
||||
static void log_printf(const char *pMsg, ...)
|
||||
{
|
||||
va_list args;
|
||||
|
||||
va_start(args, pMsg);
|
||||
char buf[2048];
|
||||
vsnprintf(buf, sizeof(buf) - 1, pMsg, args);
|
||||
buf[sizeof(buf) - 1] = '\0';
|
||||
va_end(args);
|
||||
|
||||
printf("%s", buf);
|
||||
|
||||
if (s_log_filename[0])
|
||||
{
|
||||
FILE *pFile = fopen(s_log_filename, "a+");
|
||||
if (pFile)
|
||||
{
|
||||
fprintf(pFile, "%s", buf);
|
||||
fclose(pFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static uint get_file_size(const char *pFilename)
|
||||
{
|
||||
FILE *pFile = fopen(pFilename, "rb");
|
||||
if (!pFile) return 0;
|
||||
fseek(pFile, 0, SEEK_END);
|
||||
uint file_size = ftell(pFile);
|
||||
fclose(pFile);
|
||||
return file_size;
|
||||
}
|
||||
|
||||
struct image_compare_results
|
||||
{
|
||||
image_compare_results() { memset(this, 0, sizeof(*this)); }
|
||||
|
||||
double max_err;
|
||||
double mean;
|
||||
double mean_squared;
|
||||
double root_mean_squared;
|
||||
double peak_snr;
|
||||
};
|
||||
|
||||
static void get_pixel(int* pDst, const uint8 *pSrc, bool luma_only, int num_comps)
|
||||
{
|
||||
int r, g, b;
|
||||
if (num_comps == 1)
|
||||
{
|
||||
r = g = b = pSrc[0];
|
||||
}
|
||||
else if (luma_only)
|
||||
{
|
||||
const int YR = 19595, YG = 38470, YB = 7471;
|
||||
r = g = b = (pSrc[0] * YR + pSrc[1] * YG + pSrc[2] * YB + 32768) / 65536;
|
||||
}
|
||||
else
|
||||
{
|
||||
r = pSrc[0]; g = pSrc[1]; b = pSrc[2];
|
||||
}
|
||||
pDst[0] = r; pDst[1] = g; pDst[2] = b;
|
||||
}
|
||||
|
||||
// Compute image error metrics.
|
||||
static void image_compare(image_compare_results &results, int width, int height, const uint8 *pComp_image, int comp_image_comps, const uint8 *pUncomp_image_data, int uncomp_comps, bool luma_only)
|
||||
{
|
||||
double hist[256];
|
||||
memset(hist, 0, sizeof(hist));
|
||||
|
||||
const uint first_channel = 0, num_channels = 3;
|
||||
for (int y = 0; y < height; y++)
|
||||
{
|
||||
for (int x = 0; x < width; x++)
|
||||
{
|
||||
int a[3]; get_pixel(a, pComp_image + (y * width + x) * comp_image_comps, luma_only, comp_image_comps);
|
||||
int b[3]; get_pixel(b, pUncomp_image_data + (y * width + x) * uncomp_comps, luma_only, uncomp_comps);
|
||||
for (uint c = 0; c < num_channels; c++)
|
||||
hist[labs(a[first_channel + c] - b[first_channel + c])]++;
|
||||
}
|
||||
}
|
||||
|
||||
results.max_err = 0;
|
||||
double sum = 0.0f, sum2 = 0.0f;
|
||||
for (uint i = 0; i < 256; i++)
|
||||
{
|
||||
if (!hist[i])
|
||||
continue;
|
||||
if (i > results.max_err)
|
||||
results.max_err = i;
|
||||
double x = i * hist[i];
|
||||
sum += x;
|
||||
sum2 += i * x;
|
||||
}
|
||||
|
||||
// See http://bmrc.berkeley.edu/courseware/cs294/fall97/assignment/psnr.html
|
||||
double total_values = width * height;
|
||||
|
||||
results.mean = sum / total_values;
|
||||
results.mean_squared = sum2 / total_values;
|
||||
|
||||
results.root_mean_squared = sqrt(results.mean_squared);
|
||||
|
||||
if (!results.root_mean_squared)
|
||||
results.peak_snr = 1e+10f;
|
||||
else
|
||||
results.peak_snr = log10(255.0f / results.root_mean_squared) * 20.0f;
|
||||
}
|
||||
|
||||
// Simple exhaustive test. Tries compressing/decompressing image using all supported quality, subsampling, and Huffman optimization settings.
|
||||
static int exhausive_compression_test(const char *pSrc_filename, bool use_jpgd)
|
||||
{
|
||||
int status = EXIT_SUCCESS;
|
||||
|
||||
// Load the source image.
|
||||
const int req_comps = 3; // request RGB image
|
||||
int width = 0, height = 0, actual_comps = 0;
|
||||
uint8 *pImage_data = stbi_load(pSrc_filename, &width, &height, &actual_comps, req_comps);
|
||||
if (!pImage_data)
|
||||
{
|
||||
log_printf("Failed loading file \"%s\"!\n", pSrc_filename);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
log_printf("Source file: \"%s\" Image resolution: %ix%i Actual comps: %i\n", pSrc_filename, width, height, actual_comps);
|
||||
|
||||
int orig_buf_size = width * height * 3; // allocate a buffer that's hopefully big enough (this is way overkill for jpeg)
|
||||
if (orig_buf_size < 1024) orig_buf_size = 1024;
|
||||
void *pBuf = malloc(orig_buf_size);
|
||||
|
||||
uint8 *pUncomp_image_data = NULL;
|
||||
|
||||
double max_err = 0;
|
||||
double lowest_psnr = 9e+9;
|
||||
double threshold_psnr = 9e+9;
|
||||
double threshold_max_err = 0.0f;
|
||||
|
||||
image_compare_results prev_results;
|
||||
|
||||
for (uint quality_factor = 1; quality_factor <= 100; quality_factor++)
|
||||
{
|
||||
for (uint subsampling = 0; subsampling <= jpge::H2V2; subsampling++)
|
||||
{
|
||||
for (uint optimize_huffman_tables = 0; optimize_huffman_tables <= 1; optimize_huffman_tables++)
|
||||
{
|
||||
// Fill in the compression parameter structure.
|
||||
jpge::params params;
|
||||
params.m_quality = quality_factor;
|
||||
params.m_subsampling = static_cast<jpge::subsampling_t>(subsampling);
|
||||
params.m_two_pass_flag = (optimize_huffman_tables != 0);
|
||||
|
||||
int comp_size = orig_buf_size;
|
||||
if (!jpge::compress_image_to_jpeg_file_in_memory(pBuf, comp_size, width, height, req_comps, pImage_data, params))
|
||||
{
|
||||
status = EXIT_FAILURE;
|
||||
goto failure;
|
||||
}
|
||||
|
||||
int uncomp_width = 0, uncomp_height = 0, uncomp_actual_comps = 0, uncomp_req_comps = 3;
|
||||
free(pUncomp_image_data);
|
||||
if (use_jpgd)
|
||||
pUncomp_image_data = jpgd::decompress_jpeg_image_from_memory((const stbi_uc*)pBuf, comp_size, &uncomp_width, &uncomp_height, &uncomp_actual_comps, uncomp_req_comps);
|
||||
else
|
||||
pUncomp_image_data = stbi_load_from_memory((const stbi_uc*)pBuf, comp_size, &uncomp_width, &uncomp_height, &uncomp_actual_comps, uncomp_req_comps);
|
||||
if (!pUncomp_image_data)
|
||||
{
|
||||
status = EXIT_FAILURE;
|
||||
goto failure;
|
||||
}
|
||||
|
||||
if ((uncomp_width != width) || (uncomp_height != height))
|
||||
{
|
||||
status = EXIT_FAILURE;
|
||||
goto failure;
|
||||
}
|
||||
|
||||
image_compare_results results;
|
||||
image_compare(results, width, height, pImage_data, req_comps, pUncomp_image_data, uncomp_req_comps, (params.m_subsampling == jpge::Y_ONLY) || (actual_comps == 1) || (uncomp_actual_comps == 1));
|
||||
//log_printf("Q: %3u, S: %u, O: %u, CompSize: %7u, Error Max: %3.3f, Mean: %3.3f, Mean^2: %5.3f, RMSE: %3.3f, PSNR: %3.3f\n", quality_factor, subsampling, optimize_huffman_tables, comp_size, results.max_err, results.mean, results.mean_squared, results.root_mean_squared, results.peak_snr);
|
||||
log_printf("%3u, %u, %u, %7u, %3.3f, %3.3f, %5.3f, %3.3f, %3.3f\n", quality_factor, subsampling, optimize_huffman_tables, comp_size, results.max_err, results.mean, results.mean_squared, results.root_mean_squared, results.peak_snr);
|
||||
if (results.max_err > max_err) max_err = results.max_err;
|
||||
if (results.peak_snr < lowest_psnr) lowest_psnr = results.peak_snr;
|
||||
|
||||
if (quality_factor == 1)
|
||||
{
|
||||
if (results.peak_snr < threshold_psnr)
|
||||
threshold_psnr = results.peak_snr;
|
||||
if (results.max_err > threshold_max_err)
|
||||
threshold_max_err = results.max_err;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Couple empirically determined tests - worked OK on my test data set.
|
||||
if ((results.peak_snr < (threshold_psnr - 3.0f)) || (results.peak_snr < 6.0f))
|
||||
{
|
||||
status = EXIT_FAILURE;
|
||||
goto failure;
|
||||
}
|
||||
if (optimize_huffman_tables)
|
||||
{
|
||||
if ((prev_results.max_err != results.max_err) || (prev_results.peak_snr != results.peak_snr))
|
||||
{
|
||||
status = EXIT_FAILURE;
|
||||
goto failure;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
prev_results = results;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log_printf("Max error: %f Lowest PSNR: %f\n", max_err, lowest_psnr);
|
||||
|
||||
failure:
|
||||
free(pImage_data);
|
||||
free(pBuf);
|
||||
free(pUncomp_image_data);
|
||||
|
||||
log_printf((status == EXIT_SUCCESS) ? "Success.\n" : "Exhaustive test failed!\n");
|
||||
return status;
|
||||
}
|
||||
|
||||
// Test JPEG file decompression using jpgd.h
|
||||
static int test_jpgd(const char *pSrc_filename, const char *pDst_filename)
|
||||
{
|
||||
// Load the source JPEG image.
|
||||
const int req_comps = 3; // request RGB image
|
||||
int width = 0, height = 0, actual_comps = 0;
|
||||
|
||||
timer tm;
|
||||
tm.start();
|
||||
|
||||
uint8 *pImage_data = jpgd::decompress_jpeg_image_from_file(pSrc_filename, &width, &height, &actual_comps, req_comps);
|
||||
|
||||
tm.stop();
|
||||
|
||||
if (!pImage_data)
|
||||
{
|
||||
log_printf("Failed loading JPEG file \"%s\"!\n", pSrc_filename);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
log_printf("Source JPEG file: \"%s\", image resolution: %ix%i, actual comps: %i\n", pSrc_filename, width, height, actual_comps);
|
||||
|
||||
log_printf("Decompression time: %3.3fms\n", tm.get_elapsed_ms());
|
||||
|
||||
if (!stbi_write_tga(pDst_filename, width, height, req_comps, pImage_data))
|
||||
{
|
||||
log_printf("Failed writing image to file \"%s\"!\n", pDst_filename);
|
||||
free(pImage_data);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
log_printf("Wrote decompressed image to TGA file \"%s\"\n", pDst_filename);
|
||||
|
||||
log_printf("Success.\n");
|
||||
|
||||
free(pImage_data);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int main(int arg_c, char* ppArgs[])
|
||||
{
|
||||
printf("jpge/jpgd example app\n");
|
||||
|
||||
// Parse command line.
|
||||
bool run_exhausive_test = false;
|
||||
bool test_memory_compression = false;
|
||||
bool optimize_huffman_tables = false;
|
||||
int subsampling = -1;
|
||||
char output_filename[256] = "";
|
||||
bool use_jpgd = true;
|
||||
bool test_jpgd_decompression = false;
|
||||
|
||||
int arg_index = 1;
|
||||
while ((arg_index < arg_c) && (ppArgs[arg_index][0] == '-'))
|
||||
{
|
||||
switch (tolower(ppArgs[arg_index][1]))
|
||||
{
|
||||
case 'd':
|
||||
test_jpgd_decompression = true;
|
||||
break;
|
||||
case 'g':
|
||||
strcpy_s(s_log_filename, sizeof(s_log_filename), &ppArgs[arg_index][2]);
|
||||
break;
|
||||
case 'x':
|
||||
run_exhausive_test = true;
|
||||
break;
|
||||
case 'm':
|
||||
test_memory_compression = true;
|
||||
break;
|
||||
case 'o':
|
||||
optimize_huffman_tables = true;
|
||||
break;
|
||||
case 'l':
|
||||
if (strcasecmp(&ppArgs[arg_index][1], "luma") == 0)
|
||||
subsampling = jpge::Y_ONLY;
|
||||
else
|
||||
{
|
||||
log_printf("Unrecognized option: %s\n", ppArgs[arg_index]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
break;
|
||||
case 'h':
|
||||
if (strcasecmp(&ppArgs[arg_index][1], "h1v1") == 0)
|
||||
subsampling = jpge::H1V1;
|
||||
else if (strcasecmp(&ppArgs[arg_index][1], "h2v1") == 0)
|
||||
subsampling = jpge::H2V1;
|
||||
else if (strcasecmp(&ppArgs[arg_index][1], "h2v2") == 0)
|
||||
subsampling = jpge::H2V2;
|
||||
else
|
||||
{
|
||||
log_printf("Unrecognized subsampling: %s\n", ppArgs[arg_index]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
break;
|
||||
case 'w':
|
||||
{
|
||||
strcpy_s(output_filename, sizeof(output_filename), &ppArgs[arg_index][2]);
|
||||
break;
|
||||
}
|
||||
case 's':
|
||||
{
|
||||
use_jpgd = false;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
log_printf("Unrecognized option: %s\n", ppArgs[arg_index]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
arg_index++;
|
||||
}
|
||||
|
||||
if (run_exhausive_test)
|
||||
{
|
||||
if ((arg_c - arg_index) < 1)
|
||||
{
|
||||
log_printf("Not enough parameters (expected source file)\n");
|
||||
return print_usage();
|
||||
}
|
||||
|
||||
const char* pSrc_filename = ppArgs[arg_index++];
|
||||
return exhausive_compression_test(pSrc_filename, use_jpgd);
|
||||
}
|
||||
else if (test_jpgd_decompression)
|
||||
{
|
||||
if ((arg_c - arg_index) < 2)
|
||||
{
|
||||
log_printf("Not enough parameters (expected source and destination files)\n");
|
||||
return print_usage();
|
||||
}
|
||||
|
||||
const char* pSrc_filename = ppArgs[arg_index++];
|
||||
const char* pDst_filename = ppArgs[arg_index++];
|
||||
return test_jpgd(pSrc_filename, pDst_filename);
|
||||
}
|
||||
|
||||
// Test jpge
|
||||
if ((arg_c - arg_index) < 3)
|
||||
{
|
||||
log_printf("Not enough parameters (expected source file, dest file, quality factor to follow options)\n");
|
||||
return print_usage();
|
||||
}
|
||||
|
||||
const char* pSrc_filename = ppArgs[arg_index++];
|
||||
const char* pDst_filename = ppArgs[arg_index++];
|
||||
|
||||
int quality_factor = atoi(ppArgs[arg_index++]);
|
||||
if ((quality_factor < 1) || (quality_factor > 100))
|
||||
{
|
||||
log_printf("Quality factor must range from 1-100!\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
// Load the source image.
|
||||
const int req_comps = 3; // request RGB image
|
||||
int width = 0, height = 0, actual_comps = 0;
|
||||
uint8 *pImage_data = stbi_load(pSrc_filename, &width, &height, &actual_comps, req_comps);
|
||||
if (!pImage_data)
|
||||
{
|
||||
log_printf("Failed loading file \"%s\"!\n", pSrc_filename);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
log_printf("Source file: \"%s\", image resolution: %ix%i, actual comps: %i\n", pSrc_filename, width, height, actual_comps);
|
||||
|
||||
// Fill in the compression parameter structure.
|
||||
jpge::params params;
|
||||
params.m_quality = quality_factor;
|
||||
params.m_subsampling = (subsampling < 0) ? ((actual_comps == 1) ? jpge::Y_ONLY : jpge::H2V2) : static_cast<jpge::subsampling_t>(subsampling);
|
||||
params.m_two_pass_flag = optimize_huffman_tables;
|
||||
|
||||
log_printf("Writing JPEG image to file: %s\n", pDst_filename);
|
||||
|
||||
timer tm;
|
||||
|
||||
// Now create the JPEG file.
|
||||
if (test_memory_compression)
|
||||
{
|
||||
int buf_size = width * height * 3; // allocate a buffer that's hopefully big enough (this is way overkill for jpeg)
|
||||
if (buf_size < 1024) buf_size = 1024;
|
||||
void *pBuf = malloc(buf_size);
|
||||
|
||||
tm.start();
|
||||
if (!jpge::compress_image_to_jpeg_file_in_memory(pBuf, buf_size, width, height, req_comps, pImage_data, params))
|
||||
{
|
||||
log_printf("Failed creating JPEG data!\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
tm.stop();
|
||||
|
||||
FILE *pFile = fopen(pDst_filename, "wb");
|
||||
if (!pFile)
|
||||
{
|
||||
log_printf("Failed creating file \"%s\"!\n", pDst_filename);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (fwrite(pBuf, buf_size, 1, pFile) != 1)
|
||||
{
|
||||
log_printf("Failed writing to output file!\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (fclose(pFile) == EOF)
|
||||
{
|
||||
log_printf("Failed writing to output file!\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
tm.start();
|
||||
|
||||
if (!jpge::compress_image_to_jpeg_file(pDst_filename, width, height, req_comps, pImage_data, params))
|
||||
{
|
||||
log_printf("Failed writing to output file!\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
tm.stop();
|
||||
}
|
||||
|
||||
double total_comp_time = tm.get_elapsed_ms();
|
||||
|
||||
const uint comp_file_size = get_file_size(pDst_filename);
|
||||
const uint total_pixels = width * height;
|
||||
log_printf("Compressed file size: %u, bits/pixel: %3.3f\n", comp_file_size, (comp_file_size * 8.0f) / total_pixels);
|
||||
|
||||
// Now try loading the JPEG file using jpgd or stbi_image's JPEG decompressor.
|
||||
int uncomp_width = 0, uncomp_height = 0, uncomp_actual_comps = 0, uncomp_req_comps = 3;
|
||||
|
||||
tm.start();
|
||||
uint8 *pUncomp_image_data;
|
||||
if (use_jpgd)
|
||||
pUncomp_image_data = jpgd::decompress_jpeg_image_from_file(pDst_filename, &uncomp_width, &uncomp_height, &uncomp_actual_comps, uncomp_req_comps);
|
||||
else
|
||||
pUncomp_image_data = stbi_load(pDst_filename, &uncomp_width, &uncomp_height, &uncomp_actual_comps, uncomp_req_comps);
|
||||
|
||||
double total_uncomp_time = tm.get_elapsed_ms();
|
||||
|
||||
if (!pUncomp_image_data)
|
||||
{
|
||||
log_printf("Failed loading compressed image file \"%s\"!\n", pDst_filename);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
log_printf("Compression time: %3.3fms, Decompression time: %3.3fms\n", total_comp_time, total_uncomp_time);
|
||||
|
||||
// Write uncompressed image.
|
||||
if (output_filename[0])
|
||||
stbi_write_tga(output_filename, uncomp_width, uncomp_height, uncomp_req_comps, pUncomp_image_data);
|
||||
|
||||
if ((uncomp_width != width) || (uncomp_height != height))
|
||||
{
|
||||
log_printf("Loaded JPEG file has a different resolution than the original file!\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
// Diff the original and compressed images.
|
||||
image_compare_results results;
|
||||
image_compare(results, width, height, pImage_data, req_comps, pUncomp_image_data, uncomp_req_comps, (params.m_subsampling == jpge::Y_ONLY) || (actual_comps == 1) || (uncomp_actual_comps == 1));
|
||||
log_printf("Error Max: %f, Mean: %f, Mean^2: %f, RMSE: %f, PSNR: %f\n", results.max_err, results.mean, results.mean_squared, results.root_mean_squared, results.peak_snr);
|
||||
|
||||
log_printf("Success.\n");
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
152
libs/jpeg/timer.cpp
Normal file
152
libs/jpeg/timer.cpp
Normal file
@@ -0,0 +1,152 @@
|
||||
// File: timer.cpp - Simple high-precision timer class. Supports Win32, X360, and POSIX/Linux
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "timer.h"
|
||||
|
||||
#if defined(WIN32)
|
||||
#include <windows.h>
|
||||
#elif defined(_XBOX)
|
||||
#include <xtl.h>
|
||||
#endif
|
||||
|
||||
unsigned long long timer::g_init_ticks;
|
||||
unsigned long long timer::g_freq;
|
||||
double timer::g_inv_freq;
|
||||
|
||||
#if defined(WIN32) || defined(_XBOX)
|
||||
inline void query_counter(timer_ticks *pTicks)
|
||||
{
|
||||
QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER*>(pTicks));
|
||||
}
|
||||
inline void query_counter_frequency(timer_ticks *pTicks)
|
||||
{
|
||||
QueryPerformanceFrequency(reinterpret_cast<LARGE_INTEGER*>(pTicks));
|
||||
}
|
||||
#elif defined(__GNUC__)
|
||||
#include <sys/timex.h>
|
||||
inline void query_counter(timer_ticks *pTicks)
|
||||
{
|
||||
struct timeval cur_time;
|
||||
gettimeofday(&cur_time, NULL);
|
||||
*pTicks = static_cast<unsigned long long>(cur_time.tv_sec)*1000000ULL + static_cast<unsigned long long>(cur_time.tv_usec);
|
||||
}
|
||||
inline void query_counter_frequency(timer_ticks *pTicks)
|
||||
{
|
||||
*pTicks = 1000000;
|
||||
}
|
||||
#endif
|
||||
|
||||
timer::timer() :
|
||||
m_start_time(0),
|
||||
m_stop_time(0),
|
||||
m_started(false),
|
||||
m_stopped(false)
|
||||
{
|
||||
if (!g_inv_freq)
|
||||
init();
|
||||
}
|
||||
|
||||
timer::timer(timer_ticks start_ticks)
|
||||
{
|
||||
if (!g_inv_freq)
|
||||
init();
|
||||
|
||||
m_start_time = start_ticks;
|
||||
|
||||
m_started = true;
|
||||
m_stopped = false;
|
||||
}
|
||||
|
||||
void timer::start(timer_ticks start_ticks)
|
||||
{
|
||||
m_start_time = start_ticks;
|
||||
|
||||
m_started = true;
|
||||
m_stopped = false;
|
||||
}
|
||||
|
||||
void timer::start()
|
||||
{
|
||||
query_counter(&m_start_time);
|
||||
|
||||
m_started = true;
|
||||
m_stopped = false;
|
||||
}
|
||||
|
||||
void timer::stop()
|
||||
{
|
||||
assert(m_started);
|
||||
|
||||
query_counter(&m_stop_time);
|
||||
|
||||
m_stopped = true;
|
||||
}
|
||||
|
||||
double timer::get_elapsed_secs() const
|
||||
{
|
||||
assert(m_started);
|
||||
if (!m_started)
|
||||
return 0;
|
||||
|
||||
timer_ticks stop_time = m_stop_time;
|
||||
if (!m_stopped)
|
||||
query_counter(&stop_time);
|
||||
|
||||
timer_ticks delta = stop_time - m_start_time;
|
||||
return delta * g_inv_freq;
|
||||
}
|
||||
|
||||
timer_ticks timer::get_elapsed_us() const
|
||||
{
|
||||
assert(m_started);
|
||||
if (!m_started)
|
||||
return 0;
|
||||
|
||||
timer_ticks stop_time = m_stop_time;
|
||||
if (!m_stopped)
|
||||
query_counter(&stop_time);
|
||||
|
||||
timer_ticks delta = stop_time - m_start_time;
|
||||
return (delta * 1000000ULL + (g_freq >> 1U)) / g_freq;
|
||||
}
|
||||
|
||||
void timer::init()
|
||||
{
|
||||
if (!g_inv_freq)
|
||||
{
|
||||
query_counter_frequency(&g_freq);
|
||||
g_inv_freq = 1.0f / g_freq;
|
||||
|
||||
query_counter(&g_init_ticks);
|
||||
}
|
||||
}
|
||||
|
||||
timer_ticks timer::get_init_ticks()
|
||||
{
|
||||
if (!g_inv_freq)
|
||||
init();
|
||||
|
||||
return g_init_ticks;
|
||||
}
|
||||
|
||||
timer_ticks timer::get_ticks()
|
||||
{
|
||||
if (!g_inv_freq)
|
||||
init();
|
||||
|
||||
timer_ticks ticks;
|
||||
query_counter(&ticks);
|
||||
return ticks - g_init_ticks;
|
||||
}
|
||||
|
||||
double timer::ticks_to_secs(timer_ticks ticks)
|
||||
{
|
||||
if (!g_inv_freq)
|
||||
init();
|
||||
|
||||
return ticks * g_inv_freq;
|
||||
}
|
||||
|
||||
40
libs/jpeg/timer.h
Normal file
40
libs/jpeg/timer.h
Normal file
@@ -0,0 +1,40 @@
|
||||
// File: timer.h
|
||||
#pragma once
|
||||
|
||||
typedef unsigned long long timer_ticks;
|
||||
|
||||
class timer
|
||||
{
|
||||
public:
|
||||
timer();
|
||||
timer(timer_ticks start_ticks);
|
||||
|
||||
void start();
|
||||
void start(timer_ticks start_ticks);
|
||||
|
||||
void stop();
|
||||
|
||||
double get_elapsed_secs() const;
|
||||
inline double get_elapsed_ms() const { return get_elapsed_secs() * 1000.0f; }
|
||||
timer_ticks get_elapsed_us() const;
|
||||
|
||||
static void init();
|
||||
static inline timer_ticks get_ticks_per_sec() { return g_freq; }
|
||||
static timer_ticks get_init_ticks();
|
||||
static timer_ticks get_ticks();
|
||||
static double ticks_to_secs(timer_ticks ticks);
|
||||
static inline double ticks_to_ms(timer_ticks ticks) { return ticks_to_secs(ticks) * 1000.0f; }
|
||||
static inline double get_secs() { return ticks_to_secs(get_ticks()); }
|
||||
static inline double get_ms() { return ticks_to_ms(get_ticks()); }
|
||||
|
||||
private:
|
||||
static timer_ticks g_init_ticks;
|
||||
static timer_ticks g_freq;
|
||||
static double g_inv_freq;
|
||||
|
||||
timer_ticks m_start_time;
|
||||
timer_ticks m_stop_time;
|
||||
|
||||
bool m_started : 1;
|
||||
bool m_stopped : 1;
|
||||
};
|
||||
Reference in New Issue
Block a user