使用libpng、zlib历程

引言

	在某志的SDK中引用libpng库,过程中遇到了很多问题,记录一下,希望帮助小伙伴们少走点弯路,想引用libpng库的小伙伴们耐心看完,真的可以帮你节省很多时间。

准备环境

	这里我使用的l版本是libpng-1.5.14_与zlib-1.2.11

这两个版本亲自试过可以使用,其他版本没有试过,暂时不提。
下载地址附上:
链接: https://jaist.dl.sourceforge.net/project/libpng/

开始编译

正确安装顺序为zlib--->libpng--->加载进SDK中

这里我们按照解析错误的顺序来进行

① 开始先安装了libpng(下载解压的过程就不再说明了)

libpng历程

② 因为我们要进行第三方库的移植,所以我们需要使用SDK相同的编译链,执行顺序如下:

kws@kws-virtual-machine:/home/work_code/h700/package/png/libpng-1.5.14$ ./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether to enable maintainer-specific portions of Makefiles... no
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking for style of include used by make... GNU
checking dependency style of gcc... gcc3
checking dependency style of gcc... gcc3
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking for a sed that does not truncate output... /bin/sed
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for fgrep... /bin/grep -F
checking how to print strings... printf
checking for ld used by gcc... /usr/bin/ld
checking if the linker (/usr/bin/ld) is GNU ld... yes
checking how to run the C preprocessor... gcc -E
checking for sed... /bin/sed
checking whether ln -s works... yes
checking whether make sets $(MAKE)... (cached) yes
checking for BSD- or MS-compatible name lister (nm)... /usr/bin/nm -B
checking the name lister (/usr/bin/nm -B) interface... BSD nm
checking the maximum length of command line arguments... 1572864
checking whether the shell understands some XSI constructs... yes
checking whether the shell understands "+="... yes
checking how to convert x86_64-unknown-linux-gnu file names to x86_64-unknown-linux-gnu format... func_convert_file_noop
checking how to convert x86_64-unknown-linux-gnu file names to toolchain format... func_convert_file_noop
checking for /usr/bin/ld option to reload object files... -r
checking for objdump... objdump
checking how to recognize dependent libraries... pass_all
checking for dlltool... no
checking how to associate runtime and link libraries... printf %s\n
checking for ar... ar
checking for archiver @FILE support... @
checking for strip... strip
checking for ranlib... ranlib
checking command to parse /usr/bin/nm -B output from gcc object... ok
checking for sysroot... no
checking for mt... mt
checking if mt is a manifest tool... no
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking for dlfcn.h... yes
checking for objdir... .libs
checking if gcc supports -fno-rtti -fno-exceptions... no
checking for gcc option to produce PIC... -fPIC -DPIC
checking if gcc PIC flag -fPIC -DPIC works... yes
checking if gcc static flag -static works... yes
checking if gcc supports -c -o file.o... yes
checking if gcc supports -c -o file.o... (cached) yes
checking whether the gcc linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes
checking whether -lc should be explicitly linked in... no
checking dynamic linker characteristics... GNU/Linux ld.so
checking how to hardcode library paths into programs... immediate
checking whether stripping libraries is possible... yes
checking if libtool supports shared libraries... yes
checking whether to build shared libraries... yes
checking whether to build static libraries... yes
checking for a C preprocessor that does not parse its input... gcc -E
checking for ANSI C header files... (cached) yes
checking malloc.h usability... yes
checking malloc.h presence... yes
checking for malloc.h... yes
checking for stdlib.h... (cached) yes
checking for string.h... (cached) yes
checking for strings.h... (cached) yes
checking for an ANSI C-conforming const... yes
checking for size_t... yes
checking whether struct tm is in sys/time.h or time.h... time.h
checking for C/C++ restrict keyword... __restrict
checking for working strtod... yes
checking for memset... yes
checking for pow... no
checking for pow in -lm... yes
checking for zlibVersion in -lz... yes
checking for feenableexcept in -lm... yes
checking for feenableexcept... yes
checking if using Solaris linker... no
checking if libraries can be versioned... yes
checking for symbol prefix... 
configure: pkgconfig directory is ${libdir}/pkgconfig
configure: creating ./config.status
config.status: creating Makefile
config.status: creating libpng.pc
config.status: creating libpng-config
config.status: creating config.h
config.status: config.h is unchanged
config.status: executing depfiles commands
config.status: executing libtool commands
kws@kws-virtual-machine:/home/work_code/h700/package/png/libpng-1.5.14$

这一步是设置系统变量的值并创建makefile,在进行下一步之前需要修改Makefile中的编译链:
:因为Makefile中需要替换的东西较多且容易漏掉,这里在外部统一配置

kws@kws-virtual-machine:/home/work_code/h700/package/png/libpng-1.5.14$ ./configure --host=arm-openwrt-linux-gnueabi CC=/home/work_code/h700/prebuilt/gcc/linux-x86/arm/toolchain-sunxi-glibc/toolchain/bin/arm-openwrt-linux-gnueabi-gcc AR=/home/work_code/h700/prebuilt/gcc/linux-x86/arm/toolchain-sunxi-glibc/toolchain/bin/arm-openwrt-linux-gnueabi-ar STRIP=/home/work_code/h700/prebuilt/gcc/linux-x86/arm/toolchain-sunxi-glibc/toolchain/bin/arm-openwrt-linux-gnueabi-strip RANLIB=/home/work_code/h700/prebuilt/gcc/linux-x86/arm/toolchain-sunxi-glibc/toolchain/bin/arm-openwrt-linux-gnueabi-ranlib --prefix=$PWD/_install

关于–host相关的注解:
build:自动测试在用平台名称,若无法检测出来则需要指定。
host:若无指定,自动使用build的结果。
   build和host相同时表示本地编译,若不相同则表示交叉编译。
target:表示需要处理的目标平台名称,若无指定使用host相同名称,gcc、binutils等于平台指令相关软件有此参数,多数软件此参数无用处。
综合的例子:
我们需要在x86下编译一个运行于arm上的编译器gcc,这个gcc产生mips平台的目标代码。那么我们的配置方式是:
–build=x86 --host=arm --target=mips
–prefix:最终文件生成的目录,这里我装在了当前目录下的_install文件夹下

③报错:configure: error: zlib not installed

在我们执行完上个步骤之后,发现报错了,这个错误是因为libpng需要依赖zlib库,我们就先暂时转去安装zlib

kws@kws-virtual-machine:/home/work_code/h700/package/zlib/zlib-1.2.11$ ./configure --prefix=/path/ZLIB
Checking for shared library support...
Building shared library libz.so.1.2.11 with /home/work_code/h700/prebuilt/gcc/linux-x86/arm/toolchain-sunxi-glibc/toolchain/bin/arm-openwrt-linux-gnueabi-gcc.
Checking for size_t... Yes.
Checking for off64_t... Yes.
Checking for fseeko... Yes.
Checking for strerror... Yes.
Checking for unistd.h... Yes.
Checking for stdarg.h... Yes.
Checking whether to use vs[n]printf() or s[n]printf()... using vs[n]printf().
Checking for vsnprintf() in stdio.h... Yes.
Checking for return value of vsnprintf()... Yes.
Checking for attribute(visibility) support... No.
kws@kws-virtual-machine:/home/work_code/h700/package/zlib/zlib-1.2.11$ 

我们将安装目录放在path/ZLIB下,方便我们等下用
zlib不支持像libpng那样直接配置,所以我们手动修改Makefile文件,如下
在这里插入图片描述
执行
make&&sudo make install
成功之后我们的/path/ZLIB下应该有如下文件:

kws@kws-virtual-machine:/home/work_code/h700/package/zlib/zlib-1.2.11$ ls /path/ZLIB/
include  lib  share
kws@kws-virtual-machine:/home/work_code/h700/package/zlib/zlib-1.2.11$ 

④继续安装libpng

我们继续执行②中的命令,你会发现还是出现一样的错误,我们需要告诉libpng去哪里找zlib库与头文件,使用CPPFLAGS与LDFLAGS,如下:

kws@kws-virtual-machine:/home/work_code/h700/package/png/libpng-1.5.14$ ./configure --host=arm-openwrt-linux-gnueabi CC=/home/work_code/h700/prebuilt/gcc/linux-x86/arm/toolchain-sunxi-glibc/toolchain/bin/arm-openwrt-linux-gnueabi-gcc AR=/home/work_code/h700/prebuilt/gcc/linux-x86/arm/toolchain-sunxi-glibc/toolchain/bin/arm-openwrt-linux-gnueabi-ar STRIP=/home/work_code/h700/prebuilt/gcc/linux-x86/arm/toolchain-sunxi-glibc/toolchain/bin/arm-openwrt-linux-gnueabi-strip RANLIB=/home/work_code/h700/prebuilt/gcc/linux-x86/arm/toolchain-sunxi-glibc/toolchain/bin/arm-openwrt-linux-gnueabi-ranlib CPPFLAGS="-I/path/ZLIB/include" LDFLAGS="-L/path/ZLIB/lib/" --prefix=$PWD/_install

执行
make&&sudo make install
成功之后我们当前目录下的_install下应该有如下文件:

kws@kws-virtual-machine:/home/work_code/h700/package/png/libpng-1.5.14$ ls _install/
bin  include  lib  share
kws@kws-virtual-machine:/home/work_code/h700/package/png/libpng-1.5.14$ 

⑤ 至此安装完成,就可以进行正常的移植了

如果有问题,可以私信留言,看到会回的!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以使用以下代码将PNG图片文件转化为RGB点阵数组: ```c #include <stdio.h> #include <stdlib.h> #define PNG_BYTES_TO_CHECK 4 typedef unsigned char byte; byte* png_to_rgb(const char* filename, int* width, int* height) { FILE* fp = fopen(filename, "rb"); if (!fp) { printf("Failed to open file: %s\n", filename); return NULL; } // Check if file is a PNG image byte header[PNG_BYTES_TO_CHECK]; fread(header, 1, PNG_BYTES_TO_CHECK, fp); if (png_sig_cmp(header, 0, PNG_BYTES_TO_CHECK)) { printf("File is not a PNG image: %s\n", filename); fclose(fp); return NULL; } // Initialize PNG structures png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (!png_ptr) { printf("Failed to create PNG structure: %s\n", filename); fclose(fp); return NULL; } png_infop info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) { printf("Failed to create PNG info structure: %s\n", filename); png_destroy_read_struct(&png_ptr, NULL, NULL); fclose(fp); return NULL; } // Set up error handling if (setjmp(png_jmpbuf(png_ptr))) { printf("Failed to read PNG: %s\n", filename); png_destroy_read_struct(&png_ptr, &info_ptr, NULL); fclose(fp); return NULL; } // Initialize PNG IO png_init_io(png_ptr, fp); png_set_sig_bytes(png_ptr, PNG_BYTES_TO_CHECK); png_read_info(png_ptr, info_ptr); // Get PNG attributes *width = png_get_image_width(png_ptr, info_ptr); *height = png_get_image_height(png_ptr, info_ptr); png_byte color_type = png_get_color_type(png_ptr, info_ptr); png_byte bit_depth = png_get_bit_depth(png_ptr, info_ptr); // Transform PNG to 8-bit RGB if (color_type == PNG_COLOR_TYPE_PALETTE) { png_set_palette_to_rgb(png_ptr); } if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) { png_set_expand_gray_1_2_4_to_8(png_ptr); } if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) { png_set_tRNS_to_alpha(png_ptr); } if (bit_depth == 16) { png_set_strip_16(png_ptr); } if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) { png_set_gray_to_rgb(png_ptr); } png_read_update_info(png_ptr, info_ptr); // Allocate memory for RGB pixel data byte* pixels = (byte*) malloc(sizeof(byte) * (*width) * (*height) * 3); // Read PNG pixel data and transform to RGB png_bytep* row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * (*height)); for (int i = 0; i < (*height); i++) { row_pointers[i] = &pixels[i * (*width) * 3]; } png_read_image(png_ptr, row_pointers); free(row_pointers); // Finish PNG read png_read_end(png_ptr, NULL); png_destroy_read_struct(&png_ptr, &info_ptr, NULL); fclose(fp); // Return RGB pixel data return pixels; } ``` 这个函数使用的是C语言内置的PNG库,不需要使用libpngzlib库。使用时,只需要调用这个函数,传入PNG图片的文件名,以及两个指针变量,用于返回图片的宽度和高度。函数返回的是一个指向RGB点阵数组的指针,数组中每个元素表示一个像素的RGB值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值