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 "bcsignals.h"
26 #include "filesystem.h"
27 #include "interlacemodes.h"
29 #include "mwindow.inc"
32 #include "ImfChannelList.h"
33 #include "ImfChromaticities.h"
34 #include "ImfCompression.h"
36 #include "ImfInputFile.h"
37 #include "ImfOutputFile.h"
38 #include "ImfPixelType.h"
39 #include "ImfRgbaFile.h"
40 #include "ImfRgbaYca.h"
41 #include "ImfVersion.h"
43 class EXRIStream : public Imf::IStream
46 EXRIStream(char *data, int size);
49 bool read (char c[], int n);
51 void seekg (Imf::Int64 pos);
60 class EXROStream : public Imf::OStream
63 EXROStream(VFrame *data);
66 virtual void write(const char c[], int n);
67 virtual Imf::Int64 tellp();
68 virtual void seekp(Imf::Int64 pos);
77 EXRIStream::EXRIStream(char *data, int size)
78 : Imf::IStream("mypath")
85 EXRIStream::~EXRIStream()
89 bool EXRIStream::read(char c[], int n)
92 if(position + fragment > size)
94 fragment = size - position;
96 memcpy(c, data + position, fragment);
101 throw Iex::InputExc ("EXRIStream::read: Unexpected end of file.");
103 return position >= size;
106 Imf::Int64 EXRIStream::tellg ()
111 void EXRIStream::seekg(Imf::Int64 pos)
116 void EXRIStream::clear()
130 EXROStream::EXROStream(VFrame *data)
131 : Imf::OStream("mypath")
136 EXROStream::~EXROStream()
140 void EXROStream::write(const char c[], int n)
142 if(position + n > data->get_compressed_allocated())
143 data->allocate_compressed_data(MAX(position + n, data->get_compressed_allocated() * 2));
145 memcpy(data->get_data() + position, c, n);
147 data->set_compressed_size(MAX(position, data->get_compressed_size()));
150 Imf::Int64 EXROStream::tellp()
155 void EXROStream::seekp(Imf::Int64 pos)
170 FileEXR::FileEXR(Asset *asset, File *file)
171 : FileList(asset, file, "EXRLIST", ".exr", FILE_EXR, FILE_EXR_LIST)
173 native_cmodel = BC_RGB_FLOAT;
182 if(temp_y) delete [] temp_y;
183 if(temp_u) delete [] temp_u;
184 if(temp_v) delete [] temp_v;
187 const char* FileEXR::compression_to_str(int compression)
191 case FileEXR::NONE: return "None"; break;
192 case FileEXR::PIZ: return "PIZ"; break;
193 case FileEXR::ZIP: return "ZIP"; break;
194 case FileEXR::ZIPS: return "ZIPS"; break;
195 case FileEXR::RLE: return "RLE"; break;
196 case FileEXR::PXR24: return "PXR24"; break;
201 int FileEXR::compression_to_exr(int compression)
205 case FileEXR::NONE: return (int)Imf::NO_COMPRESSION; break;
206 case FileEXR::PIZ: return (int)Imf::PIZ_COMPRESSION; break;
207 case FileEXR::ZIP: return (int)Imf::ZIP_COMPRESSION; break;
208 case FileEXR::ZIPS: return (int)Imf::ZIPS_COMPRESSION; break;
209 case FileEXR::RLE: return (int)Imf::RLE_COMPRESSION; break;
210 case FileEXR::PXR24: return (int)Imf::PXR24_COMPRESSION; break;
212 return Imf::NO_COMPRESSION;
215 int FileEXR::str_to_compression(char *string)
217 if(!strcmp(compression_to_str(FileEXR::NONE), string))
218 return FileEXR::NONE;
219 if(!strcmp(compression_to_str(FileEXR::PIZ), string))
221 if(!strcmp(compression_to_str(FileEXR::ZIP), string))
223 if(!strcmp(compression_to_str(FileEXR::ZIPS), string))
224 return FileEXR::ZIPS;
225 if(!strcmp(compression_to_str(FileEXR::RLE), string))
227 if(!strcmp(compression_to_str(FileEXR::PXR24), string))
229 return FileEXR::NONE;
232 int FileEXR::check_sig(Asset *asset, char *test)
234 if(Imf::isImfMagic(test)) return 1;
235 if(test[0] == 'E' && test[1] == 'X' && test[2] == 'R' &&
236 test[3] == 'L' && test[4] == 'I' && test[5] == 'S' && test[6] == 'T')
244 void FileEXR::get_parameters(BC_WindowBase *parent_window,
246 BC_WindowBase* &format_window,
252 EXRConfigVideo *window = new EXRConfigVideo(parent_window, asset);
253 format_window = window;
254 window->create_objects();
255 window->run_window();
260 int FileEXR::colormodel_supported(int colormodel)
262 return native_cmodel;
265 int FileEXR::get_best_colormodel(Asset *asset, int driver)
267 if(asset->exr_use_alpha)
268 return BC_RGBA_FLOAT;
273 int64_t FileEXR::get_memory_usage()
275 int64_t result = FileList::get_memory_usage();
276 if(temp_y) result += (int64_t)asset->width * asset->height * 3 / 2;
281 int FileEXR::read_frame_header(char *path)
285 // This may have been used by VFS
288 // if(!(stream = fopen(path, "rb")))
290 // perror("FileEXR::read_frame_header");
293 // int size = FileSystem::get_size(path);
294 // char *buffer = new char[size];
295 // fread(buffer, size, 1, stream);
298 // EXRIStream exr_stream(buffer, size);
299 // Imf::InputFile file(exr_stream);
302 Imf::InputFile file(path);
304 Imath::Box2i dw = file.header().dataWindow();
306 asset->width = dw.max.x - dw.min.x + 1;
307 asset->height = dw.max.y - dw.min.y + 1;
308 asset->interlace_mode = BC_ILACE_MODE_NOTINTERLACED;
310 const Imf::ChannelList &channels = file.header().channels();
312 if(channels.findChannel("A"))
313 native_cmodel = BC_RGBA_FLOAT;
315 native_cmodel = BC_RGB_FLOAT;
317 if(channels.findChannel("Y"))
319 // for (Imf::ChannelList::ConstIterator i = channels.begin(); i != channels.end(); ++i)
321 // printf("%s\n", i.name());
328 int FileEXR::read_frame(VFrame *frame, VFrame *data)
330 EXRIStream exr_stream((char*)data->get_data(), data->get_compressed_size());
331 Imf::InputFile file(exr_stream);
332 Imath::Box2i dw = file.header().dataWindow();
335 Imf::FrameBuffer framebuffer;
336 float **rows = (float**)frame->get_rows();
337 int components = BC_CModels::components(frame->get_color_model());
341 if(!temp_y) temp_y = new float[asset->width * asset->height];
342 if(!temp_u) temp_u = new float[asset->width * asset->height / 4];
343 if(!temp_v) temp_v = new float[asset->width * asset->height / 4];
344 framebuffer.insert("Y", Imf::Slice(Imf::FLOAT,
345 (char*)(temp_y - dy * asset->width - dx),
347 sizeof(float) * frame->get_w()));
348 framebuffer.insert("BY", Imf::Slice(Imf::FLOAT,
349 (char*)(temp_u - dy * asset->width / 4 - dx / 2),
351 sizeof(float) * frame->get_w() / 2,
354 framebuffer.insert("RY", Imf::Slice(Imf::FLOAT,
355 (char*)(temp_v - dy * asset->width / 4 - dx / 2),
357 sizeof(float) * frame->get_w() / 2,
363 framebuffer.insert("R", Imf::Slice(Imf::FLOAT,
364 (char*)(&rows[-dy][-dx * components]),
365 sizeof(float) * components,
366 sizeof(float) * components * frame->get_w()));
367 framebuffer.insert("G", Imf::Slice(Imf::FLOAT,
368 (char*)(&rows[-dy][-dx * components + 1]),
369 sizeof(float) * components,
370 sizeof(float) * components * frame->get_w()));
371 framebuffer.insert("B", Imf::Slice(Imf::FLOAT,
372 (char*)(&rows[-dy][-dx * components + 2]),
373 sizeof(float) * components,
374 sizeof(float) * components * frame->get_w()));
377 // Alpha always goes directly to the output frame
380 framebuffer.insert("A", Imf::Slice(Imf::FLOAT,
381 (char*)(&rows[-dy][-dx * components + 3]),
382 sizeof(float) * components,
383 sizeof(float) * components * frame->get_w()));
386 file.setFrameBuffer(framebuffer);
387 file.readPixels (dw.min.y, dw.max.y);
391 // Convert to RGB using crazy ILM equations
393 Imf::Chromaticities cr;
394 yw = Imf::RgbaYca::computeYw(cr);
396 for(int i = 0; i < asset->height - 1; i += 2)
398 float *y_row1 = temp_y + i * asset->width;
399 float *y_row2 = temp_y + (i + 1) * asset->width;
400 float *u_row = temp_u + (i * asset->width / 4);
401 float *v_row = temp_v + (i * asset->width / 4);
402 float *out_row1 = rows[i];
403 float *out_row2 = rows[i + 1];
404 for(int j = 0; j < asset->width - 1; j += 2)
414 g = (y - r * yw.x - b * yw.z) / yw.y;
418 if(components == 4) out_row1++;
423 g = (y - r * yw.x - b * yw.z) / yw.y;
427 if(components == 4) out_row1++;
432 g = (y - r * yw.x - b * yw.z) / yw.y;
436 if(components == 4) out_row1++;
441 g = (y - r * yw.x - b * yw.z) / yw.y;
445 if(components == 4) out_row1++;
455 int FileEXR::write_frame(VFrame *frame, VFrame *data, FrameWriterUnit *unit)
457 EXRUnit *exr_unit = (EXRUnit*)unit;
459 VFrame *output_frame;
460 data->set_compressed_size(0);
463 int native_cmodel = asset->exr_use_alpha ? BC_RGBA_FLOAT : BC_RGB_FLOAT;
464 int components = BC_CModels::components(native_cmodel);
466 if(frame->get_color_model() != native_cmodel)
468 if(!exr_unit->temp_frame) exr_unit->temp_frame = new VFrame(0,
474 BC_CModels::transfer(exr_unit->temp_frame->get_rows(), /* Leave NULL if non existent */
476 exr_unit->temp_frame->get_y(), /* Leave NULL if non existent */
477 exr_unit->temp_frame->get_u(),
478 exr_unit->temp_frame->get_v(),
479 frame->get_y(), /* Leave NULL if non existent */
482 0, /* Dimensions to capture from input frame */
486 0, /* Dimensions to project on output frame */
490 frame->get_color_model(),
492 0, /* When transfering BC_RGBA8888 to non-alpha this is the background color in 0xRRGGBB hex */
493 asset->width, /* For planar use the luma rowspan */
495 output_frame = exr_unit->temp_frame;
498 output_frame = frame;
500 Imf::Header header(output_frame->get_w(), output_frame->get_h());
501 header.compression() = (Imf::Compression)compression_to_exr(
502 asset->exr_compression);
503 header.channels().insert("R", Imf::Channel(Imf::FLOAT));
504 header.channels().insert("G", Imf::Channel(Imf::FLOAT));
505 header.channels().insert("B", Imf::Channel(Imf::FLOAT));
506 if(asset->exr_use_alpha) header.channels().insert("A", Imf::Channel(Imf::FLOAT));
508 EXROStream exr_stream(data);
509 Imf::OutputFile file(exr_stream, header);
510 Imf::FrameBuffer framebuffer;
511 float **rows = (float**)output_frame->get_rows();
512 framebuffer.insert("R",
513 Imf::Slice(Imf::FLOAT,
515 sizeof(float) * components,
516 sizeof(float) * components * output_frame->get_w()));
517 framebuffer.insert("G",
518 Imf::Slice(Imf::FLOAT,
519 (char*)(rows[0] + 1),
520 sizeof(float) * components,
521 sizeof(float) * components * output_frame->get_w()));
522 framebuffer.insert("B",
523 Imf::Slice(Imf::FLOAT,
524 (char*)(rows[0] + 2),
525 sizeof(float) * components,
526 sizeof(float) * components * output_frame->get_w()));
527 if(asset->exr_use_alpha)
528 framebuffer.insert("A",
529 Imf::Slice(Imf::FLOAT,
530 (char*)(rows[0] + 3),
531 sizeof(float) * components,
532 sizeof(float) * components * output_frame->get_w()));
533 file.setFrameBuffer(framebuffer);
534 file.writePixels(asset->height);
538 FrameWriterUnit* FileEXR::new_writer_unit(FrameWriter *writer)
540 return new EXRUnit(this, writer);
554 EXRUnit::EXRUnit(FileEXR *file, FrameWriter *writer)
555 : FrameWriterUnit(writer)
563 if(temp_frame) delete temp_frame;
576 EXRConfigVideo::EXRConfigVideo(BC_WindowBase *parent_window, Asset *asset)
577 : BC_Window(_(PROGRAM_NAME ": Video Compression"),
578 parent_window->get_abs_cursor_x(1),
579 parent_window->get_abs_cursor_y(1),
581 BC_OKButton::calculate_h() + 100)
583 this->parent_window = parent_window;
587 EXRConfigVideo::~EXRConfigVideo()
591 void EXRConfigVideo::create_objects()
593 lock_window("EXRConfigVideo::create_objects");
595 add_subwindow(new EXRUseAlpha(this, x, y));
597 EXRCompression *menu;
598 add_subwindow(new BC_Title(x, y, _("Compression:")));
600 add_subwindow(menu = new EXRCompression(this, x, y, 100));
601 menu->create_objects();
602 add_subwindow(new BC_OKButton(this));
607 int EXRConfigVideo::close_event()
614 EXRUseAlpha::EXRUseAlpha(EXRConfigVideo *gui, int x, int y)
615 : BC_CheckBox(x, y, gui->asset->exr_use_alpha, _("Use alpha"))
620 int EXRUseAlpha::handle_event()
622 gui->asset->exr_use_alpha = get_value();
628 EXRCompression::EXRCompression(EXRConfigVideo *gui, int x, int y, int w)
632 FileEXR::compression_to_str(gui->asset->exr_compression))
636 void EXRCompression::create_objects()
638 add_item(new EXRCompressionItem(gui, FileEXR::NONE));
639 add_item(new EXRCompressionItem(gui, FileEXR::PIZ));
640 add_item(new EXRCompressionItem(gui, FileEXR::ZIP));
641 add_item(new EXRCompressionItem(gui, FileEXR::ZIPS));
642 add_item(new EXRCompressionItem(gui, FileEXR::RLE));
643 add_item(new EXRCompressionItem(gui, FileEXR::PXR24));
646 int EXRCompression::handle_event()
651 EXRCompressionItem::EXRCompressionItem(EXRConfigVideo *gui, int value)
652 : BC_MenuItem(FileEXR::compression_to_str(value))
658 int EXRCompressionItem::handle_event()
660 gui->asset->exr_compression = value;