-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchangetheme.h
113 lines (96 loc) · 2.88 KB
/
changetheme.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#ifndef _CHANGETHEME_
#define _CHANGETHEME_
#include <stdio.h>
#include <stdlib.h>
#include <png.h>
#include <strings.h>
int get_pic_theme(const char *filepath, char *theme)
{
png_structp png_ptr;
png_infop info_ptr;
FILE *fp = fopen(filepath, "rb");
if (!fp) {
return 1;
}
char checkheader[8];
if (fread(checkheader, 1, 8, fp) != 8) { //长度过长,直接退出
fclose(fp);
return 1;
} else if (png_check_sig(checkheader, 8) == 0) {
fclose(fp);
return 1;
}
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
info_ptr = png_create_info_struct(png_ptr);
rewind(fp);
png_init_io(png_ptr, fp);
png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_EXPAND, 0);
int channels, color_type;
int width, height;
unsigned char * rgba;
channels = png_get_channels(png_ptr, info_ptr);
color_type = png_get_color_type(png_ptr, info_ptr);
height = png_get_image_height(png_ptr, info_ptr);
width = png_get_image_width(png_ptr, info_ptr);
int size;
int pos = 0;
png_bytepp row_pointers;
row_pointers = png_get_rows(png_ptr, info_ptr);
size = height * width;
if (channels == 4 || color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
rgba = (png_bytep)malloc(size*sizeof(unsigned char)*4);
if (NULL == rgba) {
png_destroy_read_struct(&png_ptr, &info_ptr, 0);
fclose(fp);
return 1;
}
long int r_sum, g_sum, b_sum;
r_sum = g_sum = b_sum = 0;
for (int i = 0; i < height*0.01; ++i) {
for (int j = 0; j < width * 4; j += 4) {
rgba[pos++] = row_pointers[i][j+3];
rgba[pos++] = row_pointers[i][j+2];
rgba[pos++] = row_pointers[i][j+1];
rgba[pos++] = row_pointers[i][j+0];
r_sum += row_pointers[i][j+0];
g_sum += row_pointers[i][j+1];
b_sum += row_pointers[i][j+2];
}
}
free(rgba);
int all = height*0.01 * width;
printf("%ld %ld %ld\n", r_sum, g_sum, b_sum);
printf("#%lx%lx%lx", r_sum/all, g_sum/all, b_sum/all);
sprintf(theme, "#%lx%lx%lx", r_sum/all, g_sum/all, b_sum/all);
png_destroy_read_struct(&png_ptr, &info_ptr, 0);
return 0;
} else if (channels == 3 || color_type == PNG_COLOR_TYPE_RGB) {
rgba = (png_bytep)malloc(size*sizeof(unsigned char)*3);
if (NULL == rgba) {
png_destroy_read_struct(&png_ptr, &info_ptr, 0);
fclose(fp);
return 1;
}
long int r_sum, g_sum, b_sum;
r_sum = g_sum = b_sum = 0;
for (int i = 0; i < height*0.01; ++i)
for (int j = 0; j < width*3; j+=3) {
rgba[pos++] = row_pointers[i][j+2];
rgba[pos++] = row_pointers[i][j+1];
rgba[pos++] = row_pointers[i][j+0];
r_sum += row_pointers[i][j+0];
g_sum += row_pointers[i][j+1];
b_sum += row_pointers[i][j+2];
}
free(rgba);
int all = height*0.01 * width;
printf("%ld %ld %ld\n", r_sum, g_sum, b_sum);
sprintf(theme, "#%lx%lx%lx", r_sum/all, g_sum/all, b_sum/all);
png_destroy_read_struct(&png_ptr, &info_ptr, 0);
return 0;
} else {
png_destroy_read_struct(&png_ptr, &info_ptr, 0);
return 1;
}
}
#endif