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
25 #include "bcsignals.h"
28 #include "filesystem.h"
29 #include "interlacemodes.h"
31 #include "mwindow.inc"
34 #include "ImfChannelList.h"
35 #include "ImfChromaticities.h"
36 #include "ImfCompression.h"
38 #include "ImfInputFile.h"
39 #include "ImfOutputFile.h"
40 #include "ImfPixelType.h"
41 #include "ImfRgbaFile.h"
42 #include "ImfRgbaYca.h"
43 #include "ImfVersion.h"
45 class EXRIStream : public Imf::IStream
48 EXRIStream(char *data, int size);
51 bool read (char c[], int n);
53 void seekg (Imf::Int64 pos);
62 class EXROStream : public Imf::OStream
65 EXROStream(VFrame *data);
68 virtual void write(const char c[], int n);
69 virtual Imf::Int64 tellp();
70 virtual void seekp(Imf::Int64 pos);
79 EXRIStream::EXRIStream(char *data, int size)
80 : Imf::IStream("mypath")
87 EXRIStream::~EXRIStream()
91 bool EXRIStream::read(char c[], int n)
94 if(position + fragment > size)
96 fragment = size - position;
98 memcpy(c, data + position, fragment);
103 throw Iex::InputExc ("EXRIStream::read: Unexpected end of file.");
105 return position >= size;
108 Imf::Int64 EXRIStream::tellg ()
113 void EXRIStream::seekg(Imf::Int64 pos)
118 void EXRIStream::clear()
132 EXROStream::EXROStream(VFrame *data)
133 : Imf::OStream("mypath")
138 EXROStream::~EXROStream()
142 void EXROStream::write(const char c[], int n)
144 if(position + n > data->get_compressed_allocated())
145 data->allocate_compressed_data(MAX(position + n, data->get_compressed_allocated() * 2));
147 memcpy(data->get_data() + position, c, n);
149 data->set_compressed_size(MAX(position, data->get_compressed_size()));
152 Imf::Int64 EXROStream::tellp()
157 void EXROStream::seekp(Imf::Int64 pos)
172 FileEXR::FileEXR(Asset *asset, File *file)
173 : FileList(asset, file, "EXRLIST", ".exr", FILE_EXR, FILE_EXR_LIST)
175 native_cmodel = BC_RGB_FLOAT;
184 if(temp_y) delete [] temp_y;
185 if(temp_u) delete [] temp_u;
186 if(temp_v) delete [] temp_v;
189 const char* FileEXR::compression_to_str(int compression)
193 case FileEXR::NONE: return "None"; break;
194 case FileEXR::PIZ: return "PIZ"; break;
195 case FileEXR::ZIP: return "ZIP"; break;
196 case FileEXR::ZIPS: return "ZIPS"; break;
197 case FileEXR::RLE: return "RLE"; break;
198 case FileEXR::PXR24: return "PXR24"; break;
203 int FileEXR::compression_to_exr(int compression)
207 case FileEXR::NONE: return (int)Imf::NO_COMPRESSION; break;
208 case FileEXR::PIZ: return (int)Imf::PIZ_COMPRESSION; break;
209 case FileEXR::ZIP: return (int)Imf::ZIP_COMPRESSION; break;
210 case FileEXR::ZIPS: return (int)Imf::ZIPS_COMPRESSION; break;
211 case FileEXR::RLE: return (int)Imf::RLE_COMPRESSION; break;
212 case FileEXR::PXR24: return (int)Imf::PXR24_COMPRESSION; break;
214 return Imf::NO_COMPRESSION;
217 int FileEXR::str_to_compression(char *string)
219 if(!strcmp(compression_to_str(FileEXR::NONE), string))
220 return FileEXR::NONE;
221 if(!strcmp(compression_to_str(FileEXR::PIZ), string))
223 if(!strcmp(compression_to_str(FileEXR::ZIP), string))
225 if(!strcmp(compression_to_str(FileEXR::ZIPS), string))
226 return FileEXR::ZIPS;
227 if(!strcmp(compression_to_str(FileEXR::RLE), string))
229 if(!strcmp(compression_to_str(FileEXR::PXR24), string))
231 return FileEXR::NONE;
234 int FileEXR::check_sig(Asset *asset, char *test)
236 if(Imf::isImfMagic(test)) return 1;
237 if(test[0] == 'E' && test[1] == 'X' && test[2] == 'R' &&
238 test[3] == 'L' && test[4] == 'I' && test[5] == 'S' && test[6] == 'T')
246 void FileEXR::get_parameters(BC_WindowBase *parent_window,
248 BC_WindowBase* &format_window,
254 EXRConfigVideo *window = new EXRConfigVideo(parent_window, asset);
255 format_window = window;
256 window->create_objects();
257 window->run_window();
262 int FileEXR::colormodel_supported(int colormodel)
264 return native_cmodel;
267 int FileEXR::get_best_colormodel(Asset *asset, int driver)
269 if(asset->exr_use_alpha)
270 return BC_RGBA_FLOAT;
275 int64_t FileEXR::get_memory_usage()
277 int64_t result = FileList::get_memory_usage();
278 if(temp_y) result += (int64_t)asset->width * asset->height * 3 / 2;
283 int FileEXR::read_frame_header(char *path)
287 // This may have been used by VFS
290 // if(!(stream = fopen(path, "rb")))
292 // perror("FileEXR::read_frame_header");
295 // int size = FileSystem::get_size(path);
296 // char *buffer = new char[size];
297 // fread(buffer, size, 1, stream);
300 // EXRIStream exr_stream(buffer, size);
301 // Imf::InputFile file(exr_stream);
304 Imf::InputFile file(path);
306 Imath::Box2i dw = file.header().dataWindow();
308 asset->width = dw.max.x - dw.min.x + 1;
309 asset->height = dw.max.y - dw.min.y + 1;
310 asset->interlace_mode = ILACE_MODE_NOTINTERLACED;
312 const Imf::ChannelList &channels = file.header().channels();
314 if(channels.findChannel("A"))
315 native_cmodel = BC_RGBA_FLOAT;
317 native_cmodel = BC_RGB_FLOAT;
318 asset->exr_use_alpha = BC_CModels::has_alpha(native_cmodel) ? 1 : 0;
320 if(channels.findChannel("Y"))
322 // for (Imf::ChannelList::ConstIterator i = channels.begin(); i != channels.end(); ++i)
324 // printf("%s\n", i.name());
331 int FileEXR::read_frame(VFrame *frame, VFrame *data)
333 EXRIStream exr_stream((char*)data->get_data(), data->get_compressed_size());
334 Imf::InputFile file(exr_stream);
335 Imath::Box2i dw = file.header().dataWindow();
338 Imf::FrameBuffer framebuffer;
339 float **rows = (float**)frame->get_rows();
340 int components = BC_CModels::components(frame->get_color_model());
344 if(!temp_y) temp_y = new float[asset->width * asset->height];
345 if(!temp_u) temp_u = new float[asset->width * asset->height / 4];
346 if(!temp_v) temp_v = new float[asset->width * asset->height / 4];
347 framebuffer.insert("Y", Imf::Slice(Imf::FLOAT,
348 (char*)(temp_y - dy * asset->width - dx),
350 sizeof(float) * frame->get_w()));
351 framebuffer.insert("BY", Imf::Slice(Imf::FLOAT,
352 (char*)(temp_u - dy * asset->width / 4 - dx / 2),
354 sizeof(float) * frame->get_w() / 2,
357 framebuffer.insert("RY", Imf::Slice(Imf::FLOAT,
358 (char*)(temp_v - dy * asset->width / 4 - dx / 2),
360 sizeof(float) * frame->get_w() / 2,
366 framebuffer.insert("R", Imf::Slice(Imf::FLOAT,
367 (char*)(&rows[-dy][-dx * components]),
368 sizeof(float) * components,
369 sizeof(float) * components * frame->get_w()));
370 framebuffer.insert("G", Imf::Slice(Imf::FLOAT,
371 (char*)(&rows[-dy][-dx * components + 1]),
372 sizeof(float) * components,
373 sizeof(float) * components * frame->get_w()));
374 framebuffer.insert("B", Imf::Slice(Imf::FLOAT,
375 (char*)(&rows[-dy][-dx * components + 2]),
376 sizeof(float) * components,
377 sizeof(float) * components * frame->get_w()));
380 // Alpha always goes directly to the output frame
383 framebuffer.insert("A", Imf::Slice(Imf::FLOAT,
384 (char*)(&rows[-dy][-dx * components + 3]),
385 sizeof(float) * components,
386 sizeof(float) * components * frame->get_w()));
389 file.setFrameBuffer(framebuffer);
390 file.readPixels (dw.min.y, dw.max.y);
394 // Convert to RGB using crazy ILM equations
396 Imf::Chromaticities cr;
397 yw = Imf::RgbaYca::computeYw(cr);
399 for(int i = 0; i < asset->height - 1; i += 2)
401 float *y_row1 = temp_y + i * asset->width;
402 float *y_row2 = temp_y + (i + 1) * asset->width;
403 float *u_row = temp_u + (i * asset->width / 4);
404 float *v_row = temp_v + (i * asset->width / 4);
405 float *out_row1 = rows[i];
406 float *out_row2 = rows[i + 1];
407 for(int j = 0; j < asset->width - 1; j += 2)
417 g = (y - r * yw.x - b * yw.z) / yw.y;
421 if(components == 4) out_row1++;
426 g = (y - r * yw.x - b * yw.z) / yw.y;
430 if(components == 4) out_row1++;
435 g = (y - r * yw.x - b * yw.z) / yw.y;
439 if(components == 4) out_row1++;
444 g = (y - r * yw.x - b * yw.z) / yw.y;
448 if(components == 4) out_row1++;
458 int FileEXR::write_frame(VFrame *frame, VFrame *data, FrameWriterUnit *unit)
460 EXRUnit *exr_unit = (EXRUnit*)unit;
462 VFrame *output_frame;
463 data->set_compressed_size(0);
466 int native_cmodel = asset->exr_use_alpha ? BC_RGBA_FLOAT : BC_RGB_FLOAT;
467 int components = BC_CModels::components(native_cmodel);
469 if(frame->get_color_model() != native_cmodel)
471 if(!exr_unit->temp_frame) exr_unit->temp_frame =
472 new VFrame(asset->width, asset->height, native_cmodel, 0);
473 BC_CModels::transfer(exr_unit->temp_frame->get_rows(), /* Leave NULL if non existent */
475 exr_unit->temp_frame->get_y(), /* Leave NULL if non existent */
476 exr_unit->temp_frame->get_u(),
477 exr_unit->temp_frame->get_v(),
478 frame->get_y(), /* Leave NULL if non existent */
481 0, /* Dimensions to capture from input frame */
485 0, /* Dimensions to project on output frame */
489 frame->get_color_model(),
491 0, /* When transfering BC_RGBA8888 to non-alpha this is the background color in 0xRRGGBB hex */
492 asset->width, /* For planar use the luma rowspan */
494 output_frame = exr_unit->temp_frame;
497 output_frame = frame;
499 Imf::Header header(output_frame->get_w(), output_frame->get_h());
500 header.compression() = (Imf::Compression)compression_to_exr(
501 asset->exr_compression);
502 header.channels().insert("R", Imf::Channel(Imf::FLOAT));
503 header.channels().insert("G", Imf::Channel(Imf::FLOAT));
504 header.channels().insert("B", Imf::Channel(Imf::FLOAT));
505 if(asset->exr_use_alpha) header.channels().insert("A", Imf::Channel(Imf::FLOAT));
507 EXROStream exr_stream(data);
508 Imf::OutputFile file(exr_stream, header);
509 Imf::FrameBuffer framebuffer;
510 float **rows = (float**)output_frame->get_rows();
511 framebuffer.insert("R",
512 Imf::Slice(Imf::FLOAT,
514 sizeof(float) * components,
515 sizeof(float) * components * output_frame->get_w()));
516 framebuffer.insert("G",
517 Imf::Slice(Imf::FLOAT,
518 (char*)(rows[0] + 1),
519 sizeof(float) * components,
520 sizeof(float) * components * output_frame->get_w()));
521 framebuffer.insert("B",
522 Imf::Slice(Imf::FLOAT,
523 (char*)(rows[0] + 2),
524 sizeof(float) * components,
525 sizeof(float) * components * output_frame->get_w()));
526 if(asset->exr_use_alpha)
527 framebuffer.insert("A",
528 Imf::Slice(Imf::FLOAT,
529 (char*)(rows[0] + 3),
530 sizeof(float) * components,
531 sizeof(float) * components * output_frame->get_w()));
532 file.setFrameBuffer(framebuffer);
533 file.writePixels(asset->height);
537 FrameWriterUnit* FileEXR::new_writer_unit(FrameWriter *writer)
539 return new EXRUnit(this, writer);
553 EXRUnit::EXRUnit(FileEXR *file, FrameWriter *writer)
554 : FrameWriterUnit(writer)
562 if(temp_frame) delete temp_frame;
575 EXRConfigVideo::EXRConfigVideo(BC_WindowBase *parent_window, Asset *asset)
576 : BC_Window(_(PROGRAM_NAME ": Video Compression"),
577 parent_window->get_abs_cursor_x(1),
578 parent_window->get_abs_cursor_y(1),
580 BC_OKButton::calculate_h() + 100)
582 this->parent_window = parent_window;
586 EXRConfigVideo::~EXRConfigVideo()
590 void EXRConfigVideo::create_objects()
592 lock_window("EXRConfigVideo::create_objects");
594 add_subwindow(new EXRUseAlpha(this, x, y));
596 EXRCompression *menu;
597 add_subwindow(new BC_Title(x, y, _("Compression:")));
599 add_subwindow(menu = new EXRCompression(this, x, y, 100));
600 menu->create_objects();
601 add_subwindow(new BC_OKButton(this));
606 int EXRConfigVideo::close_event()
613 EXRUseAlpha::EXRUseAlpha(EXRConfigVideo *gui, int x, int y)
614 : BC_CheckBox(x, y, gui->asset->exr_use_alpha, _("Use alpha"))
619 int EXRUseAlpha::handle_event()
621 gui->asset->exr_use_alpha = get_value();
627 EXRCompression::EXRCompression(EXRConfigVideo *gui, int x, int y, int w)
631 FileEXR::compression_to_str(gui->asset->exr_compression))
635 void EXRCompression::create_objects()
637 add_item(new EXRCompressionItem(gui, FileEXR::NONE));
638 add_item(new EXRCompressionItem(gui, FileEXR::PIZ));
639 add_item(new EXRCompressionItem(gui, FileEXR::ZIP));
640 add_item(new EXRCompressionItem(gui, FileEXR::ZIPS));
641 add_item(new EXRCompressionItem(gui, FileEXR::RLE));
642 add_item(new EXRCompressionItem(gui, FileEXR::PXR24));
645 int EXRCompression::handle_event()
650 EXRCompressionItem::EXRCompressionItem(EXRConfigVideo *gui, int value)
651 : BC_MenuItem(FileEXR::compression_to_str(value))
657 int EXRCompressionItem::handle_event()
659 gui->asset->exr_compression = value;