5 Star 1 Fork 1

bian / fbv

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
png.c 6.82 KB
一键复制 编辑 原始数据 按行查看 历史
jqc 提交于 2014-01-09 18:16 . add log to file and stdout
/*
fbv -- simple image viewer for the linux framebuffer
Copyright (C) 2000, 2001, 2003 Mateusz Golicz
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <png.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "fbv.h"
#include "log.h"
#define DEBUG (ALL_DEBUG_SWITCH && 1)
int fh_png_id(char *name)
{
if (DEBUG)
Log(LOG_DEBUG, "[DEBUG] function:%s --- paralist:name=%s\n", __func__, name);
int fd;
char id[4];
char strtime[LEN_TIME] = { '\0' };
if (-1 == (fd = open(name, O_RDONLY))) {
getsystime(strtime);
Log(LOG_WARNING, "[%s WARNING] function:%s --- %s\n", strtime, __func__, strerror(errno));
return (0);
}
read(fd, id, 4);
close(fd);
if (id[1] == 'P' && id[2] == 'N' && id[3] == 'G')
return (1);
getsystime(strtime);
Log(LOG_WARNING, "[%s WARNING] function:%s --- file is not png image\n", strtime, __func__);
return (0);
}
int fh_png_load(char *name, unsigned char *buffer, unsigned char ** alpha, int x, int y)
{
if (DEBUG)
Log(LOG_DEBUG, "[DEBUG] function:%s --- paralist:name=%s,x=%d,y=%d\n", __func__, name, x,
y);
png_bytep rptr[2];
png_structp png_ptr;
png_infop info_ptr;
png_uint_32 width, height;
FILE *fh = NULL;
int i;
int bit_depth, color_type, interlace_type;
int number_passes, pass, trans = 0;
char *rp = NULL;
char *fbptr = NULL;
char strtime[LEN_TIME] = { '\0' };
if (!(fh = fopen(name, "rb"))) {
getsystime(strtime);
Log(LOG_WARNING, "[%s WARNING] function:%s --- %s\n", strtime, __func__, strerror(errno));
return (FH_ERROR_FILE);
}
if (NULL == (png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL))) {
getsystime(strtime);
Log(LOG_WARNING, "[%s WARNING] function:%s --- png file format error!\n", strtime,
__func__);
return (FH_ERROR_FORMAT);
}
if (NULL == (info_ptr = png_create_info_struct(png_ptr))) {
png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
fclose(fh);
getsystime(strtime);
Log(LOG_WARNING, "[%s WARNING] function:%s --- png file format error!\n", strtime,
__func__);
return (FH_ERROR_FORMAT);
}
if (setjmp(png_ptr->jmpbuf)) {
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
if (rp)
free(rp);
fclose(fh);
getsystime(strtime);
Log(LOG_WARNING, "[%s WARNING] function:%s --- png file format error!\n", strtime,
__func__);
return (FH_ERROR_FORMAT);
}
png_init_io(png_ptr, fh);
png_read_info(png_ptr, info_ptr);
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, NULL,
NULL);
if (color_type == PNG_COLOR_TYPE_PALETTE)
png_set_expand(png_ptr);
if (bit_depth < 8)
png_set_packing(png_ptr);
if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb(png_ptr);
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
trans = 1;
png_set_tRNS_to_alpha(png_ptr);
}
if (bit_depth == 16)
png_set_strip_16(png_ptr);
number_passes = png_set_interlace_handling(png_ptr);
png_read_update_info(png_ptr, info_ptr);
if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA || color_type == PNG_COLOR_TYPE_RGB_ALPHA
|| trans) {
unsigned char * alpha_buffer = NULL;
unsigned char * aptr = NULL;
if (NULL == (alpha_buffer = (unsigned char*) malloc(width * height))) {
getsystime(strtime);
Log(LOG_WARNING, "[%s WARNING] function:%s --- alpha buffer malloc failed!\n", strtime,
__func__);
free(rp);
fclose(fh);
return (FH_ERROR_FORMAT);
}
if (NULL == (rp = (char*) malloc(width * 4))) {
getsystime(strtime);
Log(LOG_WARNING, "[%s WARNING] function:%s --- rp malloc failed!\n", strtime, __func__);
free(rp);
fclose(fh);
return (FH_ERROR_FORMAT);
}
rptr[0] = (png_bytep) rp;
*alpha = alpha_buffer;
for (pass = 0; pass < number_passes; pass++) {
fbptr = (char *) buffer;
aptr = alpha_buffer;
for (i = 0; i < height; i++) {
int n;
unsigned char *trp = (unsigned char *) rp;
png_read_rows(png_ptr, rptr, NULL, 1);
for (n = 0; n < width; n++, fbptr += 3, trp += 4) {
memcpy(fbptr, trp, 3);
*(aptr++) = trp[3];
}
}
}
free(rp);
} else {
for (pass = 0; pass < number_passes; pass++) {
fbptr = (char *) buffer;
for (i = 0; i < height; i++, fbptr += width * 3) {
rptr[0] = (png_bytep) fbptr;
png_read_rows(png_ptr, rptr, NULL, 1);
}
}
}
png_read_end(png_ptr, info_ptr);
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
fclose(fh);
return (FH_ERROR_OK);
}
int fh_png_getsize(char *name, int *x, int *y)
{
png_structp png_ptr;
png_infop info_ptr;
png_uint_32 width, height;
FILE *fh = NULL;
int bit_depth, color_type, interlace_type;
char *rp = NULL;
char strtime[LEN_TIME] = { '\0' };
if (!(fh = fopen(name, "rb"))) {
getsystime(strtime);
Log(LOG_WARNING, "[%s WARNING] function:%s --- %s\n", strtime, __func__, strerror(errno));
return (FH_ERROR_FILE);
}
if (NULL == (png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL))) {
getsystime(strtime);
Log(LOG_WARNING, "[%s WARNING] function:%s --- png file format error!\n", strtime,
__func__);
return (FH_ERROR_FORMAT);
}
if (NULL == (info_ptr = png_create_info_struct(png_ptr))) {
png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
fclose(fh);
getsystime(strtime);
Log(LOG_WARNING, "[%s WARNING] function:%s --- png file format error!\n", strtime,
__func__);
return (FH_ERROR_FORMAT);
}
if (setjmp(png_ptr->jmpbuf)) {
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
if (rp)
free(rp);
fclose(fh);
getsystime(strtime);
Log(LOG_WARNING, "[%s WARNING] function:%s --- png file format error!\n", strtime,
__func__);
return (FH_ERROR_FORMAT);
}
png_init_io(png_ptr, fh);
png_read_info(png_ptr, info_ptr);
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, NULL,
NULL);
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
*x = width;
*y = height;
fclose(fh);
if (DEBUG)
Log(LOG_DEBUG, "[DEBUG] function:%s --- paralist:name=%s,x=%d,y=%d\n", __func__, name, *x,
*y);
return (FH_ERROR_OK);
}
C
1
https://gitee.com/2013/fbv.git
git@gitee.com:2013/fbv.git
2013
fbv
fbv
master

搜索帮助