e166205f0dc225d91d4d1da15760afaa41a42100
[vuplus_openvuplus_3.0] / meta-openvuplus / recipes-vuplus / png-util / files / png_util.cpp
1 #include "png_util.h"
2
3 static int x, y;
4 static int width, height;
5
6 static png_byte color_type;
7 static png_byte bit_depth;
8
9 static png_structp png_ptr;
10 static png_infop info_ptr;
11 static int number_of_passes;
12 static png_bytep * row_pointers = 0;
13 static png_bytep row_pointers_bit_shift = 0;
14 static int row_byte_len = 0;
15
16 //#define UNIT_TEST 1
17 #ifdef UNIT_TEST
18 int fd = open("/tmp/dump.txt", O_WRONLY|O_CREAT|O_LARGEFILE, 0644);
19 #endif
20
21 static int read_png_file(char* file_name)
22 {
23         /* 8 is the maximum size that can be checked */
24         char header[8] = {0,};
25
26         /* open file and test for it being a png */
27         FILE *fp = fopen(file_name, "rb");
28         if (!fp) 
29         {
30                 fprintf(stderr, "[read_png_file] File %s could not be opened for reading\n", file_name);
31                 return 0;
32         }
33
34         fread(header, 1, 8, fp);
35         if (png_sig_cmp((png_byte*)header, 0, 8)) 
36         {
37                 fprintf(stderr, "[read_png_file] File %s is not recognized as a PNG file\n", file_name);
38                 return 0;
39         }
40
41         /* initialize stuff */
42         png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
43
44         if (!png_ptr) 
45         {
46                 fprintf(stderr, "[read_png_file] png_create_read_struct failed\n");
47                 return 0;
48         }
49
50         info_ptr = png_create_info_struct(png_ptr);
51         if (!info_ptr) 
52         {
53                 fprintf(stderr, "[read_png_file] png_create_info_struct failed\n");
54                 return 0;
55         }
56
57         if (setjmp(png_jmpbuf(png_ptr))) 
58         {
59                 fprintf(stderr, "[read_png_file] Error during init_io\n");
60                 return 0;
61         }
62
63         png_init_io(png_ptr, fp);
64         png_set_sig_bytes(png_ptr, 8);
65
66         png_read_info(png_ptr, info_ptr);
67
68         width = png_get_image_width(png_ptr, info_ptr);
69         height = png_get_image_height(png_ptr, info_ptr);
70         color_type = png_get_color_type(png_ptr, info_ptr);
71         bit_depth = png_get_bit_depth(png_ptr, info_ptr);
72
73         number_of_passes = png_set_interlace_handling(png_ptr);
74         png_read_update_info(png_ptr, info_ptr);
75
76         /* read file */
77         if (setjmp(png_jmpbuf(png_ptr))) 
78         {
79                 fprintf(stderr, "[read_png_file] Error during read_image\n");
80                 return 0;
81         }
82
83         if(row_pointers == 0) 
84         {
85                 row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * height);
86                 row_byte_len = png_get_rowbytes(png_ptr,info_ptr);
87                 for (y=0; y<height; y++) 
88                 {
89                         row_pointers[y] = (png_byte*) malloc(row_byte_len);
90                 }
91                 row_pointers_bit_shift = (unsigned char*) malloc(sizeof(unsigned char) * height * width * 2);
92         }
93
94         png_read_image(png_ptr, row_pointers);
95         png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
96         fclose(fp);
97         return 1;
98 }
99
100 PNGUtil *PNGUtil::instance = 0;
101
102 PNGUtil::PNGUtil() 
103 {
104         instance = this;
105         device_fd = -1;
106 }
107
108 PNGUtil::~PNGUtil()
109 {
110         disconnect();
111 }
112
113 PNGUtil *PNGUtil::getInstance()
114 {
115         return instance;
116 }
117
118 int PNGUtil::connect()
119 {
120         const char* device_file_name = "/dev/lcd2";
121         device_fd = open("/dev/lcd2", O_RDWR);
122         if(device_fd <0)
123                 return 0;
124         return 1;
125 }
126
127 void PNGUtil::disconnect() 
128 {
129         if(device_fd >= 0)
130         {
131                 close(device_fd);
132                 device_fd = -1;
133         }
134 }
135
136 int PNGUtil::send(char* png_file_name) 
137 {
138         if(device_fd < 0) 
139         {
140                 fprintf(stderr, "unloaded!! retry.. connect!!\n");
141                 return 0;
142         }
143         if(!read_png_file(png_file_name))
144         {
145                 return 0;
146         }
147         {
148                 
149                 int i,j;
150                 int row_pointers_ptr = 0;
151                 int row_pointers_2_ptr = 0;
152                 
153                 for(i=0;i<height;i++)
154                 {
155                         for(j=0;j<row_byte_len;j+=3)
156                         {
157                                 row_pointers_bit_shift[row_pointers_2_ptr]=(row_pointers[i][j]&248)|(row_pointers[i][j+1]>>5);
158                                 row_pointers_bit_shift[row_pointers_2_ptr+1]=((row_pointers[i][j+1]&28)<<3)|(row_pointers[i][j+2]>>3);
159                                 row_pointers_2_ptr += 2;
160                         }
161                 }
162         }
163 #ifdef UNIT_TEST
164         {
165                 int w = write(fd, row_pointers_bit_shift , height * width * 2);
166                 printf("write to dst_fd : %d\n",w);
167         }
168 #endif
169 #ifndef UNIT_TEST
170         {
171                 int w=-1;
172                 w = write(device_fd, row_pointers_bit_shift, height * width * 2);
173                 printf("write ret : %d\n",w);
174 //              ret = ioctl(device_fd, 0);
175                 printf("write to /dev/lcd2 : %d\n",w);
176         }
177 #endif
178         return 1;
179 }
180