Zlib and libpng configuration with installation and generation of PRART 3 LIBPNG PNG Pictures

zhaozj2021-02-08  392

Libpng installation

Next, the process of installing libpng is slightly relaxed. First download the latest libpng program source file. The URL is http://sourceforge.net/projects/libpng/ or http://www.libpng.org/pub/png/. The files may wish to be downloaded are libpng-1.2.5.tar.gz, release this file to D: / libpng /.

Modify D: /Libpng/Libpng-1.2.5/scripts/makefile.bc32, which is made for the Borland C 32-bit version of Makefile. Change the ZLIB_DIR = .. / zlib of the 12th line to zlib_dir = d: / myLibs, then remove the 20 line # target_cpu = 6 before. Then execute

D: /libpng/libpng-1.2.5> make -fscripts / makefile.bc32

Make Version 5.2 Copyright (C) 1987, 2000 Borland

D: /libpng/libpng-1.2.5> PNGTest

Testing Libpng Version 1.2.5

With Zlib Version 1.1.4

.

Pass (9782 Zero Samples)

.

Libpng Passs Test

See the words "9782 Zero Samples" indicating that libpng installation is successful. The newly generated pngout.png should be exactly the same as the original pngtest.png. PNG.H, pngconf.h copies together with the build-generated libpng.lib to D: / myLibs /.

Generate a PNG file

We write one or two programs to test the functionality of libpng to generate a PNG file. Testpng1.cpp generate a grayscale image; TestPng2.cpp generates 256 color image, where a color palette has a variety of configurations. The image files generated by the two programs are shown in Figures 1 and 2, respectively. Compile parameters are:

BCC32 -ID: / mylibs -ld: / mylibs testpng1.cpp libpng.lib zlib.lib

For future use, I put D: / myLibs / join to the BCC 5.5 of the include search path and lib search path.

// TestPng1.cpp

// Test for Black & White Pictures.

/ / Please download the panel of the article http://www.chenshuo.com

Testpng1.cpp and testpng2.cpp difference in png_set_IHDR this line, the first one is png_set_IHDR (png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_GRAY, ...) ;, the latter is png_set_IHDR (png_ptr, info_ptr, width, height, 8, png_color_type_palette, ...) ;.

// TestPng2.cpp

// Test for Colorful Pictures

/ / Please download the panel of the article http://www.chenshuo.com

Figure 1 grayscale PNG picture Figure 2 Color PNG picture

Example.c with libpng belt is a good learning example. When using libpng, you will first include png.h. This header file contains many types of libPng customized, such as PNG_Struct, PNG_INFO, etc. in the program, the former is the structure used inside the libPng, which is used to represent a PNG file.

Writing a program that generates PNG first declares several necessary variables, where png_structp is png_struct *: file * fp;

PNG_STRUCTP PNG_PTR;

PNG_INFOP INFO_PTR;

PNG_Colorp Palette;

Then open the PNG file that wants to write in a Binary Write mode.

FP = fopen (filename.c_str (), "wb");

To use libpng, assign and initialize PNG_Struct:

PNG_PTR = PNG_create_write_struct (png_libpng_ver_string, null, null, null);

Then initializes INFO_PTR as the parameter as PNG_PTR. The properties of the PNG file are read or set with a PNG_GET_ * () or png_set _ * () function.

INFO_PTR = PNG_CREATE_INFO_STRUCT (PNG_PTR);

Set the error handling method, or you can specify the Callback function of the error in Chain 6.

IF (setjmp (PNG_JMPBUF (PNG_PTR))))

{

//.

}

Next, tell libpng to write the PNG file with fwrite and pass the file * fp that has been opened in binary mode:

PNG_INIT_IO (PNG_PTR, FP);

Set the basic properties of the PNG file, such as height, width, color depth (monochrome, 16 colors, 256 colors or true color), color type (grayscale, palette, RGB), and the like. Here we generate a 256 (28 = 256) color, using the PNG file of the palette (png_color_type_palette).

Const int width = 120;

Const int Height = 512;

PNG_SET_IHDR (PNG_PTR, INFO_PTR, Width, Height, 8, PNG_COLOR_TYPE_PALETTE,

PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);

Next, set the palette, first allocate space. The value of constant png_max_palette_length is 256:

Palette = (png_colorp) PNG_malloc (png_ptr,

PNG_MAX_PALETTE_LENGTH * SIZEOF (PNG_COLOR));

The png_color here represents the RGB component of a color in the palette.

Typedef struct png_color_struct

{

PNG_BYTE RED;

PNG_BYTE GREEN;

PNG_BYTE Blue;

} png_color;

Then set the palette with the SET_PALETTE function written.

Set_palette (Palette, Red_Black);

The set_palette function can generate three types of palette, Gray grayscale, red_black red and black, Spectrum spectrum.

The code that generates the Gray palette is:

For (int i = 0; i

Palette [i] .red = Palette [i] .green = Palette [i] .boe = i;

}

Tell LibPNG to use our prepared palette and write the head of the PNG file.

PNG_SET_PLTE (PNG_PTR, INFO_PTR, PALETTE, PNG_MAX_PALETTE_LENGTH);

PNG_WRITE_INFO (PNG_PTR, INFO_PTR); After these preparations are finished, enter the most critical step - draw the picture content, here we only use the color lines of the palette to draw horizontal lines. We prepare a large enough memory image to represent all points in the entire image, which is arranged from left to right and from top to bottom. Note that the PNG file is a line priority. When writing a PNG file, don't tell it all the graphics, just tell it to each line (represented by array row_points). So if some rows in the image are the same, the row pointer Row_Pointer can repeat the address of these rows, which saves memory space. I wrote this program that does not use this method, it will be a big problem.

PNG_UINT_32 K;

PNG_BYTE Image [height] [width];

PNG_BYTEP ROW_POINTERS [HEIGHT];

FOR (k = 0; k

MEMSET (Image [K], K / 2, Width;

Row_Pointers [K] = Image [k];

}

Next, write an overall image, is the most labor-saving approach:

PNG_WRITE_IMAGE (PNG_PTR, Row_Pointers);

At the end, the necessary sweeping work:

PNG_WRITE_END (PNG_PTR, INFO_PTR);

PNG_Free (PNG_PTR, PALETTE);

PNG_DESTROY_WRITE_STRUCT (& PNG_PTR, & Info_PTR);

Fclose (fp);

At this point, it is very good.

转载请注明原文地址:https://www.9cbs.com/read-437.html

New Post(0)