1 /* Sitronix st2205 picframe access library
3 * Copyright (c) 2010 Hans de Goede <hdegoede@redhat.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation; either version 2.1 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <gphoto2/gphoto2-library.h>
29 #include <gphoto2-endian.h>
31 #define GP_MODULE "st2205"
33 #define ST2205_CMD_OFFSET 0x6200
34 #define ST2205_WRITE_OFFSET 0x6600
35 #define ST2205_READ_OFFSET 0xb000
36 /* 2 block sizes, first is bytes per transfer, second is bytes per erase /
38 #define ST2205_BLOCK_SIZE 32768
39 #define ST2205_ERASE_BLOCK_SIZE 65536
40 #define ST2205_FAT_SIZE 8192
41 #define ST2205_COUNT_OFFSET 0x6
42 /* 16 bytes info per file + a 16 bytes header */
43 #define ST2205_FILE_OFFSET(x) (((x) + 1) * 16)
44 #define ST2205_LOOKUP_SIZE (0x8000 - 0x0477)
45 #define ST2205_V1_LOOKUP_OFFSET 0x8477
46 #define ST2205_V2_LOOKUP_OFFSET 0xd8477
47 #define ST2205_V1_FIRMWARE_SIZE 65536
48 #define ST2205_V2_FIRMWARE_SIZE 262144
49 #define ST2205_V1_PICTURE_START 65536
50 #define ST2205_V2_PICTURE_START 8192
51 #define ST2205_LOOKUP_CHECKSUM 0x0016206f
52 #define ST2205_SHUFFLE_SIZE (240 * 320 / 64)
53 #define ST2205_HEADER_MARKER 0xf5
54 /* The "FAT" cannot contain more then 510 entries */
55 #define ST2205_MAX_NO_FILES 510
56 #define ST2205_FILENAME_LENGTH 10
60 ORIENTATION_LANDSCAPE,
64 /* We prefix all names with there idx in the FAT table, to make sure they are
65 all unique and don't change when files with the same name (after converting
66 to ascii and truncating) are added / deleted. */
67 #define ST2205_SET_FILENAME(dest, name, idx) \
68 snprintf(dest, sizeof(st2205_filename), "%04d-%s.png", (idx) + 1, name)
75 struct st2205_image_header {
76 uint8_t marker; /* Always 0xF5 */
77 uint16_t width; /* big endian */
78 uint16_t height; /* big endian */
79 uint16_t blocks; /* number of 8x8 blocks in the image, big endian */
80 uint8_t shuffle_table; /* shuffle table idx (for transition effects) */
81 uint8_t unknown2; /* Unknown usually 0x04 */
82 uint8_t unknown3; /* shuffle related, must have a special value depend
83 on the pattern see the table in st2205_init */
84 uint16_t length; /* length of the data *following* the header (be) */
85 uint8_t unknown4[4]; /* Always 4x 0x00 (padding ?) */
86 } __attribute__((packed));
88 #define CHECK(result) {int r=(result); if (r<0) return (r);}
90 /* The + 5 is space for the "####-" prefix we add, + 4 for .png postfix. */
91 typedef char st2205_filename[ST2205_FILENAME_LENGTH + 5 + 4 + 1];
92 typedef int16_t st2205_lookup_row[8];
94 struct _CameraPrivateLibrary {
95 /* Used by library.c glue code */
99 st2205_filename filenames[ST2205_MAX_NO_FILES];
101 /* Driver configuration settings */
105 /* Used by st2205.c / st2205_decode.c */
108 int compressed; /* Is the image data compressed or rgb565 ? */
111 char *buf; /* 512 bytes aligned buffer (for sending / reading cmds) */
116 int block_is_present[2097152 / ST2205_BLOCK_SIZE];
117 int block_dirty[2097152 / ST2205_BLOCK_SIZE];
118 st2205_lookup_row lookup[3][256];
119 struct st2205_coord shuffle[8][ST2205_SHUFFLE_SIZE];
121 unsigned char unknown3[8];
122 unsigned int rand_seed;
125 /* functions in st2205.c */
127 st2205_open_device(Camera *camera);
130 st2205_open_dump(Camera *camera, const char *dump,
131 int width, int height);
133 void st2205_close(Camera *camera);
136 st2205_get_filenames(Camera *camera, st2205_filename *names);
139 st2205_read_file(Camera *camera, int idx, int **rgb24);
142 st2205_read_raw_file(Camera *camera, int idx, unsigned char **raw);
145 st2205_set_time_and_date(Camera *camera, struct tm *t);
147 /* This function returns the index used to save the file at on success */
149 st2205_write_file(Camera *camera,
150 const char *filename, int **rgb24);
153 st2205_delete_file(Camera *camera, int idx);
156 st2205_delete_all(Camera *camera);
159 st2205_commit(Camera *camera);
162 st2205_get_mem_size(Camera *camera);
165 st2205_get_free_mem_size(Camera *camera);
167 /* functions in st2205_decode.c */
169 st2205_decode_image(CameraPrivateLibrary *pl, unsigned char *src, int **dest);
172 st2205_code_image(CameraPrivateLibrary *pl, int **src,
173 unsigned char *dest, uint8_t shuffle_pattern, int allow_uv_corr);
176 st2205_rgb565_to_rgb24(CameraPrivateLibrary *pl, unsigned char *src,
180 st2205_rgb24_to_rgb565(CameraPrivateLibrary *pl, int **src,
181 unsigned char *dest);