1 #include "subtitler.h"
2 
3 extern int execute(char *);
4 
5 char subtitles_dir[] = "";
6 
7 
change_picture_geometry(char * data,int xsize,int ysize,double * new_xsize,double * new_ysize,int keep_aspect,double zrotation,double xshear,double yshear)8 char *change_picture_geometry(\
9 char *data, int xsize, int ysize, double *new_xsize, double *new_ysize,\
10 int keep_aspect,\
11 double zrotation,\
12 double xshear, double yshear)
13 {
14 int a, x, y;
15 char temp[1024];
16 char *ptr;
17 
18 /*
19 returns new data adjusted for geometry, calls Imagemagick package mogrify.
20 */
21 if(debug_flag)
22 	{
23 	tc_log_msg(MOD_NAME, "change_picture_geometry(): data=%lu xsize=%d ysize=%d\n\
24 	new_xsize=%.2f new_ysize=%.2f keep_aspect=%d\n\
25 	zrotation=%.2f xshear=%.2f yshear=%.2f\n",\
26 	(unsigned long)data, xsize, ysize,\
27 	*new_xsize, *new_ysize, keep_aspect,\
28 	zrotation,\
29 	xshear, yshear);
30 	}
31 
32 /* write data as a temp .ppm file */
33 tc_snprintf(temp, sizeof(temp), "%s/%s/temp.ppm", home_dir, subtitles_dir);
34 if(! yuv_to_ppm(data, xsize, ysize, temp) )
35 	{
36 	tc_log_msg(MOD_NAME, "subtitler(): change_picture_geometry(): yuv_to_ppm() error return\n");
37 
38 	return 0;
39 	}
40 
41 /*
42 NOTE to programmers: it seems mogrify (and possibly the other ImageMagic
43 programs) gets confused if you attempt ~/.subtitles/temp.ppm
44 It then thinks .subtitles is a picture type and reports:
45 
46 panteltje:/video/test# mogrify -geometry 352x288 /root/.subtitles/temp.ppm
47 mogrify: no encode delegate for this image format (SUBTITLES/MAGICRMGWIV).
48 
49 So, I am using home_dir now, although that is not a good way of doing this,
50 but /temp needs special write permissions....
51 */
52 
53 /* if '!' in mogrify, aspect is overruled */
54 if(keep_aspect) a = ' ';
55 else a = '!';
56 
57 /* change geometry temp file, this overwrites the temp file */
58 
59 if(xshear == 0.0)
60 
61 /* workaround bug in mogrify that causes exit if xshear is zero */
62 if(yshear != 0)
63 	{
64 	if(xshear == 0.0) xshear = 0.001;
65 	}
66 
67 if( (xshear != 0.0) || (yshear != 0.0) )
68 	{
69 	tc_snprintf(temp, sizeof(temp),\
70 "mogrify -geometry %dx%d%c  -rotate %.2f  -shear %.2fx%.2f  %s/%s/temp.ppm",\
71 	(int) *new_xsize, (int) *new_ysize, a,\
72 	zrotation,\
73 	xshear, yshear,\
74 	home_dir, subtitles_dir);
75 	}
76 else
77 	{
78 	tc_snprintf(temp, sizeof(temp),\
79 	"mogrify -geometry %dx%d%c  -rotate %.2f  %s/%s/temp.ppm",\
80 	(int) *new_xsize, (int) *new_ysize, a,\
81 	zrotation,\
82 	home_dir, subtitles_dir);
83 	}
84 
85 if(!execute(temp) ) return 0;
86 
87 /* load temp .ppm file */
88 tc_snprintf(temp, sizeof(temp), "%s/%s/temp.ppm", home_dir, subtitles_dir);
89 ptr = ppm_to_yuv_in_char(temp, &x, &y);
90 
91 *new_xsize = (double)x;
92 *new_ysize = (double)y;
93 
94 #if 0
95 	tc_log_msg(MOD_NAME, "WAS RELOAD x=%d y=%d *new_xsize=%.2f *new_ysize=%.2f\n",\
96 	x, y, *new_xsize, *new_ysize);
97 #endif
98 
99 return ptr;
100 } /* end function change_picture_geometry */
101 
102 
execute(char * command)103 int execute(char *command)
104 {
105 FILE *pptr;
106 
107 if(debug_flag)
108 	{
109 	tc_log_msg(MOD_NAME, "subtitler() execute(): arg command=%s\n", command);
110 	}
111 
112 pptr = popen(command, "r");
113 if(pptr <= 0)
114 	{
115 	tc_log_perror(MOD_NAME, "command");
116 
117 	return 0;
118 	}
119 
120 pclose(pptr);
121 
122 return 1;
123 } /* end function execute */
124 
125