convert img to xpm
cover convert img to xpm
Changes from last version:
* Added length parameter
* created enum for scheme
* added sanity checking for img length
* added character lookup table to keep color strings to no more than 2 chars per pixel.
Kristen Carlson Accardi (2):
stkutil: convert img to xpm
test-stkutil: unit test for img to xpm converter
src/stkutil.c | 148 +++++++++++++++++++++++++++++++ src/stkutil.h | 8 ++ unit/test-stkutil.c | 240 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 396 insertions(+), 0 deletions(-)
PATCH 1/2 stkutil: convert img to xpm
--- src/stkutil.c | 148 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/stkutil.h | 8 +++ 2 files changed, 156 insertions(+), 0 deletions(-)
src/stkutil.c +148 -0
@@ -6076,3 +6076,151 @@ char *stk_text_to_html(const char *utf8,/* return characters from string. Caller must free char data */ return g_string_free (string, FALSE); } + +static const char chars_table[] = { +'0' ,'1' ,'2' ,'3' ,'4' ,'5' ,'6' ,'7' ,'8' ,'9' ,'A' ,'B' ,'C' , +'D' ,'E' ,'F' ,'G' ,'H' ,'I' ,'J' ,'K' ,'L' ,'M' ,'N' ,'O' ,'P' , +'Q' ,'R' ,'S' ,'T' ,'U' ,'V' ,'W' ,'X' ,'Y' ,'Z' ,'a' ,'b' ,'c' , +'d' ,'e' ,'f' ,'g' ,'h' ,'i' ,'j' ,'k' ,'l' ,'m' ,'n' ,'o' ,'p' , +'q' ,'r' ,'s' ,'t' ,'u' ,'v' ,'w' ,'x' ,'y' ,'z' ,'+' ,'.' }; + +char *stk_image_to_xpm (const unsigned char *img,unsigned int len, +enum stk_img_scheme scheme) +{ +guint8 width, height; +unsigned int ncolors, nbits, entry, cpp; +unsigned int i, j; +int bit, k; +unsigned short clut_offset =0 ; +guint8 *clut; +GString *xpm; +unsigned int pos =0 ; +const char xpm_header[] ="/* XPM */\n" ; +const char declaration[] ="static char *xpm[] = {\n" ; +char c[3 ]; + +if (img ==NULL ) +return NULL ; + +/* sanity check length */ +if (len <3 ) +return NULL ; + + width = img[pos++]; + height = img[pos++]; + +if (scheme ==STK_IMG_SCHEME_BASIC ) { + nbits =1 ; + ncolors =2 ; + +if (pos + ((width * height)/8 ) > len) +return NULL ; + }else { +/* sanity check length */ +if (pos +4 > len) +return NULL ; + + nbits = img[pos++]; + ncolors = img[pos++]; + +/* the value of zero should be interpreted as 256 */ +if (ncolors ==0 ) + ncolors =256 ; + + clut_offset = img[pos++] <<8 ; + clut_offset |= img[pos++]; + +if ((clut_offset + (ncolors *3 ) > len) || + (pos + (width * height * nbits)/8 ) > clut_offset) +return NULL ; + } + +/* determine the number of chars need to represent the pixel */ + cpp = ncolors >64 ?2 :1 ; + +/* +* space needed: +* header line +* declaration and beginning of assignment line +* values - max length of 19 +* colors - ncolors * (cpp + whitespace + deliminators + color) +* pixels - width * height * cpp + height deliminators "",\n +* end of assignment - 2 chars "};" +*/ + xpm =g_string_sized_new (strlen (xpm_header) +strlen (declaration) + +19 + ((cpp +14 ) * ncolors) + + (width * height * cpp) + (4 * height) +2 ); +if (xpm ==NULL ) +return NULL ; + +/* add header, declaration, values */ +g_string_append (xpm, xpm_header); +g_string_append (xpm, declaration); +g_string_append_printf (xpm,"\"%d %d %d %d\",\n" , width, height, + ncolors, cpp); + +/* create colors */ +if (scheme ==STK_IMG_SCHEME_BASIC ) { +g_string_append (xpm,"\"0\tc #000000\",\n" ); +g_string_append (xpm,"\"1\tc #FFFFFF\",\n" ); + }else { + clut = (guint8 *) &img[clut_offset]; + +for (i =0 ; i < ncolors; i++) { +/* lookup char representation of this number */ +if (ncolors >64 ) { + c[0 ] = chars_table[i /64 ]; + c[1 ] = chars_table[i %64 ]; + c[2 ] ='\0' ; + }else { + c[0 ] = chars_table[i %64 ]; + c[1 ] ='\0' ; + } + +if ((i == (ncolors -1 )) && + scheme ==STK_IMG_SCHEME_TRANSPARENCY ) +g_string_append_printf (xpm, +"\"%s\tc None\",\n" , c); +else +g_string_append_printf (xpm, +"\"%s\tc #%02hhX%02hhX%02hhX\",\n" , + c, clut[0 ], clut[1 ], clut[2 ]); + clut +=3 ; + } + } + +/* height rows of width pixels */ + k =7 ; +for (i =0 ; i < height; i++) { +g_string_append (xpm,"\"" ); +for (j =0 ; j < width; j++) { + entry =0 ; +for (bit = nbits -1 ; bit >=0 ; bit--) { + entry |= (img[pos] >> k &0x1 ) << bit; + k--; + +/* see if we crossed a byte boundary */ +if (k <0 ) { + k =7 ; + pos++; + } + } + +/* lookup char representation of this number */ +if (ncolors >64 ) { + c[0 ] = chars_table[entry /64 ]; + c[1 ] = chars_table[entry %64 ]; + c[2 ] ='\0' ; + }else { + c[0 ] = chars_table[entry %64 ]; + c[1 ] ='\0' ; + } +g_string_append_printf (xpm,"%s" , c); + } +g_string_append (xpm,"\",\n" ); + } +g_string_append (xpm,"};" ); + +/* Caller must free char data */ +return g_string_free (xpm, FALSE); +}
src/stkutil.h +8 -0
@@ -557,6 +557,12 @@ enum stk_me_status {STK_ME_STATUS_NOT_IDLE =0x01 }; +enum stk_img_scheme { +STK_IMG_SCHEME_BASIC =0x11 , +STK_IMG_SCHEME_COLOR =0x21 , +STK_IMG_SCHEME_TRANSPARENCY =0x22 , +}; +/* For data object that only has a byte array with undetermined length */ struct stk_common_byte_array {unsigned char *array;
@@ -1644,3 +1650,5 @@ const unsigned char *stk_pdu_from_envelope(const struct stk_envelope *envelope, unsigned int *out_length);char *stk_text_to_html (const char *text,const unsigned short *attrs,int num_attrs); +char *stk_image_to_xpm (const unsigned char *img,unsigned int len, +enum stk_img_scheme scheme);
Signature · 1 line
1.6.6.1
PATCH 2/2 test-stkutil: unit test for img to xpm converter
In reply to Kristen Carlson Accardi--- unit/test-stkutil.c | 240 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 240 insertions(+), 0 deletions(-)
unit/test-stkutil.c +240 -0
@@ -22693,6 +22693,233 @@ static void test_html_attr(gconstpointer data)check_text_attr_html (&test->text_attr, test->text, test->html); } +struct img_xpm_test { +const unsigned char *img; +unsigned int len; +guint8 scheme; +char *xpm; +}; +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x99 ,0x99 , +0xA5 ,0xC3 ,0xFF }; + +static struct img_xpm_test xpm_test_1 = { + .img = img1, + .len =sizeof (img1), + .scheme =STK_IMG_SCHEME_BASIC , + .xpm ="/* XPM */\n" +"static char *xpm[] = {\n" +"\"5 5 2 1\",\n" +"\"0 c #000000\",\n" +"\"1 c #FFFFFF\",\n" +"\"11111\",\n" +"\"11011\",\n" +"\"10101\",\n" +"\"11011\",\n" +"\"11111\",\n" +"};" , +}; + +static struct img_xpm_test xpm_test_2 = { + .img = img2, + .len =sizeof (img2), + .scheme =STK_IMG_SCHEME_COLOR , + .xpm ="/* XPM */\n" +"static char *xpm[] = {\n" +"\"8 8 3 1\",\n" +"\"0 c #FF0000\",\n" +"\"1 c #00FF00\",\n" +"\"2 c #0000FF\",\n" +"\"22222222\",\n" +"\"20000002\",\n" +"\"20111002\",\n" +"\"20011002\",\n" +"\"20011002\",\n" +"\"20011102\",\n" +"\"20000002\",\n" +"\"22222222\",\n" +"};" , +}; + +static struct img_xpm_test xpm_test_3 = { + .img = img3, + .len =sizeof (img3), + .scheme =STK_IMG_SCHEME_BASIC , + .xpm ="/* XPM */\n" +"static char *xpm[] = {\n" +"\"46 40 2 1\",\n" +"\"0 c #000000\",\n" +"\"1 c #FFFFFF\",\n" +"\"0000000000000000000000000000000000000000000000\",\n" +"\"0000000000000000011111111110000000000000000000\",\n" +"\"0000000000000000111111111111000000000000000000\",\n" +"\"0000000000000001110111111111100000000000000000\",\n" +"\"0000000000000001101111111111100000000000000000\",\n" +"\"0000000000000001101111111111100000000000000000\",\n" +"\"0000000000000001101000000011100000000000000000\",\n" +"\"0000000000000001101011111101101011110000000000\",\n" +"\"0000000000000001101011111101100000111000000000\",\n" +"\"0000000000000001101011111101100000001000000000\",\n" +"\"0000000000000001101011111101100000000100000000\",\n" +"\"0000000000000001101011111101100000000010000000\",\n" +"\"0000000000000001101000000001111100000010000000\",\n" +"\"0000000000000001101111111111100100000001000000\",\n" +"\"0000000000000001101111111111100100000001000000\",\n" +"\"0000000000000001101101111011100100000001000000\",\n" +"\"0000000000000001101111111111100100000001000000\",\n" +"\"0000000000011011111111111111100100000001000000\",\n" +"\"0000000000000001101111111111100100000001000000\",\n" +"\"0000000000000001101111111111100100000001000000\",\n" +"\"0000000000000001101111111111100100000001000000\",\n" +"\"0000000000000001101111111111100110000001000000\",\n" +"\"0000000000000001101111111111100010000001000000\",\n" +"\"0000000000000001101111111111100010000001000000\",\n" +"\"0000000000000001101111111111100001100110000000\",\n" +"\"0000000000000001101111111111100000111100000000\",\n" +"\"0000000000000001101111111111100000001000000000\",\n" +"\"0000000000000001111111111111100000000000000000\",\n" +"\"0000000000000011000000000000110000000000000000\",\n" +"\"0000000000000111111111111111111000000000000000\",\n" +"\"0000000000000000000000000000000000000000000000\",\n" +"\"0000000000000000000000000000000000000000000000\",\n" +"\"0000000000000000000000000000000000000000000000\",\n" +"\"0000011100001000010000100001000100111011100000\",\n" +"\"0000010010001100010000110001100100100010000000\",\n" +"\"0000010010010100010001010001010101000010000000\",\n" +"\"0000011100010100010001010001010101000011100000\",\n" +"\"0000010010011100010001110001001101000010000000\",\n" +"\"0000010010100010010010001001001100100010000000\",\n" +"\"0000011100100010011110001001000100111011100000\",\n" +"};" , +}; + +static struct img_xpm_test xpm_test_4 = { + .img = img4, + .len =sizeof (img4), + .scheme =STK_IMG_SCHEME_BASIC , + .xpm ="/* XPM */\n" +"static char *xpm[] = {\n" +"\"24 16 2 1\",\n" +"\"0 c #000000\",\n" +"\"1 c #FFFFFF\",\n" +"\"111111111111111111111111\",\n" +"\"100000000000000000000001\",\n" +"\"100000000000000000000001\",\n" +"\"100000000000000000000001\",\n" +"\"100011110011110011110001\",\n" +"\"100010010010000010000001\",\n" +"\"100010010010000010000001\",\n" +"\"100010010010000011110001\",\n" +"\"100010010010000000010001\",\n" +"\"100010010010000000010001\",\n" +"\"100010010010000000010001\",\n" +"\"100011110011110011110001\",\n" +"\"100000000000000000000001\",\n" +"\"100000000000000000000001\",\n" +"\"100000000000000000000001\",\n" +"\"111111111111111111111111\",\n" +"};" , +}; + +static struct img_xpm_test xpm_test_5 = { + .img = img5, + .len =sizeof (img5), + .scheme =STK_IMG_SCHEME_BASIC , + .xpm ="/* XPM */\n" +"static char *xpm[] = {\n" +"\"8 8 2 1\",\n" +"\"0 c #000000\",\n" +"\"1 c #FFFFFF\",\n" +"\"11111111\",\n" +"\"00000011\",\n" +"\"10100101\",\n" +"\"10011001\",\n" +"\"10011001\",\n" +"\"10100101\",\n" +"\"11000011\",\n" +"\"11111111\",\n" +"};" , +}; + +static struct img_xpm_test xpm_test_6 = { + .img = img2, + .len =sizeof (img2), + .scheme =STK_IMG_SCHEME_TRANSPARENCY , + .xpm ="/* XPM */\n" +"static char *xpm[] = {\n" +"\"8 8 3 1\",\n" +"\"0 c #FF0000\",\n" +"\"1 c #00FF00\",\n" +"\"2 c None\",\n" +"\"22222222\",\n" +"\"20000002\",\n" +"\"20111002\",\n" +"\"20011002\",\n" +"\"20011002\",\n" +"\"20011102\",\n" +"\"20000002\",\n" +"\"22222222\",\n" +"};" , +}; + +static void test_img_to_xpm (gconstpointer data) +{ +const struct img_xpm_test *test = data; +char *xpm; + + xpm =stk_image_to_xpm (test->img, test->len, test->scheme); + +g_assert (memcmp (xpm, test->xpm,strlen (test->xpm)) ==0 ); +g_free (xpm); +} +int main (int argc,char **argv) {g_test_init (&argc, &argv,NULL );
@@ -24727,5 +24954,18 @@ int main(int argc, char **argv)g_test_add_data_func ("/teststk/HTML Attribute Test 4" , &html_attr_data_4, test_html_attr); +g_test_add_data_func ("/teststk/IMG to XPM Test 1" , + &xpm_test_1, test_img_to_xpm); +g_test_add_data_func ("/teststk/IMG to XPM Test 2" , + &xpm_test_2, test_img_to_xpm); +g_test_add_data_func ("/teststk/IMG to XPM Test 3" , + &xpm_test_3, test_img_to_xpm); +g_test_add_data_func ("/teststk/IMG to XPM Test 4" , + &xpm_test_4, test_img_to_xpm); +g_test_add_data_func ("/teststk/IMG to XPM Test 5" , + &xpm_test_5, test_img_to_xpm); +g_test_add_data_func ("/teststk/IMG to XPM Test 6" , + &xpm_test_6, test_img_to_xpm); +return g_test_run (); }
Signature · 1 line
1.6.6.1
Re: [PATCH 1/2] stkutil: convert img to xpm
In reply to Kristen Carlson AccardiHi Kristen,
Quoted 42 lines · Kristen Carlson Accardi
--- src/stkutil.c | 148 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/stkutil.h | 8 +++ 2 files changed, 156 insertions(+), 0 deletions(-)src/stkutil.c +29
-0@@ -6076,3 +6076,151 @@ char *stk_text_to_html(const char *utf8,/* return characters from string. Caller must free char data */ return g_string_free (string, FALSE); } + +static const char chars_table[] = { +'0' ,'1' ,'2' ,'3' ,'4' ,'5' ,'6' ,'7' ,'8' ,'9' ,'A' ,'B' ,'C' , +'D' ,'E' ,'F' ,'G' ,'H' ,'I' ,'J' ,'K' ,'L' ,'M' ,'N' ,'O' ,'P' , +'Q' ,'R' ,'S' ,'T' ,'U' ,'V' ,'W' ,'X' ,'Y' ,'Z' ,'a' ,'b' ,'c' , +'d' ,'e' ,'f' ,'g' ,'h' ,'i' ,'j' ,'k' ,'l' ,'m' ,'n' ,'o' ,'p' , +'q' ,'r' ,'s' ,'t' ,'u' ,'v' ,'w' ,'x' ,'y' ,'z' ,'+' ,'.' }; + +char *stk_image_to_xpm (const unsigned char *img,unsigned int len, +enum stk_img_scheme scheme) +{ +guint8 width, height; +unsigned int ncolors, nbits, entry, cpp; +unsigned int i, j; +int bit, k; +unsigned short clut_offset =0 ; +guint8 *clut; +GString *xpm; +unsigned int pos =0 ; +const char xpm_header[] ="/* XPM */\n" ; +const char declaration[] ="static char *xpm[] = {\n" ; +char c[3 ]; + +if (img ==NULL ) +return NULL ; + +/* sanity check length */ +if (len <3 ) +return NULL ;
src/stkutil.c char *stk_text_to_html(const char *utf8, · line 6117
Quoted 10 lines
src/stkutil.c +10
-0+ + width = img[pos++]; + height = img[pos++]; + +if (scheme ==STK_IMG_SCHEME_BASIC ) { + nbits =1 ; + ncolors =2 ; + +if (pos + ((width * height)/8 ) > len) +return NULL ;
You probably want (pos + (width * height + 7) / 8) > len here. Also, please keep doc/coding-style.txt Section M3 in mind here.
Quoted 7 lines
src/stkutil.c +7
-0+ }else { +/* sanity check length */ +if (pos +4 > len) +return NULL ; + + nbits = img[pos++]; + ncolors = img[pos++];
src/stkutil.c char *stk_text_to_html(const char *utf8, · line 6134
Quoted 10 lines
src/stkutil.c +10
-0+ +/* the value of zero should be interpreted as 256 */ +if (ncolors ==0 ) + ncolors =256 ; + + clut_offset = img[pos++] <<8 ; + clut_offset |= img[pos++]; + +if ((clut_offset + (ncolors *3 ) > len) || + (pos + (width * height * nbits)/8 ) > clut_offset)
Please keep in mind doc/coding-style.txt Section M4. Sometimes it is better to separate the || conditions into two separate ifs to satisfy this rule.
Also, you're off by 1 again here. You probably want (width * height * nbits + 7) / 8.
This also brings up another point. You're assuming that the caller is appending the CLUT right after the image data and massaging the clut offset appropriately. This is a really bad idea since the caller will have to do some significant pre-processing.
You can handle this in one of two ways:
1. Assume the calling logic will read the entire image file before calling this function. In this case, modifying the signature as follows might be a good idea:
char *stk_image_to_xpm(const unsigned char *file,
unsigned short file_len, enum stk_img_scheme scheme, unsigned short img_offset, unsigned short img_len);
2. Assume the calling logic is clever and will optimize reading of the file, including peeking into the image header to determine the where the CLUT is located and reading it. In this case you can either reuse the signature from 1 above, or come up with something else.
Remember, reading from the SIM is extremely slow, so any and all clever optimization tricks are definitely wanted.
Quoted 73 lines
src/stkutil.c +73
-0+return NULL ; + } + +/* determine the number of chars need to represent the pixel */ + cpp = ncolors >64 ?2 :1 ; + +/* +* space needed: +* header line +* declaration and beginning of assignment line +* values - max length of 19 +* colors - ncolors * (cpp + whitespace + deliminators + color) +* pixels - width * height * cpp + height deliminators "",\n +* end of assignment - 2 chars "};" +*/ + xpm =g_string_sized_new (strlen (xpm_header) +strlen (declaration) + +19 + ((cpp +14 ) * ncolors) + + (width * height * cpp) + (4 * height) +2 ); +if (xpm ==NULL ) +return NULL ; + +/* add header, declaration, values */ +g_string_append (xpm, xpm_header); +g_string_append (xpm, declaration); +g_string_append_printf (xpm,"\"%d %d %d %d\",\n" , width, height, + ncolors, cpp); + +/* create colors */ +if (scheme ==STK_IMG_SCHEME_BASIC ) { +g_string_append (xpm,"\"0\tc #000000\",\n" ); +g_string_append (xpm,"\"1\tc #FFFFFF\",\n" ); + }else { + clut = (guint8 *) &img[clut_offset]; + +for (i =0 ; i < ncolors; i++) { +/* lookup char representation of this number */ +if (ncolors >64 ) { + c[0 ] = chars_table[i /64 ]; + c[1 ] = chars_table[i %64 ]; + c[2 ] ='\0' ; + }else { + c[0 ] = chars_table[i %64 ]; + c[1 ] ='\0' ; + } + +if ((i == (ncolors -1 )) && + scheme ==STK_IMG_SCHEME_TRANSPARENCY ) +g_string_append_printf (xpm, +"\"%s\tc None\",\n" , c); +else +g_string_append_printf (xpm, +"\"%s\tc #%02hhX%02hhX%02hhX\",\n" , + c, clut[0 ], clut[1 ], clut[2 ]); + clut +=3 ; + } + } + +/* height rows of width pixels */ + k =7 ; +for (i =0 ; i < height; i++) { +g_string_append (xpm,"\"" ); +for (j =0 ; j < width; j++) { + entry =0 ; +for (bit = nbits -1 ; bit >=0 ; bit--) { + entry |= (img[pos] >> k &0x1 ) << bit; + k--; + +/* see if we crossed a byte boundary */ +if (k <0 ) { + k =7 ; + pos++; + } + }
src/stkutil.c char *stk_text_to_html(const char *utf8, · line 6217
Quoted 10 lines
src/stkutil.c +10
-0+ +/* lookup char representation of this number */ +if (ncolors >64 ) { + c[0 ] = chars_table[entry /64 ]; + c[1 ] = chars_table[entry %64 ]; + c[2 ] ='\0' ; + }else { + c[0 ] = chars_table[entry %64 ]; + c[1 ] ='\0' ; + }
There should be an empty line here
src/stkutil.c char *stk_text_to_html(const char *utf8, · line 6219
Quoted 2 lines
src/stkutil.c +2
-0+g_string_append_printf (xpm,"%s" , c); + }
And another empty line here.
src/stkutil.c char *stk_text_to_html(const char *utf8, · line 6221
Quoted 2 lines
src/stkutil.c +2
-0+g_string_append (xpm,"\",\n" ); + }
And another empty line here.
Quoted 23 lines
src/stkutil.c +5
-0+g_string_append (xpm,"};" ); + +/* Caller must free char data */ +return g_string_free (xpm, FALSE); +}src/stkutil.h +6
-0@@ -557,6 +557,12 @@ enum stk_me_status {STK_ME_STATUS_NOT_IDLE =0x01 }; +enum stk_img_scheme { +STK_IMG_SCHEME_BASIC =0x11 , +STK_IMG_SCHEME_COLOR =0x21 , +STK_IMG_SCHEME_TRANSPARENCY =0x22 , +}; +/* For data object that only has a byte array with undetermined length */ struct stk_common_byte_array {unsigned char *array;
src/stkutil.h const unsigned char *stk_pdu_from_envelope(const struct stk_envelope *envelope, · line 1654
Quoted 5 lines
src/stkutil.h +2
-0@@ -1644,3 +1650,5 @@ const unsigned char *stk_pdu_from_envelope(const struct stk_envelope *envelope, unsigned int *out_length);char *stk_text_to_html (const char *text,const unsigned short *attrs,int num_attrs); +char *stk_image_to_xpm (const unsigned char *img,unsigned int len, +enum stk_img_scheme scheme);
Regards,
-Denis
Re: [PATCH 1/2] stkutil: convert img to xpm
Quoted 18 lines · On Fri, 23 Jul 2010 15:03:59 -0500 Denis Kenzior <[email protected]> wrote:
This also brings up another point. You're assuming that the caller is appending the CLUT right after the image data and massaging the clut offset appropriately. This is a really bad idea since the caller will have to do some significant pre-processing.
You can handle this in one of two ways:
1. Assume the calling logic will read the entire image file before calling this function. In this case, modifying the signature as follows might be a good idea:
char *stk_image_to_xpm(const unsigned char *file,
unsigned shortfile_len, enumstk_img_schemescheme, unsigned shortimg_offset, unsigned shortimg_len);
Quoted 7 lines
2. Assume the calling logic is clever and will optimize reading of the file, including peeking into the image header to determine the where the CLUT is located and reading it. In this case you can either reuse the signature from 1 above, or come up with something else.
Remember, reading from the SIM is extremely slow, so any and all clever optimization tricks are definitely wanted.
So, is it likely given normal usage that we'll access an image a single image at a time, or is it more likely that we'll access a bunch of images all at once? It may be better to read an entire image data file (with multiple images) and keep it cached if we are likely to immediately need the other images. In which case I'd be inclined to just pass in the entire data image file and the offset like you have above. If we are only likely to use a single image for any given length of time, then it seems better to have the caller be smart and pass us the clut.
Re: [PATCH 1/2] stkutil: convert img to xpm
Hi Kristen,
Quoted 28 lines · Kristen Carlson Accardi
Quoted 18 lines · On Fri, 23 Jul 2010 15:03:59 -0500 Denis Kenzior <[email protected]> wrote:
This also brings up another point. You're assuming that the caller is appending the CLUT right after the image data and massaging the clut offset appropriately. This is a really bad idea since the caller will have to do some significant pre-processing.
You can handle this in one of two ways:
1. Assume the calling logic will read the entire image file before calling this function. In this case, modifying the signature as follows might be a good idea:
char *stk_image_to_xpm(const unsigned char *file,
unsigned shortfile_len, enumstk_img_schemescheme, unsigned shortimg_offset, unsigned shortimg_len);Quoted 7 lines
2. Assume the calling logic is clever and will optimize reading of the file, including peeking into the image header to determine the where the CLUT is located and reading it. In this case you can either reuse the signature from 1 above, or come up with something else.
Remember, reading from the SIM is extremely slow, so any and all clever optimization tricks are definitely wanted.
Quoted 8 lines
So, is it likely given normal usage that we'll access an image a single image at a time, or is it more likely that we'll access a bunch of images all at once? It may be better to read an entire image data file (with multiple images) and keep it cached if we are likely to immediately need the other images. In which case I'd be inclined to just pass in the entire data image file and the offset like you have above. If we are only likely to use a single image for any given length of time, then it seems better to have the caller be smart and pass us the clut.
The problem is we just don't know, so we have to assume the worst case.
Anything that minimizes the number of reads is a good thing (TM).
For instance, you might have couple of dozen images from EFimg dispersed among multiple EFiidf files. Each EFiidf file might be 65K in length, but EFimg files might only refer to about 10k from all of them.
It is perfectly OK for EFiidf to contain mostly garbage (e.g. for future updates, installation of new SIM Toolkit applications on the SIM + associated image data, etc) So you simply can't assume any sort of packing or efficient storage use.
In this case, a clever algorithm that minimizes the number of SIM fetches is needed.
Regards,
-Denis
Re: [PATCH 1/2] stkutil: convert img to xpm
Quoted 52 lines · On Fri, 23 Jul 2010 16:03:16 -0500 Denis Kenzior <[email protected]> wrote:
Hi Kristen,
Quoted 28 lines · Kristen Carlson Accardi
Quoted 18 lines · On Fri, 23 Jul 2010 15:03:59 -0500 Denis Kenzior <[email protected]> wrote:
This also brings up another point. You're assuming that the caller is appending the CLUT right after the image data and massaging the clut offset appropriately. This is a really bad idea since the caller will have to do some significant pre-processing.
You can handle this in one of two ways:
1. Assume the calling logic will read the entire image file before calling this function. In this case, modifying the signature as follows might be a good idea:
char *stk_image_to_xpm(const unsigned char *file,
unsigned shortfile_len, enumstk_img_schemescheme, unsigned shortimg_offset, unsigned shortimg_len);Quoted 7 lines
2. Assume the calling logic is clever and will optimize reading of the file, including peeking into the image header to determine the where the CLUT is located and reading it. In this case you can either reuse the signature from 1 above, or come up with something else.
Remember, reading from the SIM is extremely slow, so any and all clever optimization tricks are definitely wanted.
Quoted 8 lines
So, is it likely given normal usage that we'll access an image a single image at a time, or is it more likely that we'll access a bunch of images all at once? It may be better to read an entire image data file (with multiple images) and keep it cached if we are likely to immediately need the other images. In which case I'd be inclined to just pass in the entire data image file and the offset like you have above. If we are only likely to use a single image for any given length of time, then it seems better to have the caller be smart and pass us the clut.
The problem is we just don't know, so we have to assume the worst case.
Anything that minimizes the number of reads is a good thing (TM).For instance, you might have couple of dozen images from EFimg dispersed among multiple EFiidf files. Each EFiidf file might be 65K in length, but EFimg files might only refer to about 10k from all of them.
It is perfectly OK for EFiidf to contain mostly garbage (e.g. for future updates, installation of new SIM Toolkit applications on the SIM + associated image data, etc) So you simply can't assume any sort of packing or efficient storage use.
Quoted 5 lines
In this case, a clever algorithm that minimizes the number of SIM fetches is needed.
Regards,
-Denis
If that is the case then I propose we assume a smart algorithm fetched our data for us and change the signature to something like this:
unsigned char *stk_image_to_xpm(const unsigned char *image_body,
enum stk_image_scheme scheme, unsigned int height,
unsigned int width, unsigned int ncolors, const unsigned char *clut);
We would assume that the caller has done all the sanity checking on the file and handed us a clut of appropriate size as well.
Re: [PATCH 1/2] stkutil: convert img to xpm
Hi Kristen,
Quoted 64 lines · Kristen Carlson Accardi
Quoted 52 lines · On Fri, 23 Jul 2010 16:03:16 -0500 Denis Kenzior <[email protected]> wrote:
Hi Kristen,
Quoted 28 lines · Kristen Carlson Accardi
Quoted 18 lines · On Fri, 23 Jul 2010 15:03:59 -0500 Denis Kenzior <[email protected]> wrote:
This also brings up another point. You're assuming that the caller is appending the CLUT right after the image data and massaging the clut offset appropriately. This is a really bad idea since the caller will have to do some significant pre-processing.
You can handle this in one of two ways:
1. Assume the calling logic will read the entire image file before calling this function. In this case, modifying the signature as follows might be a good idea:
char *stk_image_to_xpm(const unsigned char *file,
unsigned shortfile_len, enumstk_img_schemescheme, unsigned shortimg_offset, unsigned shortimg_len);Quoted 7 lines
2. Assume the calling logic is clever and will optimize reading of the file, including peeking into the image header to determine the where the CLUT is located and reading it. In this case you can either reuse the signature from 1 above, or come up with something else.
Remember, reading from the SIM is extremely slow, so any and all clever optimization tricks are definitely wanted.
Quoted 8 lines
So, is it likely given normal usage that we'll access an image a single image at a time, or is it more likely that we'll access a bunch of images all at once? It may be better to read an entire image data file (with multiple images) and keep it cached if we are likely to immediately need the other images. In which case I'd be inclined to just pass in the entire data image file and the offset like you have above. If we are only likely to use a single image for any given length of time, then it seems better to have the caller be smart and pass us the clut.
The problem is we just don't know, so we have to assume the worst case.
Anything that minimizes the number of reads is a good thing (TM).For instance, you might have couple of dozen images from EFimg dispersed among multiple EFiidf files. Each EFiidf file might be 65K in length, but EFimg files might only refer to about 10k from all of them.
It is perfectly OK for EFiidf to contain mostly garbage (e.g. for future updates, installation of new SIM Toolkit applications on the SIM + associated image data, etc) So you simply can't assume any sort of packing or efficient storage use.
Quoted 5 lines
In this case, a clever algorithm that minimizes the number of SIM fetches is needed.
Regards,
-DenisIf that is the case then I propose we assume a smart algorithm fetched our data for us and change the signature to something like this:
Quoted 8 lines
unsigned char *stk_image_to_xpm(const unsigned char
*image_body,
enumstk_image_schemescheme, unsigned int height,
unsigned int width, unsigned int ncolors, const unsigned char *clut);We would assume that the caller has done all the sanity checking on the file and handed us a clut of appropriate size as well.
You sure you want unsigned char * as the return and not char *?
Also, you might want to change unsigned ints to unsigned chars. The image size and CLUT size can never be more than 256.
Regards,
-Denis
Re: [PATCH 1/2] stkutil: convert img to xpm
Quoted 73 lines · On Fri, 23 Jul 2010 16:46:51 -0500 Denis Kenzior <[email protected]> wrote:
Hi Kristen,
Quoted 70 lines · Kristen Carlson Accardi
Quoted 52 lines · On Fri, 23 Jul 2010 16:03:16 -0500 Denis Kenzior <[email protected]> wrote:
Hi Kristen,
Quoted 28 lines · Kristen Carlson Accardi
Quoted 18 lines · On Fri, 23 Jul 2010 15:03:59 -0500 Denis Kenzior <[email protected]> wrote:
This also brings up another point. You're assuming that the caller is appending the CLUT right after the image data and massaging the clut offset appropriately. This is a really bad idea since the caller will have to do some significant pre-processing.
You can handle this in one of two ways:
1. Assume the calling logic will read the entire image file before calling this function. In this case, modifying the signature as follows might be a good idea:
char *stk_image_to_xpm(const unsigned char *file,
unsigned shortfile_len, enumstk_img_schemescheme, unsigned shortimg_offset, unsigned shortimg_len);Quoted 7 lines
2. Assume the calling logic is clever and will optimize reading of the file, including peeking into the image header to determine the where the CLUT is located and reading it. In this case you can either reuse the signature from 1 above, or come up with something else.
Remember, reading from the SIM is extremely slow, so any and all clever optimization tricks are definitely wanted.
Quoted 8 lines
So, is it likely given normal usage that we'll access an image a single image at a time, or is it more likely that we'll access a bunch of images all at once? It may be better to read an entire image data file (with multiple images) and keep it cached if we are likely to immediately need the other images. In which case I'd be inclined to just pass in the entire data image file and the offset like you have above. If we are only likely to use a single image for any given length of time, then it seems better to have the caller be smart and pass us the clut.
The problem is we just don't know, so we have to assume the worst case.
Anything that minimizes the number of reads is a good thing (TM).For instance, you might have couple of dozen images from EFimg dispersed among multiple EFiidf files. Each EFiidf file might be 65K in length, but EFimg files might only refer to about 10k from all of them.
It is perfectly OK for EFiidf to contain mostly garbage (e.g. for future updates, installation of new SIM Toolkit applications on the SIM + associated image data, etc) So you simply can't assume any sort of packing or efficient storage use.
Quoted 5 lines
In this case, a clever algorithm that minimizes the number of SIM fetches is needed.
Regards,
-DenisIf that is the case then I propose we assume a smart algorithm fetched our data for us and change the signature to something like this:
unsigned char *stk_image_to_xpm(const unsigned char
*image_body,
enumstk_image_schemescheme, unsigned int height,
unsigned int width, unsigned int ncolors, const unsigned char *clut);
Quoted 4 lines
Quoted 2 lines
We would assume that the caller has done all the sanity checking on the file and handed us a clut of appropriate size as well.
You sure you want unsigned char * as the return and not char *?
Oops - no, I typed that wrong.
Quoted 4 lines
Also, you might want to change unsigned ints to unsigned chars. The image size and CLUT size can never be more than 256.
The image size can be greater than 256, but I think you mean the value of height, width, and ncolors can never be greater than 256, which is true.
Re: [PATCH 1/2] stkutil: convert img to xpm
Hi Kristen,
Quoted 6 lines
Quoted 3 lines
Also, you might want to change unsigned ints to unsigned chars. The image size and CLUT size can never be more than 256.
The image size can be greater than 256, but I think you mean the value of height, width, and ncolors can never be greater than 256, which is true.
Sorry, yes that is what I meant.
Regards,
-Denis