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
  
/* 
|| This file is part of Pike. For copyright information see COPYRIGHT. 
|| Pike is distributed under GPL, LGPL and MPL. See the file COPYING 
|| for more information. 
*/ 
 
#include "global.h" 
#include "program.h" 
#include "module_support.h" 
#include "interpret.h" 
#include "builtin_functions.h" 
#include "mapping.h" 
#include "operators.h" 
 
#include "image_machine.h" 
#include "atari.h" 
 
 
extern struct program *image_program; 
 
/* Some format references: 
 * 
 *   http://oreilly.com/www/centers/gff/formats/atari/index.htm 
 *   http://wiki.multimedia.cx/index.php?title=Neochrome 
 *   http://www.mediatel.lu/workshop/graphic/2D_fileformat/h_atari.html 
 */ 
 
/*! @module Image 
 */ 
 
/*! @module NEO 
 *! Decodes images from the Atari image editor Neochrome. 
 */ 
 
/*! @decl mapping _decode(string data) 
 *! Low level decoding of the NEO file contents in @[data]. 
 *! @returns 
 *!   @mapping 
 *!     @member Image.Image "image" 
 *!       The decoded bitmap 
 *!     @member array(Image.Image) "images" 
 *!       Coler cycled images. 
 *!     @member string "filename" 
 *!       The filename stored into the file. 
 *!     @member int(0..15) "right_limit" 
 *!     @member int(0..15) "left_limit" 
 *!       The palette color range to be color cycled. 
 *!     @member int(0..255) "speed" 
 *!       The animation speed, expressed as the number of VBLs 
 *!       per animation frame. 
 *!     @member string "direction" 
 *!       Color cycling direction. Can be either @expr{"left"@} 
 *!       or @expr{"right"@}. 
 *!     @member array(array(int(0..255))) "palette" 
 *!       The palette to be used for color cycling. 
 *!   @endmapping 
 */ 
void image_neo_f__decode(INT32 args) 
{ 
  unsigned int i, res, size = 0; 
  struct atari_palette *pal=0; 
  struct object *img; 
 
  struct pike_string *s, *fn; 
  unsigned char *q; 
  ONERROR err; 
 
  get_all_args( NULL, args, "%S", &s ); 
  if(s->len!=32128) 
    Pike_error("This is not a NEO file (wrong file size).\n"); 
 
  q = (unsigned char *)s->str; 
  res = q[3]; 
 
  if(q[2]!=0 || (res!=0 && res!=1 && res!=2)) 
    Pike_error("This is not a NEO file (invalid resolution).\n"); 
 
  /* Checks done... */ 
  add_ref(s); 
  pop_n_elems(args); 
 
  if(res==0) 
    pal = decode_atari_palette(q+4, 16); 
  else if(res==1) 
    pal = decode_atari_palette(q+4, 4); 
  else if(res==2) 
    pal = decode_atari_palette(q+4, 2); 
  SET_ONERROR(err, free_atari_palette, pal); 
 
  push_static_text("palette"); 
  for( i=0; i<pal->size; i++ ) { 
    push_int(pal->colors[i].r); 
    push_int(pal->colors[i].g); 
    push_int(pal->colors[i].b); 
    f_aggregate(3); 
  } 
  f_aggregate(pal->size); 
  size += 2; 
 
  img = decode_atari_screendump(q+128, res, pal); 
 
  push_static_text("image"); 
  push_object(img); 
  size += 2; 
 
  if(q[48]&128) { 
    int rl, ll, i; 
    rl = q[49]&0xf; 
    ll = (q[49]&0xf0)>>4; 
 
    push_static_text("right_limit"); 
    push_int( rl ); 
    push_static_text("left_limit"); 
    push_int( ll ); 
    push_static_text("speed"); 
    push_int( q[51] ); 
    push_static_text("direction"); 
    if( q[50]&128 ) 
      push_static_text("right"); 
    else 
      push_static_text("left"); 
 
    push_static_text("images"); 
    for(i=0; i<rl-ll+1; i++) { 
      if( q[50]&128 ) 
        rotate_atari_palette(pal, ll, rl); 
      else 
        rotate_atari_palette(pal, rl, ll); 
      img = decode_atari_screendump(q+128, res, pal); 
      push_object(img); 
    } 
    f_aggregate(rl-ll+1); 
 
    size += 10; 
  } 
 
  UNSET_ONERROR(err); 
  free_atari_palette(pal); 
 
  fn = make_shared_binary_string((const char *)q+36, 12); 
 
  push_static_text("filename"); 
  push_string(fn); 
  size += 2; 
 
  free_string(s); 
  f_aggregate_mapping(size); 
} 
 
/*! @decl Image.Image decode(string data) 
 *! Decodes the image @[data] into an @[Image.Image] object. 
 */ 
void image_neo_f_decode(INT32 args) 
{ 
  image_neo_f__decode(args); 
  push_static_text("image"); 
  f_index(2); 
} 
 
/*! @endmodule 
 */ 
 
/*! @endmodule 
 */ 
 
void init_image_neo(void) 
{ 
  ADD_FUNCTION("decode",  image_neo_f_decode,  tFunc(tStr,tObj),  0); 
  ADD_FUNCTION("_decode", image_neo_f__decode, 
               tFunc(tStr,tMap(tStr,tMix)), 0); 
} 
 
void exit_image_neo(void) 
{ 
}