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