31 #include <tqcstring.h>
35 #include <tqpainter.h>
40 #define GEN(g) F[h] ^= G.g[k]; break
43 #define DIGITS (PIXELS / BITSPERDIG)
45 #define WORDSPERLINE (WIDTH / DIGSPERWORD / BITSPERDIG)
50 #define FIRSTPRINT '!'
52 #define NUMPRINTS (LASTPRINT - FIRSTPRINT + 1)
60 #define COMP unsigned long
61 #define WORDCARRY (1 << BITSPERWORD)
62 #define WORDMASK (WORDCARRY - 1)
67 #define ERR_INTERNAL -2
73 #define MAX_XFACE_LENGTH 2048
86 TQString KXFace::fromImage( const TQImage &image )
91 TQImage scaledImg = image.smoothScale( 48, 48 );
93 TQBuffer buffer( ba );
94 buffer.open( IO_WriteOnly );
95 scaledImg.save( &buffer, "XBM" );
97 xbm.remove( 0, xbm.find( "{" ) + 1 );
98 xbm.truncate( xbm.find( "}" ) );
104 TQCString tmp = TQCString( xbm.latin1() );
105 uint len = tmp.length();
106 for( uint i=0; i<len; ++i )
110 case '1': tmp[i] = '8'; break;
111 case '2': tmp[i] = '4'; break;
112 case '3': tmp[i] = 'c'; break;
113 case '4': tmp[i] = '2'; break;
114 case '5': tmp[i] = 'a'; break;
115 case '7': tmp[i] = 'e'; break;
116 case '8': tmp[i] = '1'; break;
118 case 'a': tmp[i] = '5'; break;
120 case 'b': tmp[i] = 'd'; break;
122 case 'c': tmp[i] = '3'; break;
124 case 'd': tmp[i] = 'b'; break;
126 case 'e': tmp[i] = '7'; break;
135 tmp.replace( TQRegExp( "(\\w{12})" ), "\\1\n" );
136 tmp.replace( TQRegExp( "(\\w{4})" ), "0x\\1," );
138 char *fbuf = ( char *)malloc( len + 1 );
139 strncpy( fbuf, ( const char *)tmp, len );
141 if ( !( status = setjmp( comp_env ) ) )
147 TQString ret( fbuf );
153 TQImage KXFace::toImage( const TQString &xface)
155 if ( xface.length() > MAX_XFACE_LENGTH )
158 char *fbuf = ( char *)malloc( MAX_XFACE_LENGTH );
159 memset( fbuf, '\0', MAX_XFACE_LENGTH );
160 strncpy( fbuf, xface.latin1(), xface.length() );
162 if ( !( status = setjmp( comp_env ) ) )
170 p.loadFromData( img, "XBM" );
178 void KXFace::RevPush( const Prob *p)
180 if (NumProbs >= PIXELS * 2 - 1)
181 longjmp(comp_env, ERR_INTERNAL);
182 ProbBuf[NumProbs++] = (Prob *) p;
185 void KXFace::BigPush(Prob *p)
187 static unsigned char tmp;
189 BigDiv(p->p_range, &tmp);
191 BigAdd(tmp + p->p_offset);
194 int KXFace::BigPop( const Prob *p)
196 static unsigned char tmp;
201 while ((tmp < p->p_offset) || (tmp >= p->p_range + p->p_offset))
207 BigAdd(tmp - p->p_offset);
215 void KXFace::BigDiv( unsigned char a, unsigned char *r)
222 if ((a == 1) || (B.b_words == 0))
240 w = B.b_word + (i = B.b_words);
248 *w = ( unsigned char)(d & WORDMASK);
251 if (B.b_word[B.b_words - 1] == 0)
257 void KXFace::BigMul( unsigned char a)
264 if ((a == 1) || (B.b_words == 0))
268 if ((i = B.b_words++) >= MAXWORDS - 1)
269 longjmp(comp_env, ERR_INTERNAL);
284 c += (COMP)*w * (COMP)a;
285 *(w++) = ( unsigned char)(c & WORDMASK);
290 if (B.b_words++ >= MAXWORDS)
291 longjmp(comp_env, ERR_INTERNAL);
292 *w = (COMP)(c & WORDMASK);
298 void KXFace::BigAdd( unsigned char a)
310 while ((i < B.b_words) && c)
313 *w++ = ( unsigned char)(c & WORDMASK);
317 if ((i == B.b_words) && c)
319 if (B.b_words++ >= MAXWORDS)
320 longjmp(comp_env, ERR_INTERNAL);
321 *w = (COMP)(c & WORDMASK);
325 void KXFace::BigClear()
330 TQCString KXFace::WriteFace()
333 int i, j, bits, digits, words;
334 int digsperword = DIGSPERWORD;
335 int wordsperline = WORDSPERLINE;
336 TQCString t( "#define noname_width 48\n#define noname_height 48\nstatic char noname_bits[] = {\n " );
340 bits = digits = words = i = 0;
341 t.resize( MAX_XFACE_LENGTH );
344 while ( s < F + PIXELS )
346 if ( ( bits == 0 ) && ( digits == 0 ) )
352 i = ( i >> 1 ) | 0x8;
355 if ( ++bits == BITSPERDIG )
358 t[j-( ( digits & 1 ) * 2 )] = *(i + HexDigits);
360 if ( ++digits == digsperword )
362 if ( s >= F + PIXELS )
366 if ( ++words == wordsperline )
380 void KXFace::UnCompAll( char *fbuf)
387 while (p < F + PIXELS)
389 UnCompress(F, 16, 16, 0);
390 UnCompress(F + 16, 16, 16, 0);
391 UnCompress(F + 32, 16, 16, 0);
392 UnCompress(F + WIDTH * 16, 16, 16, 0);
393 UnCompress(F + WIDTH * 16 + 16, 16, 16, 0);
394 UnCompress(F + WIDTH * 16 + 32, 16, 16, 0);
395 UnCompress(F + WIDTH * 32, 16, 16, 0);
396 UnCompress(F + WIDTH * 32 + 16, 16, 16, 0);
397 UnCompress(F + WIDTH * 32 + 32, 16, 16, 0);
400 void KXFace::UnCompress( char *f, int wid, int hei, int lev)
402 switch (BigPop(&levels[lev][0]))
407 PopGreys(f, wid, hei);
413 UnCompress(f, wid, hei, lev);
414 UnCompress(f + wid, wid, hei, lev);
415 UnCompress(f + hei * WIDTH, wid, hei, lev);
416 UnCompress(f + wid + hei * WIDTH, wid, hei, lev);
421 void KXFace::BigWrite( char *fbuf)
423 static unsigned char tmp;
424 static char buf[DIGITS];
429 while (B.b_words > 0)
431 BigDiv(NUMPRINTS, &tmp);
432 *(s++) = tmp + FIRSTPRINT;
441 if (++i >= MAXLINELEN)
452 void KXFace::BigRead( char *fbuf)
456 while (*fbuf != '\0')
459 if ((c < FIRSTPRINT) || (c > LASTPRINT))
462 BigAdd(( unsigned char)(c - FIRSTPRINT));
466 void KXFace::ReadFace( char *fbuf)
472 for(i = strlen(s); i > 0; i--)
475 if ((c >= '0') && (c <= '9'))
477 if (t >= fbuf + DIGITS)
484 else if ((c >= 'A') && (c <= 'F'))
486 if (t >= fbuf + DIGITS)
491 *(t++) = c - 'A' + 10;
493 else if ((c >= 'a') && (c <= 'f'))
495 if (t >= fbuf + DIGITS)
500 *(t++) = c - 'a' + 10;
502 else if (((c == 'x') || (c == 'X')) && (t > fbuf) && (*(t-1) == 0))
505 if (t < fbuf + DIGITS)
506 longjmp(comp_env, ERR_INSUFF);
509 c = 1 << (BITSPERDIG - 1);
510 while (t < F + PIXELS)
512 *(t++) = (*s & c) ? 1 : 0;
516 c = 1 << (BITSPERDIG - 1);
521 void KXFace::GenFace()
523 static char newp[PIXELS];
536 void KXFace::UnGenFace()
542 void KXFace::Gen( char *f)
544 int m, l, k, j, i, h;
546 for (j = 0; j < HEIGHT; j++)
548 for (i = 0; i < WIDTH; i++)
552 for (l = i - 2; l <= i + 2; l++)
553 for (m = j - 2; m <= j; m++)
555 if ((l >= i) && (m == j))
557 if ((l > 0) && (l <= WIDTH) && (m > 0))
558 k = *(f + l + m * WIDTH) ? k * 2 + 1 : k * 2;
610 void KXFace::PopGreys( char *f, int wid, int hei)
616 PopGreys(f, wid, hei);
617 PopGreys(f + wid, wid, hei);
618 PopGreys(f + WIDTH * hei, wid, hei);
619 PopGreys(f + WIDTH * hei + wid, wid, hei);
631 *(f + WIDTH + 1) = 1;
635 void KXFace::CompAll( char *fbuf)
637 Compress(F, 16, 16, 0);
638 Compress(F + 16, 16, 16, 0);
639 Compress(F + 32, 16, 16, 0);
640 Compress(F + WIDTH * 16, 16, 16, 0);
641 Compress(F + WIDTH * 16 + 16, 16, 16, 0);
642 Compress(F + WIDTH * 16 + 32, 16, 16, 0);
643 Compress(F + WIDTH * 32, 16, 16, 0);
644 Compress(F + WIDTH * 32 + 16, 16, 16, 0);
645 Compress(F + WIDTH * 32 + 32, 16, 16, 0);
648 BigPush(ProbBuf[--NumProbs]);
652 void KXFace::Compress( char *f, int wid, int hei, int lev)
654 if (AllWhite(f, wid, hei))
656 RevPush(&levels[lev][WHITE]);
659 if (AllBlack(f, wid, hei))
661 RevPush(&levels[lev][BLACK]);
662 PushGreys(f, wid, hei);
665 RevPush(&levels[lev][GREY]);
669 Compress(f, wid, hei, lev);
670 Compress(f + wid, wid, hei, lev);
671 Compress(f + hei * WIDTH, wid, hei, lev);
672 Compress(f + wid + hei * WIDTH, wid, hei, lev);
675 int KXFace::AllWhite( char *f, int wid, int hei)
677 return ((*f == 0) && Same(f, wid, hei));
680 int KXFace::AllBlack( char *f, int wid, int hei)
686 return (AllBlack(f, wid, hei) && AllBlack(f + wid, wid, hei) &&
687 AllBlack(f + WIDTH * hei, wid, hei) &&
688 AllBlack(f + WIDTH * hei + wid, wid, hei));
691 return (*f || *(f + 1) || *(f + WIDTH) || *(f + WIDTH + 1));
694 int KXFace::Same( char *f, int wid, int hei)
712 void KXFace::PushGreys( char *f, int wid, int hei)
718 PushGreys(f, wid, hei);
719 PushGreys(f + wid, wid, hei);
720 PushGreys(f + WIDTH * hei, wid, hei);
721 PushGreys(f + WIDTH * hei + wid, wid, hei);
724 RevPush(freqs + *f + 2 * *(f + 1) + 4 * *(f + WIDTH) +
725 8 * *(f + WIDTH + 1));
729 #include "kxface.moc"
TDEPIM classes for drag and drop of mails.
|