File: TIMGreader.cpp

package info (click to toggle)
gentle 1.9+cvs20100605+dfsg1-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 12,224 kB
  • ctags: 6,214
  • sloc: cpp: 41,569; ansic: 3,978; sh: 1,420; makefile: 243
file content (136 lines) | stat: -rwxr-xr-x 3,841 bytes parent folder | download | duplicates (6)
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
/** \file
	\brief Contains the TIMGreader class, and its TIMGitem helper class
*/
#include "TIMGreader.h"
#include <wx/image.h>

TIMGreader::~TIMGreader ()
    {
    if ( buffer ) delete buffer ;
    if ( output ) delete output ;
    }

int TIMGreader::getInt ( int adr )
   {
   int a1 = (unsigned char) buffer[adr] ;
   int a2 = (unsigned char) buffer[adr+1] ;
   int r = a1 + a2 * 256 ;
   return r ;
   }
   
void TIMGreader::readFile ( wxString fn )
    {
    wxFile file ( fn ) ;
    size = file.Length() ;
    buffer = new unsigned char [size+5];
    file.Read ( (char*)buffer, size );
    file.Close() ;
    }
    
wxImage TIMGreader::makeImage()
    {
    int w_adr = 3549 ;
    int h_adr = w_adr + 2 ;
    int xoff = 0 , yoff = 0 ;
    
    w = getInt ( w_adr ) ;
    h = getInt ( h_adr ) ;
    
//    xoff = -getInt ( 3577 ) ;
//    yoff = -getInt ( 3577 ) ;
//    yoff = -buffer[3577] ;
      
    int cur = 4089 ;
    output = (unsigned char*) malloc ( w * h * 3 ) ;
    int offset = 0 , x , y ;
    for ( y = 0 ; y < h ; y++ )
       {
       for ( x = 0 ; x < w ; x++ )
          {
          int col = buffer[cur++] ;
          output[offset++] = col ;
          output[offset++] = col ;
          output[offset++] = col ;
          }
       }
    wxImage i ( w , h ) ;
    i.SetData ( output ) ;
    
    // Items
    items.clear() ;
    x = 0 ;
    for ( y = size - 1 ; y > 5 && x < 5 ; y-- )
       {
       for ( x = 0 ; y-x > 0 && x < 5 && buffer[y-x] == 255 ; x++ ) ;
       }
    for ( y++ ; y > 10 && y + 3 < size ; y++ )
       {
       if ( buffer[y] == 0 && buffer[y+1] == buffer[y-1] &&
              buffer[y+2] == buffer[y-2] && buffer[y+3] == buffer[y-3] &&
              buffer[y+1] == buffer[y+2] && buffer[y+2] == buffer[y+3] &&
              buffer[y+1] == 255 )
          { // "FF FF FF 00 FF FF FF" | y at 00
          TIMGitem it ;
          it.type = IMGTYPE_TEXT ;
          
          for ( x = y + 7 ; buffer[x] ; x++ ) ;
          for ( x++ ; buffer[x] == 0 ; x++ ) ;
          
          it.p1.x = getInt ( x + 1 ) + xoff ;
          it.p1.y = getInt ( x + 5 ) + yoff ;
          
          it.orig = wxSize ( w , h ) ;
          for ( x = 7 ; x < 50 && buffer[y+x] ; x++ )
             it.s += buffer[y+x] ;
             
          it.font_size = ( 243 - buffer[y-61] ) / 3 + 10 ;
          it.font_name = wxString ( (char*) buffer + y - 43 , *wxConvCurrent ) ;
             
          items.push_back ( it ) ;
          }
       }
    
    return i ;
    }
    
//

void TIMGitem::draw ( wxDC &dc , int x1 , int y1 , int x2 , int y2 ) 
    {
    cur = wxSize ( x2 - x1 , y2 - y1 ) ;
    off = wxPoint ( x1 , y1 ) ;
    
//    dc.DrawLine ( xx ( 0 ) , yy ( 0 ) , xx ( orig.GetWidth() ) , yy ( orig.GetHeight() ) ) ;
//    wxString u = wxString::Format ( "Image dimensions : %d:%d" , orig.GetWidth() , orig.GetHeight() ) ;
//    dc.DrawText ( u , xx ( 0 ) , yy ( 0 ) ) ;

    if ( type == IMGTYPE_TEXT )
        {
        wxFont oldfont = dc.GetFont() ;
        dc.SetFont ( *MYFONT ( font_size*2/3 , wxMODERN , wxNORMAL , wxNORMAL /*, false , font_name.c_str()*/ ) ) ;

        int px = xx ( p1.x ) ;
        int py = yy ( p1.y ) ;
        
        wxString t = s + _T(" {") + font_name + wxString::Format ( _T(":%d} (%d:%d) [%d:%d]") , 
                                font_size ,
                                p1.x , p1.y , 
                                px , py ) ;

        t = s ; // Back to the roots ;-)
        
        dc.DrawText ( t , px , py ) ;
        dc.SetFont ( oldfont ) ;
        }
    }
    
int TIMGitem::xx ( int x )
    {
    return off.x + x * 19 / 24 ;
    }

int TIMGitem::yy ( int y )
    {
    return off.y + y * 9 / 12 ;
    }