-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.cpp
79 lines (64 loc) · 2.02 KB
/
main.cpp
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
#include <cmath>
#include "Utility.hpp"
#include "Decoder.hpp"
void printHelp()
{
std::cout << "===========================================" << std::endl;
std::cout << " K-PEG - Simple JPEG Encoder & Decoder" << std::endl;
std::cout << "===========================================" << std::endl;
std::cout << "Help\n" << std::endl;
std::cout << "<filename.jpg> : Decompress a JPEG image to a PPM image" << std::endl;
std::cout << "-h : Print this help message and exit" << std::endl;
}
void decodeJPEG(const std::string& filename)
{
if ( !kpeg::utils::isValidFilename( filename ) )
{
std::cout << "Invalid input file name passed." << std::endl;
return;
}
std::cout << "Decoding..." << std::endl;
kpeg::Decoder decoder;
decoder.open( filename );
if ( decoder.decodeImageFile() == kpeg::Decoder::ResultCode::DECODE_DONE )
{
decoder.dumpRawData();
}
decoder.close();
std::cout << "Generated file: " << filename.substr(0, filename.length() - 3 ) << ".ppm" << std::endl;
std::cout << "Complete! Check log file \'kpeg.log\' for details." << std::endl;
}
int handleInput(int argc, char** argv)
{
if ( argc < 2 )
{
std::cout << "No arguments provided." << std::endl;
return EXIT_FAILURE;
}
if ( argc == 2 && (std::string)argv[1] == "-h" )
{
printHelp();
return EXIT_SUCCESS;
}
else if ( argc == 2 )
{
decodeJPEG( argv[1] );
return EXIT_SUCCESS;
}
std::cout << "Incorrect usage, use -h to view help" << std::endl;
return EXIT_FAILURE;
}
int main( int argc, char** argv )
{
try
{
logFile << "lilbKPEG - A simple JPEG library" << std::endl;
return handleInput(argc, argv);
}
catch( std::exception& e )
{
std::cout << "Exceptions Occurred:-" << std::endl;
std::cout << "What: " << e.what() << std::endl;
}
return EXIT_SUCCESS;
}