/home/liu/actions-runner/_work/ccv/ccv/lib/io/_ccv_io_libpng.inc
Line | Count | Source (jump to first uncovered line) |
1 | | static void _ccv_read_png_fd(FILE* in, ccv_dense_matrix_t** x, int type) |
2 | 41 | { |
3 | 41 | png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0); |
4 | 41 | png_infop info_ptr = png_create_info_struct(png_ptr); |
5 | 41 | if (setjmp(png_jmpbuf(png_ptr))) |
6 | 0 | { |
7 | 0 | png_destroy_read_struct(&png_ptr, &info_ptr, 0); |
8 | 0 | return; |
9 | 0 | } |
10 | 41 | png_init_io(png_ptr, in); |
11 | 41 | png_read_info(png_ptr, info_ptr); |
12 | 41 | png_uint_32 width, height; |
13 | 41 | int bit_depth, color_type; |
14 | 41 | png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, 0, 0, 0); |
15 | | |
16 | 41 | ccv_dense_matrix_t* im = *x; |
17 | 41 | if (im == 0) |
18 | 41 | *x = im = ccv_dense_matrix_new((int) height, (int) width, (type) ? type18 : CCV_8U | (23 ((color_type & PNG_COLOR_MASK_COLOR) == PNG_COLOR_TYPE_GRAY)23 ? CCV_C10 : CCV_C323 ), 0, 0); |
19 | | |
20 | 41 | png_set_strip_16(png_ptr); |
21 | 41 | png_set_strip_alpha(png_ptr); |
22 | 41 | if (color_type == PNG_COLOR_TYPE_PALETTE) |
23 | 0 | png_set_palette_to_rgb(png_ptr); |
24 | 41 | if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 80 ) |
25 | 0 | png_set_expand_gray_1_2_4_to_8(png_ptr); |
26 | 41 | if (CCV_GET_CHANNEL(im->type) == CCV_C3) |
27 | 37 | png_set_gray_to_rgb(png_ptr); |
28 | 4 | else if (CCV_GET_CHANNEL(im->type) == CCV_C1) |
29 | 4 | png_set_rgb_to_gray(png_ptr, 1, -1, -1); |
30 | | |
31 | 41 | png_read_update_info(png_ptr, info_ptr); |
32 | | |
33 | 41 | unsigned char** row_vectors = (unsigned char**)alloca(im->rows * sizeof(unsigned char*)); |
34 | 41 | int i; |
35 | 21.4k | for (i = 0; i < im->rows; i++21.4k ) |
36 | 21.4k | row_vectors[i] = im->data.u8 + i * im->step; |
37 | 41 | png_read_image(png_ptr, row_vectors); |
38 | 41 | png_read_end(png_ptr, 0); |
39 | 41 | int ch = CCV_GET_CHANNEL(im->type); |
40 | | // empty out the padding |
41 | 41 | if (im->cols * ch < im->step) |
42 | 1 | { |
43 | 1 | size_t extra = im->step - im->cols * ch; |
44 | 1 | unsigned char* ptr = im->data.u8 + im->cols * ch; |
45 | 174 | for (i = 0; i < im->rows; i++, ptr += im->step173 ) |
46 | 173 | memset(ptr, 0, extra); |
47 | 1 | } |
48 | | |
49 | 41 | png_destroy_read_struct(&png_ptr, &info_ptr, 0); |
50 | 41 | } |
51 | | |
52 | | static int _ccv_write_png_fd(ccv_dense_matrix_t* mat, FILE* fd, void* conf) |
53 | 3 | { |
54 | 3 | png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0); |
55 | 3 | png_infop info_ptr = png_create_info_struct(png_ptr); |
56 | 3 | if (setjmp(png_jmpbuf(png_ptr))) |
57 | 1 | { |
58 | 1 | png_destroy_write_struct(&png_ptr, &info_ptr); |
59 | 1 | return -1; |
60 | 1 | } |
61 | 2 | png_init_io(png_ptr, fd); |
62 | 2 | int compression_level = 0; |
63 | 2 | if (conf != 0) |
64 | 0 | compression_level = ccv_clamp(*(int*)conf, 0, MAX_MEM_LEVEL); |
65 | 2 | if(compression_level > 0) |
66 | 0 | { |
67 | 0 | png_set_compression_mem_level(png_ptr, compression_level); |
68 | 2 | } else { |
69 | | // tune parameters for speed |
70 | | // (see http://wiki.linuxquestions.org/wiki/Libpng) |
71 | 2 | png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE, PNG_FILTER_SUB); |
72 | 2 | png_set_compression_level(png_ptr, Z_BEST_SPEED); |
73 | 2 | } |
74 | 2 | png_set_compression_strategy(png_ptr, Z_HUFFMAN_ONLY); |
75 | 18.4E | png_set_IHDR(png_ptr, info_ptr, mat->cols, mat->rows, (mat->type & CCV_8U)2 ? 83 : 16, (2 CCV_GET_CHANNEL2 (mat->type) == CCV_C1) ? PNG_COLOR_TYPE_GRAY : PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); |
76 | | |
77 | 2 | unsigned char** row_vectors = (unsigned char**)alloca(mat->rows * sizeof(unsigned char*)); |
78 | 2 | int i; |
79 | 1.80k | for (i = 0; i < mat->rows; i++1.80k ) |
80 | 1.80k | row_vectors[i] = mat->data.u8 + i * mat->step; |
81 | 2 | png_write_info(png_ptr, info_ptr); |
82 | 2 | png_write_image(png_ptr, row_vectors); |
83 | 2 | png_write_end(png_ptr, info_ptr); |
84 | 2 | png_destroy_write_struct(&png_ptr, &info_ptr); |
85 | 2 | return 0; |
86 | 3 | } |