summaryrefslogtreecommitdiff
path: root/recipes/png-util/files/png_util.cpp
blob: 2736202593ca2149c469c9c554f6884b2fec6ff8 (plain)
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
175
176
177
178
179
#include "png_util.h"

static int x, y;
static int width, height;

static png_byte color_type;
static png_byte bit_depth;

static png_structp png_ptr;
static png_infop info_ptr;
static int number_of_passes;
static png_bytep * row_pointers = 0;
static png_bytep row_pointers_bit_shift = 0;
static int row_byte_len = 0;

//#define UNIT_TEST 1
#ifdef UNIT_TEST
int fd = open("/tmp/dump.txt", O_WRONLY|O_CREAT|O_LARGEFILE, 0644);
#endif

static int read_png_file(char* file_name)
{
	/* 8 is the maximum size that can be checked */
        char header[8] = {0,};

        /* open file and test for it being a png */
        FILE *fp = fopen(file_name, "rb");
        if (!fp) 
	{
                fprintf(stderr, "[read_png_file] File %s could not be opened for reading\n", file_name);
		return 0;
	}

        fread(header, 1, 8, fp);
        if (png_sig_cmp((png_byte*)header, 0, 8)) 
	{
                fprintf(stderr, "[read_png_file] File %s is not recognized as a PNG file\n", file_name);
		return 0;
	}

        /* initialize stuff */
        png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);

        if (!png_ptr) 
	{
                fprintf(stderr, "[read_png_file] png_create_read_struct failed\n");
		return 0;
	}

        info_ptr = png_create_info_struct(png_ptr);
        if (!info_ptr) 
	{
                fprintf(stderr, "[read_png_file] png_create_info_struct failed\n");
		return 0;
	}

        if (setjmp(png_jmpbuf(png_ptr))) 
	{
                fprintf(stderr, "[read_png_file] Error during init_io\n");
		return 0;
	}

        png_init_io(png_ptr, fp);
        png_set_sig_bytes(png_ptr, 8);

        png_read_info(png_ptr, info_ptr);

        width = png_get_image_width(png_ptr, info_ptr);
        height = png_get_image_height(png_ptr, info_ptr);
        color_type = png_get_color_type(png_ptr, info_ptr);
        bit_depth = png_get_bit_depth(png_ptr, info_ptr);

        number_of_passes = png_set_interlace_handling(png_ptr);
        png_read_update_info(png_ptr, info_ptr);

        /* read file */
        if (setjmp(png_jmpbuf(png_ptr))) 
	{
                fprintf(stderr, "[read_png_file] Error during read_image\n");
		return 0;
	}

	if(row_pointers == 0) 
	{
		row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * height);
		row_byte_len = png_get_rowbytes(png_ptr,info_ptr);
	        for (y=0; y<height; y++) 
		{
	                row_pointers[y] = (png_byte*) malloc(row_byte_len);
		}
		row_pointers_bit_shift = (unsigned char*) malloc(sizeof(unsigned char) * height * width * 2);
	}

        png_read_image(png_ptr, row_pointers);
        fclose(fp);
	return 1;
}

PNGUtil *PNGUtil::instance = 0;

PNGUtil::PNGUtil() 
{
	instance = this;
	device_fd = -1;
}

PNGUtil::~PNGUtil()
{
	disconnect();
}

PNGUtil *PNGUtil::getInstance()
{
	return instance;
}

int PNGUtil::connect()
{
	const char* device_file_name = "/dev/lcd2";
	device_fd = open("/dev/lcd2", O_RDWR);
	if(device_fd <0)
		return 0;
	return 1;
}

void PNGUtil::disconnect() 
{
	if(device_fd >= 0)
	{
		close(device_fd);
		device_fd = -1;
	}
}

int PNGUtil::send(char* png_file_name) 
{
	if(device_fd < 0) 
	{
		fprintf(stderr, "unloaded!! retry.. connect!!\n");
		return 0;
	}
	if(!read_png_file(png_file_name))
	{
		return 0;
	}
	{
		
		int i,j;
		int row_pointers_ptr = 0;
		int row_pointers_2_ptr = 0;
		
		for(i=0;i<height;i++)
		{
			for(j=0;j<row_byte_len;j+=3)
			{
				row_pointers_bit_shift[row_pointers_2_ptr]=(row_pointers[i][j]&248)|(row_pointers[i][j+1]>>5);
				row_pointers_bit_shift[row_pointers_2_ptr+1]=((row_pointers[i][j+1]&28)<<3)|(row_pointers[i][j+2]>>3);
				row_pointers_2_ptr += 2;
			}
		}
	}
#ifdef UNIT_TEST
	{
		int w = write(fd, row_pointers_bit_shift , height * width * 2);
		printf("write to dst_fd : %d\n",w);
	}
#endif
#ifndef UNIT_TEST
	{
		int w=-1;
		w = write(device_fd, row_pointers_bit_shift, height * width * 2);
		printf("write ret : %d\n",w);
//		ret = ioctl(device_fd, 0);
		printf("write to /dev/lcd2 : %d\n",w);
	}
#endif
	return 1;
}