-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathpngtest.c
174 lines (141 loc) · 3.88 KB
/
pngtest.c
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <wiringPi.h>
#include "tft_lib.h"
#include "pngle/pngle.h"
#include "pngle/decode_png.h"
#include "driver/ili9341.h"
#define DRIVER_NAME "ILI9341"
#define SCREEN_WIDTH 240
#define SCREEN_HEIGHT 320
#define OFFSET_X 0
#define OFFSET_Y 0
#define INIT_FUNCTION(a, b, c, d, e) ili9341_lcdInit(a, b, c, d, e)
#define _DEBUG_ 0
time_t elapsedTime(struct timeval startTime, struct timeval endTime) {
time_t diffsec = difftime(endTime.tv_sec, startTime.tv_sec);
suseconds_t diffsub = endTime.tv_usec - startTime.tv_usec;
//printf("diffsec=%ld diffsub=%ld\n",diffsec, diffsub);
if(diffsub < 0) {
diffsec--;
diffsub = (endTime.tv_usec+1000000) - startTime.tv_usec;
}
uint16_t diffmsec = diffsub / 1000;
time_t diff = (diffsec * 1000) + diffmsec;
return diff;
}
time_t PNGTest(TFT_t * dev, char * file, int width, int height) {
struct timeval startTime, endTime;
gettimeofday(&startTime, NULL);
lcdFillScreen(dev, BLACK);
int _width = width;
if (width > 240) _width = 240;
int _height = height;
if (height > 320) _height = 320;
// open PNG file
FILE* fp = fopen(file, "rb");
if (fp == NULL) {
printf("File not found [%s]\n", file);
return 0;
}
char buf[1024];
size_t remain = 0;
int len;
pngle_t *pngle = pngle_new(_width, _height);
pngle_set_init_callback(pngle, png_init);
pngle_set_draw_callback(pngle, png_draw);
pngle_set_done_callback(pngle, png_finish);
double display_gamma = 2.2;
pngle_set_display_gamma(pngle, display_gamma);
while (!feof(fp)) {
if (remain >= sizeof(buf)) {
printf("Buffer exceeded\n");
return 0;
}
len = fread(buf + remain, 1, sizeof(buf) - remain, fp);
if (len <= 0) {
//printf("EOF\n");
break;
}
int fed = pngle_feed(pngle, buf, remain + len);
if (fed < 0) {
printf("ERROR; %s\n", pngle_error(pngle));
return 0;
}
remain = remain + len - fed;
if (remain > 0) memmove(buf, buf + fed, remain);
}
fclose(fp);
uint16_t pngWidth = width;
uint16_t offsetX = 0;
if (width > pngle->imageWidth) {
pngWidth = pngle->imageWidth;
offsetX = (width - pngle->imageWidth) / 2;
}
printf("pngWidth=%d offsetX=%d", pngWidth, offsetX);
uint16_t pngHeight = height;
uint16_t offsetY = 0;
if (height > pngle->imageHeight) {
pngHeight = pngle->imageHeight;
offsetY = (height - pngle->imageHeight) / 2;
}
printf("pngHeight=%d offsetY=%d\n", pngHeight, offsetY);
uint16_t *colors = (uint16_t*)malloc(sizeof(uint16_t) * pngWidth);
int ypos = (height-1) - offsetY;
for(int y = 0; y < pngHeight; y++){
for(int x = 0;x < pngWidth; x++){
pixel_png pixel = pngle->pixels[y][x];
uint16_t color = rgb565_conv(pixel.red, pixel.green, pixel.blue);
//lcdDrawPixel(dev, x+offsetX, y+offsetY, color);
lcdDrawPixel(dev, x+offsetX, ypos, color);
}
ypos--;
}
free(colors);
pngle_destroy(pngle, _width, _height);
gettimeofday(&endTime, NULL);
time_t diff = elapsedTime(startTime, endTime);
printf("%s elapsed time[ms]=%ld\n",__func__, diff);
return diff;
}
int main(int argc, char **argv){
if(wiringPiSetup() == -1) {
printf("wiringPiSetup Fail\n");
return 1;
}
int i;
char base[128];
strcpy(base, argv[0]);
for(i=strlen(base);i>0;i--) {
if (base[i-1] == '/') {
base[i] = 0;
break;
}
}
if(_DEBUG_)printf("base=%s\n",base);
char ppath[128];
strcpy(ppath,base);
strcat(ppath,"pin.conf");
if(_DEBUG_)printf("ppath=%s\n",ppath);
struct stat buffer;
if (stat(ppath, &buffer) != 0) {
printf("pin.conf [%s] not found\n",ppath);
return 1;
}
TFT_t dev;
lcdInterface(&dev, ppath);
lcdReset(&dev);
INIT_FUNCTION(&dev, SCREEN_WIDTH, SCREEN_HEIGHT, OFFSET_X, OFFSET_Y);
char file[64];
strcpy(file, "./images/Ubuntu_log.png");
PNGTest(&dev, file, SCREEN_WIDTH, SCREEN_HEIGHT);
sleep(5);
//lcdFadeout(&dev, DIRECTION90, BLACK);
}