Fix integer overflow in ft_rendered_size_line
[ilaris-y4m-tools.git] / imgload.cpp
blob2d94eaec9e29004b56e6491224d2cf59e92a0f7e
1 #include "yuv4mpeg.hpp"
2 #include "parseval.hpp"
3 #include "png.hpp"
4 #include <iostream>
6 int main(int argc, char** argv)
8 bool ispec = false;
9 std::string imgfile;
10 uint32_t fps_n = 1;
11 uint32_t fps_d = 2;
12 char interlace = 'p';
13 for(int i = 1; i < argc; i++) {
14 std::string arg = argv[i];
15 regex_results r;
16 if(r = regex("--duration=([1-9][0-9]*)", arg)) {
17 try {
18 fps_n = 1;
19 fps_d = parse_value<unsigned>(r[1]);
20 } catch(std::exception& e) {
21 std::cerr << "imgload: Bad duration '" << r[1] << "'" << std::endl;
22 return 1;
24 } else if(r = regex("--duration=([1-9][0-9]*)/([1-9][0-9]*)", arg)) {
25 try {
26 fps_n = parse_value<unsigned>(r[2]);
27 fps_d = parse_value<unsigned>(r[1]);
28 } catch(std::exception& e) {
29 std::cerr << "imgload: Bad duration '" << r[1] << "/" << r[2] << "'" << std::endl;
30 return 1;
32 } else if(r = regex("--interlace=top", arg)) {
33 interlace = 't';
34 } else if(r = regex("--interlace=bottom", arg)) {
35 interlace = 'b';
36 } else if(r = regex("--.*", arg)) {
37 std::cerr << "imgload: Unrecognized option '" << arg << "'" << std::endl;
38 return 1;
39 } else {
40 if(ispec) {
41 std::cerr << "imgload: Only one image can be specified" << std::endl;
42 return 1;
44 ispec = true;
45 imgfile = arg;
48 if(!ispec) {
49 std::cerr << "imgload: Image to load required" << std::endl;
50 return 1;
53 //Open files.
54 FILE* in = fopen(imgfile.c_str(), "rb");
55 if(!in) {
56 std::cerr << "imgload: Can't open '" << imgfile << "'" << std::endl;
57 return 1;
59 FILE* out = stdout;
60 mark_pipe_as_binary(out);
62 try {
63 regex_results r;
64 yuv4mpeg_stream_header phdr;
66 parsed_png image(imgfile);
68 phdr.width = image.width;
69 phdr.height = image.height;
70 phdr.chroma = "rgb";
71 phdr.interlace = interlace;
72 phdr.fps_n = fps_n;
73 phdr.fps_d = fps_d;
74 write_or_die(out, static_cast<std::string>(phdr));
76 yuv4mpeg_frame_header framh;
77 std::vector<char> buffer;
78 buffer.resize(3 * phdr.width * phdr.height);
80 for(size_t i = 0; i < phdr.width * phdr.height; i++) {
81 buffer[3 * i + 0] = image.data[i];
82 buffer[3 * i + 1] = image.data[i] >> 8;
83 buffer[3 * i + 2] = image.data[i] >> 16;
86 write_or_die(out, std::string(framh));
87 write_or_die(out, &buffer[0], buffer.size());
90 } catch(std::exception& e) {
91 std::cerr << "imgload: Error: " << e.what() << std::endl;
92 return 1;
94 return 0;