Files
LCUI/src/graph.c
T

1217 lines
30 KiB
C

/* ***************************************************************************
* graph.c -- LCUI base graphics processing module.
*
* Copyright (C) 2012-2017 by Liu Chao <lc-soft@live.cn>
*
* This file is part of the LCUI project, and may only be used, modified, and
* distributed under the terms of the GPLv2.
*
* (GPLv2 is abbreviation of GNU General Public License Version 2)
*
* By continuing to use, modify, or distribute this file you indicate that you
* have read the license and understand and accept it fully.
*
* The LCUI project is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GPL v2 for more details.
*
* You should have received a copy of the GPLv2 along with this file. It is
* usually in the LICENSE.TXT file, If not, see <http://www.gnu.org/licenses/>.
* ***************************************************************************/
/* ****************************************************************************
* graph.c -- LCUI 的基础图形处理模块
*
* 版权所有 (C) 2012-2017 归属于 刘超 <lc-soft@live.cn>
*
* 这个文件是LCUI项目的一部分,并且只可以根据GPLv2许可协议来使用、更改和发布。
*
* (GPLv2 是 GNU通用公共许可证第二版 的英文缩写)
*
* 继续使用、修改或发布本文件,表明您已经阅读并完全理解和接受这个许可协议。
*
* LCUI 项目是基于使用目的而加以散布的,但不负任何担保责任,甚至没有适销性或特
* 定用途的隐含担保,详情请参照GPLv2许可协议。
*
* 您应已收到附随于本文件的GPLv2许可协议的副本,它通常在LICENSE.TXT文件中,如果
* 没有,请查看:<http://www.gnu.org/licenses/>.
* ***************************************************************************/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <LCUI_Build.h>
#include <LCUI/LCUI.h>
#include <LCUI/graph.h>
void Graph_PrintInfo( LCUI_Graph *graph )
{
LOG( "address:%p\n", graph );
if( !graph ) {
return;
}
LOG( "width:%d, ", graph->w );
LOG( "height:%d, ", graph->h );
LOG( "opacity:%.2f, ", graph->opacity );
LOG( "%s\n", graph->color_type == COLOR_TYPE_ARGB ? "RGBA" : "RGB" );
if( graph->quote.is_valid ) {
LOG( "graph src:" );
Graph_PrintInfo( Graph_GetQuote( graph ) );
}
}
void Graph_Init( LCUI_Graph *graph )
{
graph->quote.is_valid = FALSE;
graph->quote.source = NULL;
graph->quote.top = 0;
graph->quote.left = 0;
graph->palette = NULL;
graph->color_type = COLOR_TYPE_RGB;
graph->bytes = NULL;
graph->opacity = 1.0;
graph->mem_size = 0;
graph->width = 0;
graph->height = 0;
graph->bytes_per_pixel = 3;
graph->bytes_per_row = 0;
}
LCUI_Graph *Graph_New( void )
{
LCUI_Graph *g = (LCUI_Graph*)malloc( sizeof( LCUI_Graph ) );
Graph_Init( g );
return g;
}
LCUI_Color RGB( uchar_t r, uchar_t g, uchar_t b )
{
LCUI_Color color;
color.red = r;
color.green = g;
color.blue = b;
color.alpha = 255;
return color;
}
LCUI_Color ARGB( uchar_t a, uchar_t r, uchar_t g, uchar_t b )
{
LCUI_Color color;
color.alpha = a;
color.red = r;
color.green = g;
color.blue = b;
return color;
}
static size_t get_pixel_size( int color_type )
{
switch( color_type ) {
case COLOR_TYPE_INDEX8:
case COLOR_TYPE_GRAY8:
case COLOR_TYPE_RGB323:
case COLOR_TYPE_ARGB2222:
return 1;
case COLOR_TYPE_RGB555:
case COLOR_TYPE_RGB565:
return 2;
case COLOR_TYPE_RGB888:
return 3;
case COLOR_TYPE_ARGB8888:
default:break;
}
return 4;
}
/*----------------------------------- RGB ----------------------------------*/
static void Pixels_ARGBFormatToRGB( const uchar_t *in_pixels,
uchar_t *out_pixels,
size_t pixel_count )
{
const LCUI_ARGB8888 *p_px, *p_end_px;
LCUI_ARGB8888 *p_out_px;
uchar_t *p_out_byte;
p_out_byte = out_pixels;
p_out_px = (LCUI_ARGB8888*)out_pixels;
p_px = (const LCUI_ARGB8888*)in_pixels;
/* 遍历到倒数第二个像素为止 */
p_end_px = p_px + pixel_count - 1;
for( ; p_px < p_end_px; ++p_px ) {
*p_out_px = *p_px;
/* 将后3字节的数据当成4字节(LCUI_ARGB8888)的像素点访问 */
p_out_px = (LCUI_ARGB8888*)(((uchar_t*)p_out_px) + 3);
}
/* 最后一个像素,以逐个字节的形式写数据 */
p_out_byte = (uchar_t*)(p_out_px + 1);
*p_out_byte++ = p_px->blue;
*p_out_byte++ = p_px->green;
*p_out_byte++ = p_px->red;
}
static void Pixels_RGBFormatToARGB( const uchar_t *in_pixels,
uchar_t *out_pixels,
size_t pixel_count )
{
LCUI_ARGB8888 *p_px, *p_end_px;
const uchar_t *p_in_byte;
p_in_byte = in_pixels;
p_px = (LCUI_ARGB8888*)out_pixels;
p_end_px = p_px + pixel_count;
while( p_px < p_end_px ) {
p_px->blue = *p_in_byte++;
p_px->green = *p_in_byte++;
p_px->red = *p_in_byte++;
p_px->alpha = 255;
++p_px;
}
}
void PixelsFormat( const uchar_t *in_pixels, int in_color_type,
uchar_t *out_pixels, int out_color_type,
size_t pixel_count )
{
switch( in_color_type ) {
case COLOR_TYPE_ARGB8888:
if( out_color_type == COLOR_TYPE_ARGB8888 ) {
return;
}
Pixels_ARGBFormatToRGB( in_pixels, out_pixels, pixel_count );
break;
case COLOR_TYPE_RGB888:
if( out_color_type == COLOR_TYPE_RGB888 ) {
return;
}
Pixels_RGBFormatToARGB( in_pixels, out_pixels, pixel_count );
break;
default: break;
}
}
static int Graph_RGBToARGB( LCUI_Graph *graph )
{
int x, y;
LCUI_ARGB *px_des, *px_row_des, *buffer;
uchar_t *byte_row_src, *byte_src;
buffer = (LCUI_ARGB*)malloc( sizeof( LCUI_ARGB )*graph->w*graph->h );
if( !buffer ) {
return -1;
}
px_row_des = buffer;
byte_row_src = graph->bytes;
for( y = 0; y < graph->h; ++y ) {
px_des = px_row_des;
byte_src = byte_row_src;
for( x = 0; x < graph->w; ++x ) {
px_des->b = *byte_src++;
px_des->g = *byte_src++;
px_des->r = *byte_src++;
px_des->a = 255;
++px_des;
}
byte_row_src += graph->bytes_per_row;
px_row_des += graph->w;
}
free( graph->argb );
graph->argb = buffer;
graph->mem_size = sizeof( LCUI_ARGB )*graph->w*graph->h;
graph->color_type = COLOR_TYPE_ARGB8888;
return 0;
}
static int Graph_CutRGB( const LCUI_Graph *graph, LCUI_Rect rect,
LCUI_Graph *buff )
{
int y;
uchar_t *byte_src_row, *byte_des_row;
buff->color_type = graph->color_type;
if( 0 != Graph_Create( buff, rect.width, rect.height ) ) {
return -1;
}
buff->opacity = graph->opacity;
byte_des_row = buff->bytes;
byte_src_row = graph->bytes + rect.y*graph->bytes_per_row;
byte_src_row += rect.x * graph->bytes_per_pixel;
for( y = 0; y < rect.height; ++y ) {
memcpy( byte_des_row, byte_src_row, buff->bytes_per_row );
byte_des_row += buff->bytes_per_row;
byte_src_row += graph->bytes_per_row;
}
return 0;
}
static void Graph_ARGBReplaceRGB( LCUI_Graph *des, LCUI_Rect des_rect,
const LCUI_Graph *src, int src_x, int src_y )
{
int y;
uchar_t *byte_row_des, *byte_row_src;
byte_row_src = src->bytes + src_y * src->bytes_per_row;
byte_row_src += src_x * src->bytes_per_pixel;
byte_row_des = des->bytes + des_rect.y * des->bytes_per_row;
byte_row_des += des_rect.x * des->bytes_per_pixel;
for( y = 0; y < des_rect.height; ++y ) {
/* 将前景图当前行像素转换成ARGB格式,并直接覆盖至背景图上 */
PixelsFormat( byte_row_src, src->color_type,
byte_row_des, des->color_type, des_rect.width );
byte_row_src += src->bytes_per_row;
byte_row_des += des->bytes_per_row;
}
}
static void Graph_RGBReplaceRGB( LCUI_Graph *des, LCUI_Rect des_rect,
const LCUI_Graph *src, int src_x, int src_y )
{
int y, row_size;
uchar_t *byte_row_des, *byte_row_src;
byte_row_src = src->bytes + src_y * src->bytes_per_row;
byte_row_src += src_x * src->bytes_per_pixel;
row_size = 3 * des_rect.width;
byte_row_des = des->bytes + (des_rect.y * des->w + des_rect.x) * 3;
for( y = 0; y < des_rect.height; ++y ) {
memcpy( byte_row_des, byte_row_src, row_size );
byte_row_src += src->bytes_per_row;
byte_row_des += des->bytes_per_row;
}
}
#define Graph_MixRGB Graph_ReplaceRGB
static int Graph_HorizFlipRGB( const LCUI_Graph *graph, LCUI_Graph *buff )
{
int x, y, n;
LCUI_Rect rect;
uchar_t *byte_src, *byte_des;
if( !Graph_IsValid( graph ) ) {
return -1;
}
Graph_GetValidRect( graph, &rect );
graph = Graph_GetQuote( graph );
buff->color_type = graph->color_type;
if( 0 != Graph_Create( buff, rect.width, rect.height ) ) {
return -2;
}
for( y = 0; y < rect.height; ++y ) {
byte_des = buff->bytes + y*buff->w * 3;
n = ((rect.y + y)*graph->w + rect.x + rect.width - 1) * 3;
byte_src = buff->bytes + n;
for( x = 0; x < rect.width; ++x ) {
*byte_des++ = *byte_src--;
*byte_des++ = *byte_src--;
*byte_des++ = *byte_src--;
}
}
return 0;
}
static int Graph_VertiFlipRGB( const LCUI_Graph *graph, LCUI_Graph *buff )
{
int y;
LCUI_Rect rect;
uchar_t *byte_src, *byte_des;
if( !Graph_IsValid( graph ) ) {
return -1;
}
Graph_GetValidRect( graph, &rect );
graph = Graph_GetQuote( graph );
buff->opacity = graph->opacity;
buff->color_type = graph->color_type;
if( 0 != Graph_Create( buff, rect.width, rect.height ) ) {
return -2;
}
/* 引用最后一行像素 */
byte_src = graph->bytes + (rect.y + rect.height - 1)*graph->bytes_per_row;
byte_src += rect.x * graph->bytes_per_pixel;
byte_des = buff->bytes;
/* 交换上下每行像素 */
for( y = 0; y < rect.height; ++y ) {
memcpy( byte_des, byte_src, buff->w * 3 );
byte_src -= graph->bytes_per_row;
byte_des += buff->bytes_per_row;
}
return 0;
}
static int Graph_FillRectRGB( LCUI_Graph *graph, LCUI_Color color, LCUI_Rect rect )
{
int x, y;
LCUI_Graph canvas;
uchar_t *rowbytep, *bytep;
if( !Graph_IsValid( graph ) ) {
return -1;
}
Graph_Quote( &canvas, graph, &rect );
Graph_GetValidRect( &canvas, &rect );
graph = Graph_GetQuote( &canvas );
rowbytep = graph->bytes + rect.y*graph->bytes_per_row;
rowbytep += rect.x*graph->bytes_per_pixel;
for( y = 0; y < rect.height; ++y ) {
bytep = rowbytep;
for( x = 0; x < rect.width; ++x ) {
*bytep++ = color.blue;
*bytep++ = color.green;
*bytep++ = color.red;
}
rowbytep += graph->bytes_per_row;
}
return 0;
}
/*--------------------------------- End RGB --------------------------------*/
/*---------------------------------- ARGB ----------------------------------*/
static int Graph_ARGBToRGB( LCUI_Graph *graph )
{
int x, y;
LCUI_ARGB *px_src, *px_row_src;
uchar_t *buffer, *byte_row_des, *byte_des;
buffer = (uchar_t*)malloc( sizeof( uchar_t )*graph->w*graph->h * 3 );
if( !buffer ) {
return -1;
}
byte_row_des = buffer;
px_row_src = graph->argb;
for( y = 0; y < graph->h; ++y ) {
px_src = px_row_src;
byte_des = byte_row_des;
for( x = 0; x < graph->w; ++x ) {
px_src->b = *byte_des++;
px_src->g = *byte_des++;
px_src->r = *byte_des++;
px_src->a = 255;
++px_src;
}
byte_row_des += graph->bytes_per_row;
px_row_src += graph->w;
}
free( graph->argb );
graph->bytes = buffer;
graph->mem_size = sizeof( uchar_t )*graph->w*graph->h * 3;
graph->color_type = COLOR_TYPE_RGB888;
graph->bytes_per_pixel = 3;
return 0;
}
static int Graph_CutARGB( const LCUI_Graph *graph, LCUI_Rect rect,
LCUI_Graph *buff )
{
int x, y;
LCUI_ARGB *pixel_src, *pixel_des;
buff->color_type = graph->color_type;
if( 0 != Graph_Create( buff, rect.width, rect.height ) ) {
return -1;
}
buff->opacity = graph->opacity;
for( y = 0; y < rect.height; ++y ) {
pixel_des = buff->argb + y*buff->w;
pixel_src = graph->argb;
pixel_src += (rect.y + y)*graph->w + rect.x;
for( x = 0; x < rect.width; ++x ) {
*pixel_des++ = *pixel_src++;
}
}
return 0;
}
static void Graph_ARGBMixARGB( LCUI_Graph *dst, LCUI_Rect des_rect,
const LCUI_Graph *src, int src_x, int src_y )
{
int x, y;
LCUI_ARGB *px_src, *px_dst;
LCUI_ARGB *px_row_src, *px_row_des;
double a, out_a, out_r, out_g, out_b, src_a;
px_row_src = src->argb + src_y*src->width + src_x;
px_row_des = dst->argb + des_rect.y*dst->width + des_rect.x;
if( src->opacity < 1.0 ) {
goto mix_with_opacity;
}
for( y = 0; y < des_rect.height; ++y ) {
px_src = px_row_src;
px_dst = px_row_des;
for( x = 0; x < des_rect.width; ++x ) {
src_a = px_src->a / 255.0;
a = (1.0 - src_a) * px_dst->a / 255.0;
out_r = px_dst->r * a + px_src->r * src_a;
out_g = px_dst->g * a + px_src->g * src_a;
out_b = px_dst->b * a + px_src->b * src_a;
out_a = src_a + a;
if( out_a > 0 ) {
out_r /= out_a;
out_g /= out_a;
out_b /= out_a;
}
px_dst->r = (uchar_t)(out_r + 0.5);
px_dst->g = (uchar_t)(out_g + 0.5);
px_dst->b = (uchar_t)(out_b + 0.5);
px_dst->a = (uchar_t)(255.0 * out_a + 0.5);
++px_src;
++px_dst;
}
px_row_des += dst->w;
px_row_src += src->w;
}
return;
mix_with_opacity:
for( y = 0; y < des_rect.height; ++y ) {
px_src = px_row_src;
px_dst = px_row_des;
for( x = 0; x < des_rect.width; ++x ) {
src_a = px_src->a / 255.0 * src->opacity;
a = (1.0 - src_a) * px_dst->a / 255.0;
out_r = px_dst->r * a + px_src->r * src_a;
out_g = px_dst->g * a + px_src->g * src_a;
out_b = px_dst->b * a + px_src->b * src_a;
out_a = src_a + a;
if( out_a > 0 ) {
out_r /= out_a;
out_g /= out_a;
out_b /= out_a;
}
px_dst->r = (uchar_t)(out_r + 0.5);
px_dst->g = (uchar_t)(out_g + 0.5);
px_dst->b = (uchar_t)(out_b + 0.5);
px_dst->a = (uchar_t)(255.0 * out_a + 0.5);
++px_src;
++px_dst;
}
px_row_des += dst->width;
px_row_src += src->width;
}
}
static void Graph_ARGBMixARGB2( LCUI_Graph *dest, LCUI_Rect des_rect,
const LCUI_Graph *src, int src_x, int src_y )
{
int x, y, a;
LCUI_ARGB *px_src, *px_dest;
LCUI_ARGB *px_row_src, *px_row_des;
px_row_src = src->argb + src_y*src->width + src_x;
px_row_des = dest->argb + des_rect.y*dest->width + des_rect.x;
if( src->opacity < 1.0 ) {
goto mix_with_opacity;
}
for( y = 0; y < des_rect.height; ++y ) {
px_src = px_row_src;
px_dest = px_row_des;
for( x = 0; x < des_rect.width; ++x ) {
PIXEL_BLEND( px_dest, px_src, px_src->a );
++px_src;
++px_dest;
}
px_row_des += dest->width;
px_row_src += src->width;
}
return;
mix_with_opacity:
for( y = 0; y < des_rect.height; ++y ) {
px_src = px_row_src;
px_dest = px_row_des;
for( x = 0; x < des_rect.width; ++x ) {
a = px_src->a * src->opacity;
PIXEL_BLEND( px_dest, px_src, a );
++px_src;
++px_dest;
}
px_row_des += dest->width;
px_row_src += src->width;
}
}
static void Graph_RGBMixARGB( LCUI_Graph *des, LCUI_Rect des_rect,
const LCUI_Graph *src, int src_x, int src_y )
{
int x, y;
uchar_t a;
LCUI_ARGB *px, *px_row;
uchar_t *rowbytep, *bytep;
/* 计算并保存第一行的首个像素的位置 */
px_row = src->argb + src_y*src->w + src_x;
rowbytep = des->bytes + des_rect.y*des->bytes_per_row;
rowbytep += des_rect.x*des->bytes_per_pixel;
if( src->opacity < 1.0 ) {
goto mix_with_opacity;
}
for( y = 0; y < des_rect.height; ++y ) {
px = px_row;
bytep = rowbytep;
for( x = 0; x < des_rect.width; ++x, ++px ) {
*bytep = _ALPHA_BLEND( *bytep, px->b, px->a );
++bytep;
*bytep = _ALPHA_BLEND( *bytep, px->g, px->a );
++bytep;
*bytep = _ALPHA_BLEND( *bytep, px->r, px->a );
++bytep;
}
rowbytep += des->bytes_per_row;
px_row += src->w;
}
return;
mix_with_opacity:
for( y = 0; y < des_rect.height; ++y ) {
px = px_row;
bytep = rowbytep;
for( x = 0; x < des_rect.width; ++x, ++px ) {
a = px->a * src->opacity;
*bytep = _ALPHA_BLEND( *bytep, px->b, a );
++bytep;
*bytep = _ALPHA_BLEND( *bytep, px->g, a );
++bytep;
*bytep = _ALPHA_BLEND( *bytep, px->r, a );
++bytep;
}
rowbytep += des->bytes_per_row;
px_row += src->w;
}
}
static int Graph_ARGBReplaceARGB( LCUI_Graph *des, LCUI_Rect des_rect,
const LCUI_Graph *src, int src_x, int src_y )
{
int x, y, row_size;
LCUI_ARGB *px_row_src, *px_row_des, *px_src, *px_des;
px_row_src = src->argb + src_y*src->w + src_x;
px_row_des = des->argb + des_rect.y*des->w + des_rect.x;
if( src->opacity < 1.0 ) {
for( y = 0; y < des_rect.height; ++y ) {
px_src = px_row_src;
px_des = px_row_des;
for( x = 0; x < des_rect.width; ++x ) {
px_des->b = px_src->b;
px_des->g = px_src->g;
px_des->r = px_src->r;
px_des->a = src->opacity*px_src->a;
}
px_row_src += src->w;
px_row_des += des->w;
}
return 0;
}
row_size = sizeof( LCUI_ARGB )*des_rect.width;
for( y = 0; y < des_rect.height; ++y ) {
memcpy( px_row_des, px_row_src, row_size );
px_row_src += src->w;
px_row_des += des->w;
}
return 0;
}
static int Graph_HorizFlipARGB( const LCUI_Graph *graph, LCUI_Graph *buff )
{
int x, y;
LCUI_Rect rect;
LCUI_ARGB *pixel_src, *pixel_des;
if( !Graph_IsValid( graph ) ) {
return -1;
}
Graph_GetValidRect( graph, &rect );
graph = Graph_GetQuote( graph );
buff->opacity = graph->opacity;
buff->color_type = graph->color_type;
if( 0 != Graph_Create( buff, rect.width, rect.height ) ) {
return -2;
}
for( y = 0; y < rect.height; ++y ) {
pixel_des = buff->argb + y*buff->w;
pixel_src = graph->argb + (rect.y + y)*graph->w;
pixel_src += rect.x + rect.width - 1;
for( x = 0; x < rect.width; ++x ) {
*pixel_des++ = *pixel_src--;
}
}
return 0;
}
static int Graph_VertiFlipARGB( const LCUI_Graph *graph, LCUI_Graph *buff )
{
int y;
LCUI_Rect rect;
uchar_t *byte_src, *byte_des;
if( !Graph_IsValid( graph ) ) {
return -1;
}
Graph_GetValidRect( graph, &rect );
graph = Graph_GetQuote( graph );
buff->opacity = graph->opacity;
buff->color_type = graph->color_type;
if( 0 != Graph_Create( buff, rect.width, rect.height ) ) {
return -2;
}
byte_src = graph->bytes + (rect.y + rect.height - 1)*graph->bytes_per_row;
byte_src += rect.x * graph->bytes_per_pixel;
byte_des = buff->bytes;
for( y = 0; y < rect.height; ++y ) {
memcpy( byte_des, byte_src, buff->bytes_per_row );
byte_src -= graph->bytes_per_row;
byte_des += buff->bytes_per_row;
}
return 0;
}
static int Graph_FillRectARGB( LCUI_Graph *graph, LCUI_Color color,
LCUI_Rect rect, LCUI_BOOL with_alpha )
{
int x, y;
LCUI_Rect rect_src;
LCUI_ARGB *px_p, *px_row_p;
if( !Graph_IsValid( graph ) ) {
return -1;
}
Graph_GetValidRect( graph, &rect_src );
graph = Graph_GetQuote( graph );
px_row_p = graph->argb + (rect_src.y + rect.y)*graph->w;
px_row_p += rect.x + rect_src.x;
if( with_alpha ) {
for( y = 0; y < rect.height; ++y ) {
px_p = px_row_p;
for( x = 0; x < rect.width; ++x ) {
*px_p++ = color;
}
px_row_p += graph->w;
}
} else {
for( y = 0; y < rect.height; ++y ) {
px_p = px_row_p;
for( x = 0; x < rect.width; ++x ) {
color.alpha = px_p->alpha;
*px_p++ = color;
}
px_row_p += graph->w;
}
}
return 0;
}
/*-------------------------------- End ARGB --------------------------------*/
int Graph_SetColorType( LCUI_Graph *graph, int color_type )
{
if( graph->color_type == color_type ) {
return -1;
}
switch( graph->color_type ) {
case COLOR_TYPE_ARGB8888:
switch( color_type ) {
case COLOR_TYPE_RGB888:
return Graph_ARGBToRGB( graph );
default:break;
}
break;
case COLOR_TYPE_RGB888:
switch( color_type ) {
case COLOR_TYPE_ARGB8888:
return Graph_RGBToARGB( graph );
default:break;
}
break;
default:
break;
}
return -2;
}
int Graph_Create( LCUI_Graph *graph, int w, int h )
{
size_t size;
if( w > 10000 || h > 10000 ) {
_DEBUG_MSG( "graph size is too large!" );
abort();
}
if( h <= 0 || w <= 0 ) {
Graph_Free( graph );
return -1;
}
graph->bytes_per_pixel = get_pixel_size( graph->color_type );
graph->bytes_per_row = graph->bytes_per_pixel * w;
size = graph->bytes_per_row * h;
if( Graph_IsValid( graph ) ) {
/* 如果现有图形尺寸大于要创建的图形的尺寸,直接改尺寸即可 */
if( graph->mem_size >= size ) {
memset( graph->bytes, 0, graph->mem_size );
graph->w = w;
graph->h = h;
return 0;
}
Graph_Free( graph );
}
graph->mem_size = size;
graph->bytes = malloc( size );
if( !graph->bytes ) {
graph->w = 0;
graph->h = 0;
return -2;
}
memset( graph->bytes, 0, graph->mem_size );
graph->w = w;
graph->h = h;
return 0;
}
void Graph_Copy( LCUI_Graph *des, const LCUI_Graph *src )
{
const LCUI_Graph *graph;
if( !des || !Graph_IsValid( src ) ) {
return;
}
graph = Graph_GetQuote( src );
des->color_type = graph->color_type;
/* 创建合适尺寸的Graph */
Graph_Create( des, src->w, src->h );
Graph_Replace( des, src, 0, 0 );
des->opacity = src->opacity;
}
void Graph_Free( LCUI_Graph *graph )
{
/* 解除引用 */
if( graph && graph->quote.is_valid ) {
graph->quote.source = NULL;
graph->quote.is_valid = FALSE;
return;
}
if( graph->bytes ) {
free( graph->bytes );
graph->bytes = NULL;
}
graph->w = 0;
graph->h = 0;
graph->mem_size = 0;
}
int Graph_Quote( LCUI_Graph *self, LCUI_Graph *source, const LCUI_Rect *rect )
{
LCUI_Rect quote_rect;
if( !rect ) {
quote_rect.x = 0;
quote_rect.y = 0;
quote_rect.width = source->width;
quote_rect.height = source->height;
} else {
quote_rect = *rect;
}
LCUIRect_ValidateArea( &quote_rect, source->width, source->height );
/* 如果引用源本身已经引用了另一个源 */
if( source->quote.is_valid ) {
quote_rect.x += source->quote.left;
quote_rect.y += source->quote.top;
source = source->quote.source;
}
if( quote_rect.width <= 0 || quote_rect.height <= 0 ) {
self->width = 0;
self->height = 0;
self->opacity = 1.0;
self->quote.left = 0;
self->quote.top = 0;
self->bytes = NULL;
self->quote.source = NULL;
self->quote.is_valid = FALSE;
return -2;
}
self->width = quote_rect.width;
self->height = quote_rect.height;
self->opacity = 1.0;
self->bytes = NULL;
self->mem_size = 0;
self->color_type = source->color_type;
self->bytes_per_pixel = source->bytes_per_pixel;
self->bytes_per_row = source->bytes_per_row;
self->quote.is_valid = TRUE;
self->quote.source = source;
self->quote.left = quote_rect.x;
self->quote.top = quote_rect.y;
return 0;
}
void Graph_GetValidRect( const LCUI_Graph *graph, LCUI_Rect *rect )
{
if( graph->quote.is_valid ) {
rect->x = graph->quote.left;
rect->y = graph->quote.top;
} else {
rect->x = 0;
rect->y = 0;
}
rect->width = graph->width;
rect->height = graph->height;
}
int Graph_SetAlphaBits( LCUI_Graph *graph, uchar_t *a, size_t size )
{
size_t i;
if( size > (size_t)(graph->w*graph->h) ) {
size = (size_t)(graph->w*graph->h);
}
if( graph->color_type != COLOR_TYPE_ARGB ) {
return -2;
}
for( i = 0; i < size; ++i ) {
graph->argb[i].a = a[i];
}
return 0;
}
int Graph_SetRedBits( LCUI_Graph *graph, uchar_t *r, size_t size )
{
size_t i;
uchar_t *pByte;
if( size > (size_t)(graph->w*graph->h) ) {
size = (size_t)(graph->w*graph->h);
}
if( graph->color_type == COLOR_TYPE_ARGB ) {
for( i = 0; i < size; ++i ) {
graph->argb[i].r = r[i];
}
} else {
pByte = graph->bytes + 2;
for( i = 0; i < size; ++i ) {
*pByte = r[i];
pByte += 3;
}
}
return 0;
}
int Graph_SetGreenBits( LCUI_Graph *graph, uchar_t *g, size_t size )
{
size_t i;
uchar_t *pByte;
if( size > (size_t)(graph->w*graph->h) ) {
size = (size_t)(graph->w*graph->h);
}
if( graph->color_type == COLOR_TYPE_ARGB ) {
for( i = 0; i < size; ++i ) {
graph->argb[i].g = g[i];
}
} else {
pByte = graph->bytes + 1;
for( i = 0; i < size; ++i ) {
*pByte = g[i];
pByte += 3;
}
}
return 0;
}
int Graph_SetBlueBits( LCUI_Graph *graph, uchar_t *b, size_t size )
{
size_t i;
uchar_t *pByte;
if( size > (size_t)(graph->w*graph->h) ) {
size = (size_t)(graph->w*graph->h);
}
if( graph->color_type == COLOR_TYPE_ARGB ) {
for( i = 0; i < size; ++i ) {
graph->argb[i].b = b[i];
}
} else {
pByte = graph->bytes;
for( i = 0; i < size; ++i ) {
*pByte = b[i];
pByte += 3;
}
}
return 0;
}
int Graph_Zoom( const LCUI_Graph *graph, LCUI_Graph *buff,
LCUI_BOOL keep_scale, int width, int height )
{
LCUI_Rect rect;
int x, y, src_x, src_y;
double scale_x = 0.0, scale_y = 0.0;
if( !Graph_IsValid( graph ) || (width <= 0 && height <= 0) ) {
return -1;
}
/* 获取引用的有效区域,以及指向引用的对象的指针 */
Graph_GetValidRect( graph, &rect );
graph = Graph_GetQuote( graph );
if( width > 0 ) {
scale_x = 1.0 * rect.width / width;
}
if( height > 0 ) {
scale_y = 1.0 * rect.height / height;
}
if( width <= 0 ) {
scale_x = scale_y;
width = (int)(0.5 + 1.0 * graph->width / scale_x);
}
if( height <= 0 ) {
scale_y = scale_x;
height = (int)(0.5 + 1.0 * graph->height / scale_y);
}
/* 如果保持宽高比 */
if( keep_scale ) {
if( scale_x < scale_y ) {
scale_y = scale_x;
} else {
scale_x = scale_y;
}
}
buff->color_type = graph->color_type;
if( Graph_Create( buff, width, height ) < 0 ) {
return -2;
}
if( graph->color_type == COLOR_TYPE_ARGB ) {
LCUI_ARGB *px_src, *px_des, *px_row_src;
for( y = 0; y < height; ++y ) {
src_y = y * scale_y;
px_row_src = graph->argb;
px_row_src += (src_y + rect.y) * graph->w + rect.x;
px_des = buff->argb + y * width;
for( x = 0; x < width; ++x ) {
src_x = x * scale_x;
px_src = px_row_src + src_x;
*px_des++ = *px_src;
}
}
} else {
uchar_t *byte_src, *byte_des, *byte_row_src;
for( y = 0; y < height; ++y ) {
src_y = y * scale_y;
byte_row_src = graph->bytes;
byte_row_src += (src_y + rect.y) * graph->bytes_per_row;
byte_row_src += rect.x * graph->bytes_per_pixel;
byte_des = buff->bytes + y * buff->bytes_per_row;
for( x = 0; x < width; ++x ) {
src_x = x * scale_x;
src_x *= graph->bytes_per_pixel;
byte_src = byte_row_src + src_x;
*byte_des++ = *byte_src++;
*byte_des++ = *byte_src++;
*byte_des++ = *byte_src;
}
}
}
return 0;
}
int Graph_Cut( const LCUI_Graph *graph, LCUI_Rect rect,
LCUI_Graph *buff )
{
if( !Graph_IsValid( graph ) ) {
return -2;
}
LCUIRect_ValidateArea( &rect, graph->w, graph->h );
if( rect.width <= 0 || rect.height <= 0 ) {
return -3;
}
switch( graph->color_type ) {
case COLOR_TYPE_ARGB8888:
return Graph_CutARGB( graph, rect, buff );
case COLOR_TYPE_RGB888:
return Graph_CutRGB( graph, rect, buff );
default:break;
}
return -4;
}
int Graph_HorizFlip( const LCUI_Graph *graph, LCUI_Graph *buff )
{
switch( graph->color_type ) {
case COLOR_TYPE_RGB888:
return Graph_HorizFlipRGB( graph, buff );
case COLOR_TYPE_ARGB8888:
return Graph_HorizFlipARGB( graph, buff );
default:break;
}
return -1;
}
int Graph_VertiFlip( const LCUI_Graph *graph, LCUI_Graph *buff )
{
switch( graph->color_type ) {
case COLOR_TYPE_RGB888:
return Graph_VertiFlipRGB( graph, buff );
case COLOR_TYPE_ARGB8888:
return Graph_VertiFlipARGB( graph, buff );
default:break;
}
return -1;
}
int Graph_FillRect( LCUI_Graph *graph, LCUI_Color color,
LCUI_Rect *rect, LCUI_BOOL with_alpha )
{
LCUI_Rect rect2;
if( rect ) {
rect2 = *rect;
} else {
rect2.x = rect2.y = 0;
rect2.width = graph->width;
rect2.height = graph->height;
}
switch( graph->color_type ) {
case COLOR_TYPE_RGB888:
return Graph_FillRectRGB( graph, color, rect2 );
case COLOR_TYPE_ARGB8888:
return Graph_FillRectARGB( graph, color, rect2, with_alpha );
default:break;
}
return -1;
}
int Graph_FillAlpha( LCUI_Graph *graph, uchar_t alpha )
{
int x, y;
LCUI_Rect rect;
LCUI_ARGB *pixel, *pixel_row;
Graph_GetValidRect( graph, &rect );
graph = Graph_GetQuote( graph );
if( !Graph_IsValid( graph ) ) {
return -1;
}
if( !Graph_HasAlpha( graph ) ) {
return -2;
}
pixel_row = graph->argb + rect.y*graph->w + rect.x;
for( y = 0; y < rect.height; ++y ) {
pixel = pixel_row;
for( x = 0; x < rect.width; ++x ) {
pixel->alpha = alpha;
++pixel;
}
pixel_row += graph->w;
}
return 0;
}
int Graph_Tile( LCUI_Graph *buff, const LCUI_Graph *graph,
LCUI_BOOL replace, LCUI_BOOL with_alpha )
{
int ret = 0, x, y;
if( !Graph_IsValid( graph ) || !Graph_IsValid( buff ) ) {
return -1;
}
for( y = 0; y < buff->h; y += graph->h ) {
for( x = 0; x < buff->w; x += graph->w ) {
if( replace ) {
ret += Graph_Replace( buff, graph, y, x );
continue;
}
ret += Graph_Mix( buff, graph, y, x, with_alpha );
}
}
return ret;
}
typedef void( *MixerPtr )(LCUI_Graph*, LCUI_Rect, const LCUI_Graph *, int, int);
int Graph_Mix( LCUI_Graph *back, const LCUI_Graph *fore,
int left, int top, LCUI_BOOL with_alpha )
{
LCUI_Graph w_slot;
LCUI_Rect r_rect, w_rect;
MixerPtr mixer = NULL;
if( !Graph_IsValid( back ) || !Graph_IsValid( fore ) ) {
return -1;
}
w_rect.x = left;
w_rect.y = top;
w_rect.width = fore->width;
w_rect.height = fore->height;
LCUIRect_GetCutArea( back->width, back->height, w_rect, &r_rect );
w_rect.x += r_rect.x;
w_rect.y += r_rect.y;
w_rect.width = r_rect.width;
w_rect.height = r_rect.height;
Graph_Quote( &w_slot, back, &w_rect );
/* 获取实际操作区域 */
Graph_GetValidRect( &w_slot, &w_rect );
Graph_GetValidRect( fore, &r_rect );
if( w_rect.width <= 0 || w_rect.height <= 0 ||
r_rect.width <= 0 || r_rect.height <= 0 ) {
return -2;
}
top = r_rect.y;
left = r_rect.x;
/* 获取引用的源图像 */
fore = Graph_GetQuote( fore );
back = Graph_GetQuote( back );
switch( fore->color_type ) {
case COLOR_TYPE_RGB888:
if( back->color_type == COLOR_TYPE_RGB888 ) {
mixer = Graph_RGBReplaceRGB;
} else {
mixer = Graph_ARGBReplaceRGB;
}
break;
case COLOR_TYPE_ARGB8888:
if( back->color_type == COLOR_TYPE_RGB888 ) {
mixer = Graph_RGBMixARGB;
} else {
if( with_alpha ) {
mixer = Graph_ARGBMixARGB;
} else {
mixer = Graph_ARGBMixARGB2;
}
}
default:break;
}
if( mixer ) {
mixer( back, w_rect, fore, left, top );
return 0;
}
return -3;
}
int Graph_Replace( LCUI_Graph *back, const LCUI_Graph *fore, int left, int top )
{
LCUI_Graph write_slot;
LCUI_Rect read_rect, write_rect;
if( !Graph_IsValid( back ) || !Graph_IsValid( fore ) ) {
return -1;
}
write_rect.x = left;
write_rect.y = top;
write_rect.width = fore->width;
write_rect.height = fore->height;
Graph_Quote( &write_slot, back, &write_rect );
Graph_GetValidRect( &write_slot, &write_rect );
Graph_GetValidRect( fore, &read_rect );
if( write_rect.width <= 0 || write_rect.height <= 0 ||
read_rect.width <= 0 || read_rect.height <= 0 ) {
return -2;
}
left = read_rect.x;
top = read_rect.y;
fore = Graph_GetQuote( fore );
back = Graph_GetQuote( back );
switch( fore->color_type ) {
case COLOR_TYPE_RGB888:
Graph_RGBReplaceRGB( back, write_rect, fore, left, top );
break;
case COLOR_TYPE_ARGB8888:
Graph_ARGBReplaceARGB( back, write_rect, fore, left, top );
default:break;
}
return -1;
}