forked from 50m30n3/QTC
/
qtidec.c
140 lines (115 loc) · 3.47 KB
/
qtidec.c
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
/*
* QTC: qtidec.c (c) 2011, 2012 50m30n3
*
* This file is part of QTC.
*
* QTC is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QTC is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QTC. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "image.h"
#include "qti.h"
#include "qtc.h"
#include "ppm.h"
/*******************************************************************************
* This is the reference qti decoder. *
* *
* It implements all the features currently supported by the qti codec. *
*******************************************************************************/
void print_help( void )
{
puts( "qtidec (c) 50m30n3 2011, 2012" );
puts( "USAGE: qtidec [options] -i infile -o outfile" );
puts( "\t-h\t\t-\tPrint help" );
puts( "\t-v\t\t-\tBe verbose" );
puts( "\t-a [0..2]\t-\tAnalysis mode" );
puts( "\t-i filename\t-\tInput file (-)" );
puts( "\t-o filename\t-\tOutput file (-)" );
}
int main( int argc, char *argv[] )
{
struct image image;
struct qti compimage;
int opt, verbose, analyze;
char *infile, *outfile;
verbose = 0;
analyze = 0;
infile = NULL;
outfile = NULL;
while( ( opt = getopt( argc, argv, "hva:i:o:" ) ) != -1 )
{
switch( opt )
{
case 'h':
print_help();
return 0;
break;
case 'v':
verbose = 1;
break;
case 'a':
if( sscanf( optarg, "%i", &analyze ) != 1 )
fputs( "main: Can not parse command line: -a\n", stderr );
break;
case 'i':
infile = strdup( optarg );
break;
case 'o':
outfile = strdup( optarg );
break;
default:
case '?':
fputs( "main: Can not parse command line: unknown option\n", stderr );
return 1;
break;
}
}
if( ( analyze < 0 ) || ( analyze > 2 ) )
{
fputs( "main: Analysis mode out of range\n", stderr );
return 1;
}
if( ! qti_read( &compimage, infile ) ) // Read compressed image from file
return 2;
if( ! image_create( &image, compimage.width, compimage.height, 0 ) )
return 2;
if( analyze == 0 )
{
if( ! qtc_decompress( &compimage, NULL, &image ) ) // Decompress image
return 2;
if( image.transform == 1 ) // Apply reverse image transforms
image_transform_fast_rev( &image );
else if( image.transform == 2 )
image_transform_rev( &image );
if( image.colordiff ) // Apply reverse fakeyuv transform
image_color_diff_rev( &image );
}
else
{
if( ! qtc_decompress_ccode( &compimage, &image, analyze-1 ) ) // Create analysis image
return 2;
}
if( ! ppm_write( &image, outfile ) ) // Write decompressed image to file
return 2;
if( verbose )
fprintf( stderr, "Width:%i, Height:%i, Type:(T:%i,Y:%i,S:%i,M:%i)\n", compimage.width, compimage.height,
compimage.transform, compimage.colordiff, compimage.minsize, compimage.maxdepth );
image_free( &image );
qti_free( &compimage );
free( infile );
free( outfile );
return 0;
}