Check events better
[jpcrr.git] / streamtools / letterbox.c
blobc2b9758ff30bf5f5cc6336cb46331697a4fb565e
1 /*
2 JPC-RR: A x86 PC Hardware Emulator
3 Release 1
5 Copyright (C) 2009 H. Ilari Liusvaara
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 2 as published by
9 the Free Software Foundation.
11 This program 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 along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 Based on JPC x86 PC Hardware emulator,
21 A project from the Physics Dept, The University of Oxford
23 Details about original JPC can be found at:
25 www-jpc.physics.ox.ac.uk
29 #include "frame.h"
30 #include "stdio.h"
31 #include <string.h>
32 #include <stdlib.h>
34 void letterbox_frame(struct frame* n, struct frame* o)
36 unsigned scalefactor;
37 unsigned src_left;
38 unsigned dst_left;
39 unsigned src_copyw;
40 unsigned src_top;
41 unsigned dst_top;
42 unsigned src_copyh;
44 scalefactor = n->f_width / o->f_width;
45 if(scalefactor > n->f_height / o->f_height)
46 scalefactor = n->f_height / o->f_height;
48 if(scalefactor == 0) {
49 src_left = o->f_width / 2 - n->f_width / 2;
50 src_top = o->f_height / 2 - n->f_height / 2;
51 dst_left = dst_top = 0;
52 src_copyw = n->f_width;
53 src_copyh = n->f_height;
54 scalefactor = 1;
55 } else {
56 src_left = src_top = 0;
57 dst_left = n->f_width / 2 - scalefactor * o->f_width / 2;
58 dst_top = n->f_height / 2 - scalefactor * o->f_height / 2;
59 src_copyw = o->f_width;
60 src_copyh = o->f_height;
64 fprintf(stderr, "Resize %u*%u -> %u*%u: src offset = %u*%u, dst offset = %u*%u "
65 "scale=%u.\n", o->f_width, o->f_height, n->f_width, n->f_height, src_left,
66 src_top, dst_left, dst_top, scalefactor);
68 for(unsigned y = 0; y < src_copyh; y++) {
69 for(unsigned i = 0; i < scalefactor; i++) {
70 unsigned dsty = y * scalefactor + i + dst_top;
71 for(unsigned x = 0; x < src_copyw; x++) {
72 int px = o->f_framedata[(y + src_top) * o->f_width + (x + src_left)];
73 for(unsigned j = 0; j < scalefactor; j++) {
74 unsigned dstx = x * scalefactor + j + dst_left;
75 n->f_framedata[dsty * n->f_width + dstx] = px;
82 int main(int argc, char** argv)
84 unsigned long width, height;
85 char* tmp;
87 if(argc < 5) {
88 fprintf(stderr, "usage: %s <in> <out> <width> <height>\n", argv[0]);
89 fprintf(stderr, "Letterbox the video stream read from <in> to be <width>x<height> and write the "
90 "resulting stream to <out>.\n");
91 return 1;
93 struct frame_input_stream* in = fis_open(argv[1]);
94 struct frame_output_stream* out = fos_open(argv[2]);
95 struct frame* frame;
96 int num = 1;
98 width = strtoul(argv[3], &tmp, 10);
99 if(*tmp) {
100 fprintf(stderr, "Bad width %s.\n", argv[3]);
101 return 1;
103 height = strtoul(argv[4], &tmp, 10);
104 if(*tmp) {
105 fprintf(stderr, "Bad height %s.\n", argv[4]);
106 return 1;
109 while(1) {
110 struct frame* newframe;
111 frame = fis_next_frame(in);
112 if(!frame)
113 break;
114 newframe = frame_create(width, height);
115 newframe->f_timeseq = frame->f_timeseq;
117 letterbox_frame(newframe, frame);
119 fos_save_frame(out, newframe);
120 frame_release(frame);
121 frame_release(newframe);
124 fis_close(in);
125 fos_close(out);
126 return 0;