Import the current wip animation datatype and subclasses. further development will...
[AROS.git] / workbench / classes / datatypes / gifanim / prefs.c
blob84a78bceb0598bf6fbc81337528e96dc1907938c
2 /*
3 **
4 ** $VER: prefs.c 2.3 (24.5.98)
5 ** gifanim.datatype 2.3
6 **
7 ** Preferences
8 **
9 ** Written 1997/1998 by Roland 'Gizzy' Mainz
10 ** Original example source from David N. Junod
14 #ifndef DEBUG
15 # define DEBUG 0
16 #endif
17 #include <aros/debug.h>
19 struct MyStackSwapStruct;
20 struct GIFAnimInstData;
21 struct GIFEncoder;
23 /* main includes */
24 #include "classbase.h"
25 #include "classdata.h"
26 #include "encoder.h"
28 /* ansi includes */
29 #include <limits.h>
30 #include <stdio.h>
32 /*****************************************************************************/
34 /* local prototypes */
35 static STRPTR GetPrefsVar( struct ClassBase *, STRPTR );
36 static BOOL matchstr( struct ClassBase *, STRPTR, STRPTR );
38 /*****************************************************************************/
40 /****** gifanim.datatype/preferences *****************************************
42 * NAME
43 * preferences
45 * DESCRIPTION
46 * The "ENV:Classes/DataTypes/gifanim.prefs" file contains global
47 * settings for the datatype.
48 * The preferences file is an ASCII file containing one line where the
49 * preferences can be set.
50 * It can be superset by a local variable with the same name.
52 * Each line can contain settings, special settings for some projects
53 * can be set using the MATCHPROJECT option.
54 * Lines beginning with a '#' or ';' chars are treated as comments.
55 * Lines are limitted to 256 chars.
57 * TEMPLATE
58 * MATCHPROJECT/K,VERBOSE/S,NOVERBOSE/S,STRICTSYNTAX/S,NOSTRICTSYNTAX/S,
59 * MODEID/K/N,16BITCHUNKY=24BITCHUNKY=TRUECOLOR/S,
60 * NO16BITCHUNKY=NO24BITCHUNKY=NOTRUECOLOR/S,FPS/K/N,
61 * SAMPLE/K,SAMPLESPERFRAME=SPF/K/N,VOLUME/K/N,LOADALL/S,
62 * NOLOADALL/S,ENC_INTERLACE/S,ENC_NO_INTERLACE/S,
63 * ENC_BACKGROUNDPEN=ENC_BG/K/N,ENC_TRANSPARENTPEN=ENC_TRANSPARENT/K/N
65 * MATCHPROJECT -- The settings in this line belongs only to this
66 * project(s), e.g. if the case-insensitive pattern does not match,
67 * this line is ignored.
68 * The maximum length of the pattern is 128 chars.
69 * Defaults to #?, which matches any project.
71 * VERBOSE -- Print information about the animation. Currently
72 * the frame numbers and the used compression are printed, after all
73 * number of scanned/loaded frames, set FPS rate, dimensions (width/
74 * height/depth), sample information etc.
76 * NOVERBOSE -- Turns verbose output and error messages OFF.
77 * Be carefull, you won't see any error messages any more !!!
79 * STRICTSYNTAX -- Prompt syntax errors in the gif streams.
81 * NOSTRICTSYNTAX -- Turns STRICTSYNTAX off
83 * MODEID -- Select screen mode id of datatype (will be stored in
84 * ADTA_ModeID). Note that the DOS ReadArgs function used for parsing
85 * fetches a SIGNED long. The bit 31 will be represented by minus
86 * '-'. (example: "MODEID=266240" sets the mode to the A2024 screen
87 * mode id)
88 * Defaults to -1, which means: Use the best screenmode available for
89 * the given width, height and depth.
91 * 16BITCHUNKY
92 * 24BITCHUNKY
93 * TRUECOLOR -- Create 24 bit chunky bitmaps, if possible.
94 * Note that the 16BITCHUNKY and the 24BITCHUNKY options will be
95 * seperated in the future. The TRUECOLOR option selects the
96 * best truecolor depth in this case...
98 * NO16BITCHUNKY
99 * NO24BITCHUNKY
100 * NOTRUECOLOR -- Turns 24BITCHUNKY option off. (Default)
101 * Note that the 16BITCHUNKY and the 24BITCHUNKY options will be
102 * seperated in the future. The TRUECOLOR option selects the
103 * best truecolor depth in this case...
105 * FPS -- Frames Per Second
106 * A value of 0 here means: Use default FPS.
108 * SAMPLE -- Attach the given sample to the animation. The sample will
109 * be loaded using datatypes (GID_SOUND).
110 * Only one sample can be attached to one animation stream, any
111 * following attempt to attach a sample will be ignored.
113 * SAMPLESPERFRAME -- Set samples per frame rate for sound. This
114 * overrides the own internal calculations to get rid of rounding
115 * errors.
117 * VOLUME -- Volume of the sound when playing.
118 * Defaults to 64, which is the maximum. A value greater than 64 will
119 * be set to 64.
121 * LOADALL -- Load all frames into memory.
123 * NOLOADALL -- Turns off the LOADALL flag, which may be set in a prefs-
124 * line before. This switch is set per default, and can be turned off
125 * by the LOADALL option, later it can be turned on again by this
126 * option.
128 * Encoder related options:
129 * ENC_INTERLACE - create interlaced gif animation
131 * ENC_NO_INTERLACE - create non-interlaced gif animation
132 * (set per default).
134 * ENC_BACKGROUNDPEN
135 * ENC_BG - background pen number
136 * Defaults to 0 (e.g. default bg pen)
138 * ENC_TRANSPARENTPEN
139 * ENC_TRANSPARENT - transparent pen number
140 * Defaults to -1 (means: no transparent pen).
143 * NOTE
144 * - An invalid prefs file line will be ignored and forces the VERBOSE
145 * output.
147 * BUGS
148 * - Low memory may cause that the prefs file won't be parsed.
150 * - Lines are limitted to 256 chars
152 * - An invalid prefs file line will be ignored.
154 * - The sample path length is limitted to 200 chars. A larger
155 * value may crash the machine if an error occurs.
157 ******************************************************************************
162 static
163 STRPTR GetPrefsVar( struct ClassBase *cb, STRPTR name )
165 STRPTR buff;
166 const ULONG buffsize = 16UL;
168 if ((buff = (STRPTR)AllocVec( (buffsize + 2UL), (MEMF_PUBLIC | MEMF_CLEAR) ) ) != NULL)
170 if( GetVar( name, buff, buffsize, GVF_BINARY_VAR ) != (-1L) )
172 ULONG varsize = IoErr();
174 varsize += 2UL;
176 if( varsize > buffsize )
178 FreeVec( buff );
180 if ((buff = (STRPTR)AllocVec( (varsize + 2UL), (MEMF_PUBLIC | MEMF_CLEAR) ) ) != NULL)
182 if( GetVar( name, buff, varsize, GVF_BINARY_VAR ) != (-1L) )
184 return( buff );
188 else
190 return( buff );
194 FreeVec( buff );
197 return( NULL );
201 static
202 BOOL matchstr( struct ClassBase *cb, STRPTR pat, STRPTR s )
204 TEXT buff[ 512 ];
206 if( pat && s )
208 if( ParsePatternNoCase( pat, buff, (sizeof( buff ) - 1) ) != (-1L) )
210 if( MatchPatternNoCase( buff, s ) )
212 return( TRUE );
217 return( FALSE );
220 void ReadENVPrefs( struct ClassBase *cb, struct GIFAnimInstData *gaid, struct GIFEncoder *genc )
222 struct RDArgs envvarrda =
224 { NULL, 256L, 0L},
226 NULL,
228 NULL,
229 RDAF_NOPROMPT
232 struct
234 STRPTR matchproject;
235 IPTR verbose;
236 IPTR noverbose;
237 IPTR strictsyntax;
238 IPTR nostrictsyntax;
239 IPTR *modeid;
240 IPTR use24bitchunky;
241 IPTR nouse24bitchunky;
242 IPTR *fps;
243 STRPTR sample;
244 IPTR *samplesperframe;
245 IPTR *volume;
246 IPTR loadall;
247 IPTR noloadall;
248 IPTR enc_interlace;
249 IPTR enc_no_interlace;
250 IPTR *enc_backgroundpen;
251 IPTR *enc_transparentpen;
252 } gifanimargs;
254 TEXT varbuff[ 258 ];
255 STRPTR var;
257 if ((var = GetPrefsVar( cb, "Classes/DataTypes/gifanim.prefs" ) ) != NULL)
259 STRPTR prefsline = var,
260 nextprefsline;
261 ULONG linecount = 1UL;
263 /* Be sure that "var" contains at least one break-char */
264 strcat( var, "\n" );
266 while ((nextprefsline = strpbrk( prefsline, "\n" ) ) != NULL)
268 stccpy( varbuff, prefsline, (int)MIN( (sizeof( varbuff ) - 2UL), (((ULONG)(nextprefsline - prefsline)) + 1UL) ) );
270 /* be sure that this line isn't a comment line or an empty line */
271 if( (varbuff[ 0 ] != '#') && (varbuff[ 0 ] != ';') && (varbuff[ 0 ] != '\n') && (strlen( varbuff ) > 2UL) )
273 /* Prepare ReadArgs processing */
274 strcat( varbuff, "\n" ); /* Add NEWLINE-char */
275 envvarrda . RDA_Source . CS_Buffer = varbuff; /* Buffer */
276 envvarrda . RDA_Source . CS_Length = strlen( varbuff ) + 1UL; /* Set up input buffer length */
277 envvarrda . RDA_Source . CS_CurChr = 0L;
278 envvarrda . RDA_Buffer = NULL;
279 envvarrda . RDA_BufSiz = 0L;
280 memset( (void *)(&gifanimargs), 0, sizeof( gifanimargs ) ); /* Clear result array */
282 if( ReadArgs( "MATCHPROJECT/K,"
283 "VERBOSE/S,"
284 "NOVERBOSE/S,"
285 "STRICTSYNTAX/S,"
286 "NOSTRICTSYNTAX/S,"
287 "MODEID/K/N,"
288 "16BITCHUNKY=24BITCHUNKY=TRUECOLOR/S,"
289 "NO16BITCHUNKY=NO24BITCHUNKY=NOTRUECOLOR/S,"
290 "FPS/K/N,"
291 "SAMPLE/K,"
292 "SAMPLESPERFRAME=SPF/K/N,"
293 "VOLUME/K/N,"
294 "LOADALL/S,"
295 "NOLOADALL/S,"
296 "ENC_INTERLACE/S,"
297 "ENC_NO_INTERLACE/S,"
298 "ENC_BACKGROUNDPEN=ENC_BG/K/N,"
299 "ENC_TRANSPARENTPEN=ENC_TRANSPARENT/K/N", (IPTR *)(&gifanimargs), (&envvarrda) ) )
301 BOOL noignore = TRUE;
303 if( (gifanimargs . matchproject) && (gaid -> gaid_ProjectName) )
305 noignore = matchstr( cb, (gifanimargs . matchproject), (gaid -> gaid_ProjectName) );
308 if( noignore )
310 /* Read encoder prefs ? */
311 if( genc )
313 if( gifanimargs . enc_interlace )
315 genc -> interlace = TRUE;
318 if( gifanimargs . enc_no_interlace )
320 genc -> interlace = FALSE;
323 if( gifanimargs . enc_backgroundpen )
325 genc -> backgroundpen = (WORD)(*(gifanimargs . enc_backgroundpen));
328 if( gifanimargs . enc_transparentpen )
330 genc -> transparentpen = (WORD)(*(gifanimargs . enc_transparentpen));
333 else
335 if( gifanimargs . verbose )
337 OpenLogfile( cb, gaid );
340 if( gifanimargs . noverbose )
342 if( (gaid -> gaid_VerboseOutput) && ((gaid -> gaid_VerboseOutput) != (BPTR)-1L) )
344 Close( (gaid -> gaid_VerboseOutput) );
347 gaid -> gaid_VerboseOutput = (BPTR)-1L;
350 if( gifanimargs . strictsyntax )
352 gaid -> gaid_StrictSyntax = TRUE;
355 if( gifanimargs . nostrictsyntax )
357 gaid -> gaid_StrictSyntax = FALSE;
360 if( gifanimargs . modeid )
362 gaid -> gaid_ModeID = *(gifanimargs . modeid);
365 if( gifanimargs . use24bitchunky )
367 #if !defined(__AROS__)
368 /* Check if we have animation.datatype V41 (or higher) as superclass */
369 if( (cb -> cb_SuperClassBase -> lib_Version) >= 41U )
371 /* Check here if we opened the cybergraphics.library. After this point, I'll assume
372 * that (gaid_UseChunkyMap == TRUE) implies a opened CyberGfxBase !!
374 if( CyberGfxBase )
376 #endif
377 gaid -> gaid_UseChunkyMap = TRUE;
378 #if !defined(__AROS__)
380 else
382 error_printf( cb, gaid, "no cybergraphics.library available, can't output a 24 bit chunky map\n" );
385 else
387 error_printf( cb, gaid, "Requires at least animation.datatype V41 for non-planar bitmap support\n" );
389 #endif
391 if( gifanimargs . nouse24bitchunky )
393 gaid -> gaid_UseChunkyMap = FALSE;
396 if( gifanimargs . fps )
398 gaid -> gaid_FPS = *(gifanimargs . fps);
401 if( gifanimargs . loadall )
403 gaid -> gaid_LoadAll = TRUE;
406 if( gifanimargs . noloadall )
408 gaid -> gaid_LoadAll = FALSE;
411 if( (gifanimargs . sample) && ((gaid -> gaid_Sample) == NULL) )
413 Object *so;
414 LONG ioerr = 0L;
416 verbose_printf( cb, gaid, "loading sample \"%s\"...\n", (gifanimargs . sample) );
418 if ((so = NewDTObject( (gifanimargs . sample), DTA_GroupID, GID_SOUND, TAG_DONE ) ) != NULL)
420 BYTE *sample;
421 ULONG length;
422 ULONG period;
424 /* Get sample data from object */
425 if( GetDTAttrs( so, SDTA_Sample, (&sample),
426 SDTA_SampleLength, (&length),
427 SDTA_Period, (&period),
428 TAG_DONE ) == 3UL )
430 if ((gaid -> gaid_Sample = (STRPTR)AllocPooled( (gaid -> gaid_Pool), (length + 1UL) ) ) != NULL)
432 /* Copy sample and context */
433 CopyMem( (APTR)sample, (APTR)(gaid -> gaid_Sample), length );
434 gaid -> gaid_SampleLength = length;
435 gaid -> gaid_Period = period;
437 else
439 /* Can't alloc sample */
440 ioerr = ERROR_NO_FREE_STORE;
443 else
445 /* Object does not support the requested attributes */
446 ioerr = ERROR_OBJECT_WRONG_TYPE;
449 DisposeDTObject( so );
451 else
453 /* NewDTObjectA failed, cannot load sample... */
454 ioerr = IoErr();
457 if( (gaid -> gaid_Sample) == NULL )
459 TEXT errbuff[ 256 ];
461 if( ioerr >= DTERROR_UNKNOWN_DATATYPE )
463 mysprintf( cb, errbuff, GetDTString( ioerr ), (gifanimargs . sample) );
465 else
467 Fault( ioerr, (gifanimargs . sample), errbuff, sizeof( errbuff ) );
470 error_printf( cb, gaid, "can't load sample: \"%s\" line %lu\n", errbuff, linecount );
474 if( gifanimargs . samplesperframe )
476 gaid -> gaid_SamplesPerFrame = *(gifanimargs . samplesperframe);
479 if( gifanimargs . volume )
481 gaid -> gaid_Volume = *(gifanimargs . volume);
483 if( (gaid -> gaid_Volume) > 64UL )
485 gaid -> gaid_Volume = 64UL;
490 else
492 verbose_printf( cb, gaid, "prefs line %lu ignored\n", linecount );
495 FreeArgs( (&envvarrda) );
497 else
499 LONG ioerr = IoErr();
500 TEXT errbuff[ 256 ];
502 Fault( ioerr, "Classes/DataTypes/gifanim.prefs", errbuff, (LONG)sizeof( errbuff ) );
504 error_printf( cb, gaid, "preferences \"%s\" line %lu\n", errbuff, linecount );
508 prefsline = ++nextprefsline;
509 linecount++;
512 FreeVec( var );