4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
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
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "bccmodels.h"
26 #include "interlacemodes.h"
27 #include "mainerror.h"
33 FilePPM::FilePPM(Asset *asset, File *file)
34 : FileList(asset, file, "PPMLIST", ".ppm", FILE_PPM, FILE_PPM_LIST)
37 if( asset->format == FILE_UNKNOWN )
38 asset->format = FILE_PPM;
50 int FilePPM::check_sig(Asset *asset)
52 FILE *stream = fopen(asset->path, "r");
55 (void)fread(test, 10, 1, stream);
57 if( !strncmp("PPMLIST",test,6) ) return 1;
58 if( !strncmp("P6\n",test,3) ) return 1;
63 int FilePPM::check_frame_header(FILE *fp)
66 if( !fgets(text, sizeof(text), fp) ) return 1;
67 if( strcmp("P6\n",text) ) return 1;
69 while( ch == '#' ) { while( (ch=getc(fp))>=0 && ch!='\n' ); }
72 if( !fgets(text, sizeof(text), fp) ||
73 sscanf(text, "%d %d\n", &w, &h) != 2 ) return 1;
74 if( !fgets(text, sizeof(text), fp) ||
75 sscanf(text, "%d\n", &ch) != 1 || ch != 255 ) return 1;
77 asset->width = w; asset->height = h;
78 asset->interlace_mode = ILACE_MODE_NOTINTERLACED;
82 int FilePPM::read_frame_header(char *path)
85 FILE *fp = fopen(path, "r");
87 ret = check_frame_header(fp);
93 int FilePPM::read_ppm(FILE *fp, VFrame *frame)
97 if( !fgets(text, sizeof(text), fp) ) return 1;
98 if( strcmp("P6\n",text) ) {
99 printf("FilePPM::read_ppm: header err\n");
102 while( (ch=getc(fp)) == '#' ) { while( (ch=getc(fp))>=0 && ch!='\n' ); }
105 if( !fgets(text, sizeof(text), fp) ||
106 sscanf(text, "%d %d\n", &w, &h) != 2 ) {
107 printf("FilePPM::read_ppm: geom err\n");
110 if( w != frame->get_w() || h != frame->get_h() ) {
111 printf("FilePPM::read_ppm: geom mismatch\n");
114 if( !fgets(text, sizeof(text), fp) ||
115 sscanf(text, "%d\n", &ch) != 1 || ch != 255 ) {
116 printf("FilePPM::read_ppm: mask err\n");
120 unsigned char **rows = frame->get_rows();
122 for( int y=0; y<h; ++y ) {
123 int ret = fread(rows[y],1,bpl,fp);
125 printf("FilePPM::read_ppm: read (%d,%d) err: %m\n", bpl, ret);
132 int FilePPM::read_frame(VFrame *frame, char *path)
135 FILE *fp = fopen(path,"r");
137 result = read_ppm(fp, frame);
141 eprintf("FilePPM::read_frame: cant read file %s\n", path);
145 PPMUnit::PPMUnit(FilePPM *file, FrameWriter *writer)
146 : FrameWriterUnit(writer)
156 FrameWriterUnit* FilePPM::new_writer_unit(FrameWriter *writer)
158 return new PPMUnit(this, writer);
161 int FilePPM::write_frame(VFrame *frame, VFrame *output,
162 FrameWriterUnit *frame_writer_unit)
164 int w = asset->width, h = asset->height;
165 char prefix[BCTEXTLEN];
166 int pfx = sprintf(prefix, "P6\n%d %d\n%d\n", w, h, 255);
167 int bpl = 3*w, image_length = h*bpl, total_length = pfx + image_length;
168 if( output->get_compressed_allocated() < total_length ) {
169 int new_length = total_length + 255;
170 output->allocate_compressed_data(new_length);
172 unsigned char *dp = output->get_data(), *bp = dp;
173 memcpy(dp, prefix, pfx); dp += pfx;
174 unsigned char *rows[h+1], **rp = rows;
175 for( int y=h; --y>=0; dp+=bpl ) *rp++ = dp;
176 BC_CModels::transfer(rows, frame->get_rows(),
177 0, 0, 0, frame->get_y(), frame->get_u(), frame->get_v(),
178 0, 0, frame->get_w(), frame->get_h(), 0, 0, w, h,
179 frame->get_color_model(), BC_RGB888, 0,
180 frame->get_bytes_per_line(), bpl);
182 output->set_compressed_size(dp - bp);
186 int FilePPM::colormodel_supported(int colormodel)
191 int FilePPM::get_best_colormodel(Asset *asset, int driver)
196 PPMConfigVideo::PPMConfigVideo(BC_WindowBase *gui, Asset *asset)
197 : BC_Window(_(PROGRAM_NAME ": Video Compression"),
198 gui->get_abs_cursor_x(1), gui->get_abs_cursor_y(1), 200, 100)
204 void PPMConfigVideo::create_objects()
206 lock_window("PPMConfigVideo::create_objects");
208 add_subwindow(new BC_Title(x, y, _("PPM, RGB raw only")));
209 add_subwindow(new BC_OKButton(this));
214 void FilePPM::get_parameters(BC_WindowBase *parent_window,
215 Asset *asset, BC_WindowBase* &format_window,
216 int audio_options, int video_options, EDL *edl)
219 PPMConfigVideo *window = new PPMConfigVideo(parent_window, asset);
220 window->create_objects();
221 format_window = window;
222 window->run_window();
227 int FilePPM::use_path()