Coverage Report

Created: 2021-04-07 21:56

/home/liu/buildslave/linux-x64-runtests/build/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
31
{
3
31
  png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
4
31
  png_infop info_ptr = png_create_info_struct(png_ptr);
5
31
  if (setjmp(png_jmpbuf(png_ptr)))
6
31
  {
7
0
    png_destroy_read_struct(&png_ptr, &info_ptr, 0);
8
0
    return;
9
0
  }
10
31
  png_init_io(png_ptr, in);
11
31
  png_read_info(png_ptr, info_ptr);
12
31
  png_uint_32 width, height;
13
31
  int bit_depth, color_type;
14
31
  png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, 0, 0, 0);
15
31
16
31
  ccv_dense_matrix_t* im = *x;
17
31
  if (im == 0)
18
31
    *x = im = ccv_dense_matrix_new((int) height, (int) width, (type) ? 
type18
:
CCV_8U | (((color_type & PNG_COLOR_MASK_COLOR) == PNG_COLOR_TYPE_GRAY) 13
?
CCV_C10
:
CCV_C313
), 0, 0);
19
31
20
31
  png_set_strip_16(png_ptr);
21
31
  png_set_strip_alpha(png_ptr);
22
31
  if (color_type == PNG_COLOR_TYPE_PALETTE)
23
31
    
png_set_palette_to_rgb(png_ptr)0
;
24
31
  if (color_type == PNG_COLOR_TYPE_GRAY && 
bit_depth < 80
)
25
0
    png_set_expand_gray_1_2_4_to_8(png_ptr);
26
31
  if (CCV_GET_CHANNEL(im->type) == CCV_C3)
27
27
    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
31
31
  png_read_update_info(png_ptr, info_ptr);
32
31
33
31
  unsigned char** row_vectors = (unsigned char**)alloca(im->rows * sizeof(unsigned char*));
34
31
  int i;
35
15.6k
  for (i = 0; i < im->rows; 
i++15.6k
)
36
15.6k
    row_vectors[i] = im->data.u8 + i * im->step;
37
31
  png_read_image(png_ptr, row_vectors);
38
31
  png_read_end(png_ptr, 0);
39
31
  int ch = CCV_GET_CHANNEL(im->type);
40
31
  // empty out the padding
41
31
  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
31
49
31
  png_destroy_read_struct(&png_ptr, &info_ptr, 0);
50
31
}
51
52
static void _ccv_write_png_fd(ccv_dense_matrix_t* mat, FILE* fd, void* conf)
53
0
{
54
0
  png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
55
0
  png_infop info_ptr = png_create_info_struct(png_ptr);
56
0
  if (setjmp(png_jmpbuf(png_ptr)))
57
0
  {
58
0
    png_destroy_write_struct(&png_ptr, &info_ptr);
59
0
    return;
60
0
  }
61
0
  png_init_io(png_ptr, fd);
62
0
  int compression_level = 0;
63
0
  if (conf != 0)
64
0
    compression_level = ccv_clamp(*(int*)conf, 0, MAX_MEM_LEVEL);
65
0
  if(compression_level > 0)
66
0
  {
67
0
    png_set_compression_mem_level(png_ptr, compression_level);
68
0
  } else {
69
0
    // tune parameters for speed
70
0
    // (see http://wiki.linuxquestions.org/wiki/Libpng)
71
0
    png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE, PNG_FILTER_SUB);
72
0
    png_set_compression_level(png_ptr, Z_BEST_SPEED);
73
0
  }
74
0
  png_set_compression_strategy(png_ptr, Z_HUFFMAN_ONLY);
75
0
  png_set_IHDR(png_ptr, info_ptr, mat->cols, mat->rows, (mat->type & CCV_8U) ? 8 : 16, (CCV_GET_CHANNEL(mat->type) == CCV_C1) ? PNG_COLOR_TYPE_GRAY : PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
76
0
  
77
0
  unsigned char** row_vectors = (unsigned char**)alloca(mat->rows * sizeof(unsigned char*));
78
0
  int i;
79
0
  for (i = 0; i < mat->rows; i++)
80
0
    row_vectors[i] = mat->data.u8 + i * mat->step;
81
0
  png_write_info(png_ptr, info_ptr);
82
0
  png_write_image(png_ptr, row_vectors);
83
0
  png_write_end(png_ptr, info_ptr);
84
0
  png_destroy_write_struct(&png_ptr, &info_ptr);
85
0
}