?? libmng_pixels.c
字號:
/* ************************************************************************** */
/* * For conditions of distribution and use, * */
/* * see copyright notice in libmng.h * */
/* ************************************************************************** */
/* * * */
/* * project : libmng * */
/* * file : libmng_pixels.c copyright (c) 2000-2004 G.Juyn * */
/* * version : 1.0.8 * */
/* * * */
/* * purpose : Pixel-row management routines (implementation) * */
/* * * */
/* * author : G.Juyn * */
/* * * */
/* * comment : implementation of the pixel-row management routines * */
/* * * */
/* * the dual alpha-composing for RGBA/BGRA/etc output-canvas' * */
/* * is based on the Note on Compositing chapter of the * */
/* * DOH-3 draft, noted to me by Adam M. Costello * */
/* * * */
/* * changes : 0.5.1 - 05/08/2000 - G.Juyn * */
/* * - changed strict-ANSI stuff * */
/* * 0.5.1 - 05/11/2000 - G.Juyn * */
/* * - added callback error-reporting support * */
/* * 0.5.1 - 05/12/2000 - G.Juyn * */
/* * - changed trace to macro for callback error-reporting * */
/* * * */
/* * 0.5.2 - 05/22/2000 - G.Juyn * */
/* * - added JNG support * */
/* * 0.5.2 - 05/30/2000 - G.Juyn * */
/* * - fixed minor bugs 16-bit pixel-handling * */
/* * - added delta-image row-processing routines * */
/* * 0.5.2 - 06/02/2000 - G.Juyn * */
/* * - fixed endian support (hopefully) * */
/* * 0.5.2 - 06/03/2000 - G.Juyn * */
/* * - fixed makeup for Linux gcc compile * */
/* * 0.5.2 - 06/05/2000 - G.Juyn * */
/* * - implemented app bkgd restore routines * */
/* * - implemented RGBA8, ARGB8, BGRA8 & ABGR8 display routines * */
/* * - added support for RGB8_A8 canvasstyle * */
/* * 0.5.2 - 06/09/2000 - G.Juyn * */
/* * - fixed alpha-handling for alpha canvasstyles * */
/* * * */
/* * 0.5.3 - 06/16/2000 - G.Juyn * */
/* * - changed progressive-display processing * */
/* * 0.5.3 - 06/17/2000 - G.Juyn * */
/* * - changed to support delta-images * */
/* * - optimized some store_xxx routines * */
/* * 0.5.3 - 06/20/2000 - G.Juyn * */
/* * - fixed nasty bug with embedded PNG after delta-image * */
/* * 0.5.3 - 06/24/2000 - G.Juyn * */
/* * - fixed problem with 16-bit GA format * */
/* * 0.5.3 - 06/25/2000 - G.Juyn * */
/* * - fixed problem with cheap transparency for 4-bit gray * */
/* * - fixed display_xxxx routines for interlaced images * */
/* * 0.5.3 - 06/28/2000 - G.Juyn * */
/* * - fixed compiler-warning for non-initialized iB variable * */
/* * * */
/* * 0.9.1 - 07/05/2000 - G.Juyn * */
/* * - fixed mandatory BACK color to be opaque * */
/* * * */
/* * 0.9.2 - 07/31/2000 - G.Juyn * */
/* * - B110547 - fixed bug in interlace code * */
/* * 0.9.2 - 08/05/2000 - G.Juyn * */
/* * - changed file-prefixes * */
/* * * */
/* * 0.9.3 - 08/20/2000 - G.Juyn * */
/* * - fixed app-supplied background restore * */
/* * 0.9.3 - 08/26/2000 - G.Juyn * */
/* * - added MAGN chunk * */
/* * 0.9.3 - 09/07/2000 - G.Juyn * */
/* * - added support for new filter_types * */
/* * 0.9.3 - 09/30/2000 - G.Juyn * */
/* * - fixed MAGN rounding errors (thanks Matthias!) * */
/* * 0.9.3 - 10/10/2000 - G.Juyn * */
/* * - fixed alpha-blending for RGBA canvasstyle * */
/* * 0.9.3 - 10/11/2000 - G.Juyn * */
/* * - fixed alpha-blending for other alpha-canvasstyles * */
/* * 0.9.3 - 10/16/2000 - G.Juyn * */
/* * - added optional support for bKGD for PNG images * */
/* * - added support for JDAA * */
/* * 0.9.3 - 10/17/2000 - G.Juyn * */
/* * - fixed support for bKGD * */
/* * 0.9.3 - 10/19/2000 - G.Juyn * */
/* * - implemented delayed delta-processing * */
/* * 0.9.3 - 10/28/2000 - G.Juyn * */
/* * - fixed tRNS processing for gray-image < 8-bits * */
/* * * */
/* * 0.9.4 - 12/16/2000 - G.Juyn * */
/* * - fixed mixup of data- & function-pointers (thanks Dimitri)* */
/* * 0.9.4 - 1/18/2001 - G.Juyn * */
/* * - removed "old" MAGN methods 3 & 4 * */
/* * - added "new" MAGN methods 3, 4 & 5 * */
/* * - removed test filter-methods 1 & 65 * */
/* * * */
/* * 1.0.1 - 04/21/2001 - G.Juyn (code by G.Kelly) * */
/* * - added BGRA8 canvas with premultiplied alpha * */
/* * 1.0.1 - 04/25/2001 - G.Juyn * */
/* * - moved mng_clear_cms to libmng_cms * */
/* * * */
/* * 1.0.2 - 06/25/2001 - G.Juyn * */
/* * - added option to turn off progressive refresh * */
/* * * */
/* * 1.0.4 - 11/04/2001 - G.Juyn * */
/* * - fixed possible compile-problem in cleanup_rowproc * */
/* * 1.0.4 - 06/22/2002 - G.Juyn * */
/* * - B558212 - off by one error * */
/* * - MNG subimage alpha composite wrong for rgba8 images * */
/* * * */
/* * 1.0.5 - 08/07/2002 - G.Juyn * */
/* * - added test-option for PNG filter method 193 (=no filter) * */
/* * 1.0.5 - 08/15/2002 - G.Juyn * */
/* * - completed PROM support * */
/* * - completed delta-image support * */
/* * 1.0.5 - 08/16/2002 - G.Juyn * */
/* * - completed MAGN support (16-bit functions) * */
/* * 1.0.5 - 08/19/2002 - G.Juyn * */
/* * - B597134 - libmng pollutes the linker namespace * */
/* * 1.0.5 - 09/19/2002 - G.Juyn * */
/* * - optimized restore-background for bKGD cases * */
/* * 1.0.5 - 09/20/2002 - G.Juyn * */
/* * - finished support for BACK image & tiling * */
/* * 1.0.5 - 09/22/2002 - G.Juyn * */
/* * - added bgrx8 canvas (filler byte) * */
/* * 1.0.5 - 09/23/2002 - G.Juyn * */
/* * - added compose over/under routines for PAST processing * */
/* * - added flip & tile routines for PAST processing * */
/* * * */
/* * 1.0.6 - 03/09/2003 - G.Juyn * */
/* * - hiding 12-bit JPEG stuff * */
/* * 1.0.6 - 05/11/2003 - Glenn RP * */
/* * - added size-optimization COMPOSE routine usage * */
/* * 1.0.6 - 05/11/2003 - G. Juyn * */
/* * - added conditionals around canvas update routines * */
/* * 1.0.6 - 05/25/2003 - Glenn RP * */
/* * - added size-optimization DIV255B8 routine usage * */
/* * 1.0.6 - 06/09/2003 - G. R-P * */
/* * - added conditionals around 8-bit magn routines * */
/* * 1.0.6 - 07/07/2003 - G. R-P * */
/* * - removed conditionals around 8-bit magn routines * */
/* * - added MNG_NO_16BIT_SUPPORT and MNG_NO_DELTA_PNG * */
/* * conditionals * */
/* * - reversed many loops to use decrementing counter * */
/* * - combined init functions * */
/* * - converted some switches to array references * */
/* * 1.0.6 - 07/29/2003 - G.Juyn * */
/* * - fixed duplicate for-loop * */
/* * 1.0.6 - 07/29/2003 - G.R-P * */
/* * - added SKIPCHUNK conditionals around PAST chunk support * */
/* * - fixed "FOOTPRINT_COMPOSEIV" typo (now "FOOTPRINT_DIV") * */
/* * 1.0.6 - 08/17/2003 - G.R-P * */
/* * - added more conditionals around "promote" functions * */
/* * * */
/* * 1.0.7 - 11/27/2003 - R.A * */
/* * - added CANVAS_RGB565 and CANVAS_BGR565 * */
/* * 1.0.7 - 12/06/2003 - R.A * */
/* * - added CANVAS_RGBA565 and CANVAS_BGRA565 * */
/* * 1.0.7 - 01/25/2004 - J.S * */
/* * - added premultiplied alpha canvas' for RGBA, ARGB, ABGR * */
/* * 1.0.7 - 03/08/2004 - G.R-P * */
/* * - added more conditionals around 16-bit-supporting code * */
/* * 1.0.7 - 03/09/2004 - G.Juyn * */
/* * - fixed bug in promote_g8_g8 with 16bit support off * */
/* * 1.0.7 - 03/09/2004 - G.R-P * */
/* * - more optimizations with 16bit support off * */
/* * 1.0.7 - 03/10/2004 - G.Juyn * */
/* * - fixed some warnings for 16bit optimizations * */
/* * 1.0.7 - 03/21/2004 - G.Juyn * */
/* * - fixed some 64-bit platform compiler warnings * */
/* * * */
/* * 1.0.8 - 06/20/2004 - G.Juyn * */
/* * - some speed optimizations (thanks to John Stiles) * */
/* * 1.0.8 - 08/01/2004 - G.Juyn * */
/* * - added support for 3+byte pixelsize for JPEG's * */
/* * * */
/* ************************************************************************** */
#include "libmng.h"
#include "libmng_data.h"
#include "libmng_error.h"
#include "libmng_trace.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#include "libmng_objects.h"
#include "libmng_object_prc.h"
#include "libmng_memory.h"
#include "libmng_cms.h"
#include "libmng_filter.h"
#include "libmng_pixels.h"
#if defined(__BORLANDC__) && defined(MNG_STRICT_ANSI)
#pragma option -A /* force ANSI-C */
#endif
/* ************************************************************************** */
#ifdef MNG_INCLUDE_DISPLAY_PROCS
/* TODO: magnification & canvas-positioning/-clipping */
/* TODO: major optimization of pixel-loops by using assembler (?) */
/* ************************************************************************** */
/* * * */
/* * Interlace tables * */
/* * * */
/* ************************************************************************** */
MNG_LOCAL mng_uint32 const interlace_row [7] = { 0, 0, 4, 0, 2, 0, 1 };
MNG_LOCAL mng_uint32 const interlace_rowskip [7] = { 8, 8, 8, 4, 4, 2, 2 };
MNG_LOCAL mng_uint32 const interlace_col [7] = { 0, 4, 0, 2, 0, 1, 0 };
MNG_LOCAL mng_uint32 const interlace_colskip [7] = { 8, 8, 4, 4, 2, 2, 1 };
MNG_LOCAL mng_uint32 const interlace_roundoff [7] = { 7, 7, 3, 3, 1, 1, 0 };
MNG_LOCAL mng_uint32 const interlace_divider [7] = { 3, 3, 2, 2, 1, 1, 0 };
/* ************************************************************************** */
/* * * */
/* * Alpha composing macros * */
/* * the code below is slightly modified from the libpng package * */
/* * the original was last optimized by Greg Roelofs & Mark Adler * */
/* * * */
/* ************************************************************************** */
#define MNG_COMPOSE8(RET,FG,ALPHA,BG) { \
mng_uint16 iH = (mng_uint16)((mng_uint16)(FG) * (mng_uint16)(ALPHA) \
+ (mng_uint16)(BG)*(mng_uint16)(255 - \
(mng_uint16)(ALPHA)) + (mng_uint16)128); \
(RET) = (mng_uint8)((iH + (iH >> 8)) >> 8); }
#define MNG_COMPOSE16(RET,FG,ALPHA,BG) { \
mng_uint32 iH = (mng_uint32)((mng_uint32)(FG) * (mng_uint32)(ALPHA) \
+ (mng_uint32)(BG)*(mng_uint32)(65535L - \
(mng_uint32)(ALPHA)) + (mng_uint32)32768L); \
(RET) = (mng_uint16)((iH + (iH >> 16)) >> 16); }
/* ************************************************************************** */
/* * * */
/* * Alpha blending macros * */
/* * this code is based on Adam Costello's "Note on Compositing" from the * */
/* * mng-list which gives the following formula: * */
/* * * */
/* * top pixel = (Rt, Gt, Bt, At) * */
/* * bottom pixel = (Rb, Gb, Bb, Ab) * */
/* * composite pixel = (Rc, Gc, Bc, Ac) * */
/* * * */
/* * all values in the range 0..1 * */
/* * * */
/* * Ac = 1 - (1 - At)(1 - Ab) * */
/* * s = At / Ac * */
/* * t = (1 - At) Ab / Ac * */
/* * Rc = s Rt + t Rb * */
/* * Gc = s Gt + t Gb * */
/* * Bc = s Bt + t Bb * */
/* * * */
/* * (I just hope I coded it correctly in integer arithmetic...) * */
/* * * */
/* ************************************************************************** */
#define MNG_BLEND8(RT, GT, BT, AT, RB, GB, BB, AB, RC, GC, BC, AC) { \
mng_uint32 S, T; \
(AC) = (mng_uint8)((mng_uint32)255 - \
((((mng_uint32)255 - (mng_uint32)(AT)) * \
((mng_uint32)255 - (mng_uint32)(AB)) ) >> 8)); \
S = (mng_uint32)(((mng_uint32)(AT) << 8) / \
(mng_uint32)(AC)); \
T = (mng_uint32)(((mng_uint32)255 - (mng_uint32)(AT)) * \
(mng_uint32)(AB) / (mng_uint32)(AC)); \
(RC) = (mng_uint8)((S * (mng_uint32)(RT) + \
T * (mng_uint32)(RB) + (mng_uint32)127) >> 8); \
(GC) = (mng_uint8)((S * (mng_uint32)(GT) + \
T * (mng_uint32)(GB) + (mng_uint32)127) >> 8); \
(BC) = (mng_uint8)((S * (mng_uint32)(BT) + \
T * (mng_uint32)(BB) + (mng_uint32)127) >> 8); }
#define MNG_BLEND16(RT, GT, BT, AT, RB, GB, BB, AB, RC, GC, BC, AC) { \
mng_uint32 S, T; \
(AC) = (mng_uint16)((mng_uint32)65535 - \
((((mng_uint32)65535 - (mng_uint32)(AT)) * \
((mng_uint32)65535 - (mng_uint32)(AB)) ) >> 16)); \
S = (mng_uint32)(((mng_uint32)(AT) << 16) / \
(mng_uint32)(AC)); \
T = (mng_uint32)(((mng_uint32)65535 - (mng_uint32)(AT)) * \
(mng_uint32)(AB) / (mng_uint32)(AC)); \
(RC) = (mng_uint16)((S * (mng_uint32)(RT) + \
T * (mng_uint32)(RB) + (mng_uint32)32767) >> 16); \
(GC) = (mng_uint16)((S * (mng_uint32)(GT) + \
T * (mng_uint32)(GB) + (mng_uint32)32767) >> 16); \
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -