1 // Copyright 2002, 2003 David Hilvert <dhilvert@auricle.dyndns.org>,
2 // <dhilvert@ugcs.caltech.edu>
4 /* This file is part of the Anti-Lamenessing Engine.
6 The Anti-Lamenessing Engine is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 The Anti-Lamenessing Engine is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with the Anti-Lamenessing Engine; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * tfile.h: Read and write transformation data files.
26 * This version of ALE reads transformation data file versions 0, 1, 2, and 3,
27 * and writes version 2 and 3 transformation data files. Data file versions 1
28 * and higher are identified by a version command "V x", where x is the version
29 * number, prior to any transformation command. Data file version 0 is
30 * identified by having no version command.
36 #include "transformation.h"
38 #define TFILE_VERSION 3
39 #define TFILE_VERSION_MAX 3
41 extern int tfile_input_version
;
42 extern int tfile_output_version
;
45 * Structure to describe a transformation data file to load data from.
54 * Structure to describe a transformation data file to write data to.
66 * Create a new tload_t transformation data file structure, used for
67 * reading data from transformation data files.
70 static inline struct tload_t
*tload_new(const char *filename
) {
71 FILE *file
= fopen (filename
, "r");
72 struct tload_t
*result
= NULL
;
75 fprintf(stderr
, "tload: Error: could not open transformation data file '%s'.", filename
);
79 result
= (struct tload_t
*)
80 malloc(sizeof(struct tload_t
));
81 result
->filename
= filename
;
88 * Load the first transformation from a transformation data file associated with
89 * transformation data file structure T, or return the default transformation
90 * if no transformation is available.
92 * T is a pointer to the tload_t transformation data file structure.
94 * IS_P is nonzero if a projective transformation is expected.
96 * DEFAULT_TRANSFORM is the default transformation result.
98 * IS_DEFAULT is used to signal a non-default transformation result.
101 static inline transformation
tload_first(struct tload_t
*t
, int is_p
,
102 transformation default_transform
, int *is_default
) {
104 transformation result
= default_transform
;
109 * If there is no file, return the default.
116 * Search through the initial part of the file to determine
126 first_character
= fgetc(t
->file
);
128 while (first_character
== ' '
129 || first_character
== 0xa
130 || first_character
== 0xd
131 || first_character
== '\t'
132 || first_character
== '#') {
133 ungetc(first_character
, t
->file
);
135 fgets(line
, 1024, t
->file
);
136 if (strlen(line
) >= 1023) {
138 "\ntrans-load: Error: line too long in input file\n");
142 first_character
= fgetc(t
->file
);
145 if (first_character
!= EOF
)
146 ungetc(first_character
, t
->file
);
149 * Check for version 0
152 if (first_character
!= 'V')
161 * Obtain version from version command string.
165 fgets(line
, 1024, t
->file
);
166 if (strlen(line
) >= 1023) {
168 "\ntrans-load: Error: line too long in input file\n");
172 int count
= sscanf(line
, "V %d", &tfile_input_version
);
175 fprintf(stderr
, "Error in transformation "
176 "file version command.\n");
178 } else if (tfile_input_version
> TFILE_VERSION_MAX
) {
179 fprintf(stderr
, "Unsupported transformation "
181 tfile_input_version
);
186 * Handle versions lower than 3.
189 if (tfile_input_version
< 3)
192 * Versions lower than 3 use the default transformation
193 * for the original frame.
199 * Read each line of the file until we find a transformation
203 while (!feof(t
->file
)) {
206 fgets(line
, 1024, t
->file
);
211 if (strlen(line
) >= 1023) {
213 "\ntrans-load: Error: line too long in input file\n");
223 /* Comment or whitespace */
227 /* Default transformation */
231 if (tfile_input_version
< 3) {
232 fprintf(stderr
, "\ntrans-load: Error: "
233 "Barrel distortion not supported "
234 "for version %d input files.\n"
235 "trans-load: Hint: Use version 3 "
236 "file syntax.\n", tfile_input_version
);
240 unsigned int pos
= 0, chars
;
242 double dparameters
[BARREL_DEGREE
];
243 ale_pos parameters
[BARREL_DEGREE
];
245 count
= sscanf(line
, "B %u%n", &bdc
, &chars
);
249 fprintf(stderr
, "\ntrans-load: Error: "
250 "Malformed 'B' command.\n");
254 if (bdc
> result
.bd_max()) {
255 fprintf(stderr
, "\ntrans-load: Error: "
256 "Barrel distortion degree %d "
257 "is too large. (Maximum is %d.)\n"
259 "Reduce degree or re-compile "
260 "with BD_DEGREE=%d\n", bdc
, BARREL_DEGREE
, bdc
);
264 for (unsigned int d
= 0; d
< bdc
; d
++) {
265 count
= sscanf(line
+ pos
, "%lf%n", &dparameters
[d
], &chars
);
269 fprintf(stderr
, "\ntrans-load: Error: "
270 "Malformed 'B' command.\n");
274 parameters
[d
] = dparameters
[d
];
277 result
.bd_set(bdc
, parameters
);
282 /* Projective transformation data */
285 fprintf(stderr
, "\ntrans-load: Error: "
286 "Projective data for euclidean "
289 "Use command-line option --projective.\n");
292 double width
, height
, values
[8];
296 count
= sscanf(line
, "P %lf%lf%lf%lf%lf%lf%lf%lf%lf%lf", &width
, &height
,
297 &values
[0], &values
[1], &values
[2], &values
[3],
298 &values
[4], &values
[5], &values
[6], &values
[7]);
301 for (int i
= 0; i
< 4; i
++)
302 for (int j
= 1; j
>= 0; j
--)
303 x
[i
][j
] = values
[index
++];
306 fprintf(stderr
, "\ntrans-load: warning:"
307 "Missing args for 'P'\n");
309 for (i
= 0; i
< count
- 2; i
++) {
310 ale_pos factor
= (i
% 2)
311 ? (result
.scaled_width() / width
)
312 : (result
.scaled_height() / height
);
314 x
[i
/ 2][i
% 2] *= factor
;
324 /* Euclidean transformation data */
327 double width
, height
;
328 double values
[3] = {0, 0, 0};
332 count
= sscanf(line
, "E %lf%lf%lf%lf%lf",
334 &values
[0], &values
[1], &values
[2]);
341 fprintf(stderr
, "\ntrans-load: warning:"
342 "Missing args for 'E'\n");
344 for (i
= 0; (i
< count
- 2) && (i
< 2); i
++) {
345 ale_pos factor
= (i
% 2)
346 ? (result
.scaled_width() / width
)
347 : (result
.scaled_height() / height
);
359 "\ntrans-load: Error in tload_first: unrecognized command '%s'\n",
366 * EOF reached: return default transformation.
373 * Load the next transformation from a transformation data file associated with
374 * transformation data file structure T, or return the default transformation
375 * if no transformation is available.
377 * T is a pointer to the tload_t transformation data file structure.
379 * IS_P is nonzero if a projective transformation is expected.
381 * DEFAULT_TRANSFORM is the default transformation result.
383 * IS_DEFAULT is used to signal a non-default transformation result.
385 * IS_PRIMARY is used to differentiate primary and non-primary
389 static inline transformation
tload_next(struct tload_t
*t
, int is_p
,
390 transformation default_transform
, int *is_default
,
393 transformation result
= default_transform
;
398 * Read each line of the file until we find a transformation.
401 while (t
&& !feof(t
->file
)) {
403 char c
= fgetc(t
->file
);
404 if (!feof(t
->file
) && c
!= EOF
)
419 fgets(line
, 1024, t
->file
);
424 if (strlen(line
) >= 1023) {
426 "\ntrans-load: warning: line too long in input file\n");
435 /* Comment or whitespace */
439 /* Default transformation */
443 if (tfile_input_version
< 3) {
444 fprintf(stderr
, "\ntrans-load: Error: "
445 "Barrel distortion not supported "
446 "for version %d input files.\n"
447 "trans-load: Hint: Use version 3 "
448 "file syntax.\n", tfile_input_version
);
452 unsigned int pos
= 0, chars
;
454 ale_pos parameters
[BARREL_DEGREE
];
455 double dparameters
[BARREL_DEGREE
];
457 count
= sscanf(line
, "B %u%n", &bdc
, &chars
);
461 fprintf(stderr
, "\ntrans-load: Error: "
462 "Malformed 'B' command.\n");
466 if (bdc
> result
.bd_max()) {
467 fprintf(stderr
, "\ntrans-load: Error: "
468 "Barrel distortion degree %d "
469 "is too large. (Maximum is %d.)\n"
471 "Reduce degree or re-compile "
472 "with BD_DEGREE=%d\n", bdc
, BARREL_DEGREE
, bdc
);
476 for (unsigned int d
= 0; d
< bdc
; d
++) {
477 count
= sscanf(line
+ pos
, "%lf%n", &dparameters
[d
], &chars
);
481 fprintf(stderr
, "\ntrans-load: Error: "
482 "Malformed 'B' command.\n");
486 parameters
[d
] = dparameters
[d
];
489 result
.bd_set(bdc
, parameters
);
498 /* Projective transformation data */
501 fprintf(stderr
, "\ntrans-load: Error: "
502 "Projective data for euclidean "
505 "Use command-line option --projective.\n");
508 double width
, height
, values
[8];
512 count
= sscanf(line
, "P %lf%lf%lf%lf%lf%lf%lf%lf%lf%lf", &width
, &height
,
513 &values
[0], &values
[1], &values
[2], &values
[3],
514 &values
[4], &values
[5], &values
[6], &values
[7]);
517 for (int i
= 0; i
< 4; i
++)
518 for (int j
= 1; j
>= 0; j
--)
519 x
[i
][j
] = values
[index
++];
522 fprintf(stderr
, "\ntrans-load: warning:"
523 "Missing args for 'P'\n");
525 for (i
= 0; i
< count
- 2; i
++) {
526 ale_pos factor
= (i
% 2)
527 ? (result
.scaled_width() / width
)
528 : (result
.scaled_height() / height
);
530 x
[i
/ 2][i
% 2] *= factor
;
533 if (tfile_input_version
< 1) {
535 * Accommodate older versions
538 for (i
= 0; i
< 4; i
++) {
543 result
.gpt_v0_set(x
);
557 /* Euclidean transformation data */
560 double width
, height
;
561 double values
[3] = {0, 0, 0};
565 count
= sscanf(line
, "E %lf%lf%lf%lf%lf",
567 &values
[0], &values
[1], &values
[2]);
573 if (tfile_input_version
< 2) {
581 fprintf(stderr
, "\ntrans-load: warning:"
582 "Missing args for 'E'\n");
584 for (i
= 0; (i
< count
- 2) && (i
< 2); i
++) {
585 ale_pos factor
= (i
% 2)
586 ? (result
.scaled_width() / width
)
587 : (result
.scaled_height() / height
);
592 if (tfile_input_version
< 1) {
593 result
.eu_v0_set(eu
);
603 "\ntrans-load: Error in tload_next: unrecognized command '%s'\n",
613 * Create a new tsave_t transformation data file structure, used for
614 * writing data to transformation data files.
617 static inline struct tsave_t
*tsave_new(const char *filename
) {
618 FILE *file
= fopen (filename
, "w");
619 struct tsave_t
*result
= NULL
;
622 fprintf(stderr
, "tsave: Error: could not open transformation data file '%s'.", filename
);
626 result
= (struct tsave_t
*)
627 malloc(sizeof(struct tsave_t
));
628 result
->filename
= filename
;
630 result
->orig
= "unknown";
631 result
->target
= "unknown";
633 fprintf(file
, "# created by ALE transformation file handler version %d\n",
642 * Save the first transformation to a transformation data file associated with
643 * transformation data file structure T, or do nothing if T is NULL. This
644 * function also establishes the output file version.
646 * OFFSET is the transformation to be saved.
648 * IS_PROJECTIVE indicates whether to write a projective transformation.
652 static inline void tsave_first(struct tsave_t
*t
, transformation offset
, int is_projective
) {
657 t
->file
= fopen(t
->filename
, "a");
660 * Determine the output version to use. We use version 3 output syntax only when
661 * necessary. This comprises two cases:
663 * (i) an input file is used, and this file uses version 3 syntax.
664 * (ii) non-degenerate barrel distortion correction is selected.
666 * (i) can be directly examined. When (i) does not hold, (ii) can be
667 * inferred from offset.bd_count(), since this value should be constant
668 * when (i) does not hold. XXX: This logic should be reviewed.
671 if (tfile_input_version
== 3 || offset
.bd_count() > 0)
672 tfile_output_version
= 3;
674 tfile_output_version
= 2;
677 fprintf(t
->file
, "# producing transformation file syntax version %d\n", tfile_output_version
);
678 fprintf(t
->file
, "V %d\n", tfile_output_version
);
680 fprintf(t
->file
, "# Comment: Target output file is %s\n", t
->target
);
681 fprintf(t
->file
, "# Comment: Original frame is %s\n", t
->orig
);
682 fprintf(t
->file
, "# Comment: Avg magnitude [r=%f g=%f b=%f]\n", t
->orig_apm
[0], t
->orig_apm
[1], t
->orig_apm
[2]);
684 if (tfile_output_version
< 3) {
689 if (offset
.bd_count() > 0) {
690 assert (tfile_output_version
>= 3);
693 fprintf(t
->file
, "B ");
694 fprintf(t
->file
, "%u ", offset
.bd_count());
695 for (i
= 0; i
< offset
.bd_count(); i
++)
696 fprintf(t
->file
, "%f ", (double) offset
.bd_get(i
));
697 fprintf(t
->file
, "\n");
704 fprintf(t
->file
, "P ");
705 fprintf(t
->file
, "%f %f ", (double) offset
.scaled_width(), (double) offset
.scaled_height());
706 for (i
= 0; i
< 4; i
++)
707 for (j
= 1; j
>= 0; j
--)
708 fprintf(t
->file
, "%f ", (double) offset
.gpt_get(i
, j
));
710 fprintf(t
->file
, "E ");
711 fprintf(t
->file
, "%f %f ", (double) offset
.scaled_width(), (double) offset
.scaled_height());
712 fprintf(t
->file
, "%f ", (double) offset
.eu_get(1));
713 fprintf(t
->file
, "%f ", (double) offset
.eu_get(0));
714 fprintf(t
->file
, "%f ", (double) offset
.eu_get(2));
717 fprintf(t
->file
, "\n");
723 * Save the next transformation to a transformation data file associated with
724 * transformation data file structure T, or do nothing if T is NULL.
726 * OFFSET is the transformation to be saved.
728 * IS_PROJECTIVE indicates whether to write a projective transformation.
730 * IS_PRIMARY indicates whether to write a primary transformation
734 static inline void tsave_next(struct tsave_t
*t
, transformation offset
, int is_projective
,
740 t
->file
= fopen(t
->filename
, "a");
742 if (is_primary
&& offset
.bd_count() > 0) {
743 assert (tfile_output_version
>= 3);
746 fprintf(t
->file
, "B ");
747 fprintf(t
->file
, "%u ", offset
.bd_count());
748 for (i
= 0; i
< offset
.bd_count(); i
++)
749 fprintf(t
->file
, "%f ", (double) offset
.bd_get(i
));
750 fprintf(t
->file
, "\n");
756 fprintf(t
->file
, is_primary
? "P " : "Q ");
757 fprintf(t
->file
, "%f %f ", (double) offset
.scaled_width(), (double) offset
.scaled_height());
758 for (i
= 0; i
< 4; i
++)
759 for (j
= 1; j
>= 0; j
--)
760 fprintf(t
->file
, "%f ", (double) offset
.gpt_get(i
, j
));
762 fprintf(t
->file
, is_primary
? "E " : "F ");
763 fprintf(t
->file
, "%f %f ", (double) offset
.scaled_width(), (double) offset
.scaled_height());
764 fprintf(t
->file
, "%f ", (double) offset
.eu_get(1));
765 fprintf(t
->file
, "%f ", (double) offset
.eu_get(0));
766 fprintf(t
->file
, "%f ", (double) offset
.eu_get(2));
769 fprintf(t
->file
, "\n");
775 * Write information to a transformation file indicating the target output
779 static inline void tsave_target(struct tsave_t
*t
, const char *filename
) {
783 t
->target
= filename
;
785 t
->file
= fopen(t
->filename
, "a");
793 * Write information to a transformation data file indicating the filename
794 * of the original frame (i.e. the first frame in the sequence of input
798 static inline void tsave_orig(struct tsave_t
*t
, const char *filename
, pixel apm
) {
807 * Write information to a transformation data file indicating the filename
808 * of a supplemental frame (i.e. a frame in the sequence of input frames
809 * that is not the first frame).
812 static inline void tsave_info(struct tsave_t
*t
, const char *filename
) {
814 t
->file
= fopen(t
->filename
, "a");
816 fprintf(t
->file
, "# Comment: Supplemental frame %s\n", filename
);
823 * Write information to a transformation data file indicating the tonal
824 * registration multiplier.
827 static inline void tsave_trm(struct tsave_t
*t
, ale_real r
, ale_real g
, ale_real b
) {
829 t
->file
= fopen(t
->filename
, "a");
831 fprintf(t
->file
, "# Comment: Exposure [r=%f g=%f b=%f]\n", r
, g
, b
);
838 * Write information to a transformation data file indicating the average
842 static inline void tsave_apm(struct tsave_t
*t
, ale_real r
, ale_real g
, ale_real b
) {
844 t
->file
= fopen(t
->filename
, "a");
846 fprintf(t
->file
, "# Comment: Avg magnitude [r=%f g=%f b=%f]\n", r
, g
, b
);
854 * Destroy a tload_t transformation data file structure.
857 static inline void tload_delete(struct tload_t
*victim
) {
859 fclose(victim
->file
);
864 * Destroy a tsave_t transformation data file structure.
867 static inline void tsave_delete(struct tsave_t
*victim
) {