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 "mwindow.inc"
31 #include "ImfChannelList.h"
32 #include "ImfChromaticities.h"
33 #include "ImfCompression.h"
35 #include "ImfInputFile.h"
36 #include "ImfOutputFile.h"
37 #include "ImfPixelType.h"
38 #include "ImfRgbaFile.h"
39 #include "ImfRgbaYca.h"
40 #include "ImfVersion.h"
42 class EXRIStream : public Imf::IStream
45 EXRIStream(char *data, int size);
48 bool read (char c[], int n);
50 void seekg (Imf::Int64 pos);
59 class EXROStream : public Imf::OStream
62 EXROStream(VFrame *data);
65 virtual void write(const char c[], int n);
66 virtual Imf::Int64 tellp();
67 virtual void seekp(Imf::Int64 pos);
76 EXRIStream::EXRIStream(char *data, int size)
77 : Imf::IStream("mypath")
84 EXRIStream::~EXRIStream()
88 bool EXRIStream::read(char c[], int n)
91 if(position + fragment > size)
93 fragment = size - position;
95 memcpy(c, data + position, fragment);
100 throw Iex::InputExc ("EXRIStream::read: Unexpected end of file.");
102 return position >= size;
105 Imf::Int64 EXRIStream::tellg ()
110 void EXRIStream::seekg(Imf::Int64 pos)
115 void EXRIStream::clear()
129 EXROStream::EXROStream(VFrame *data)
130 : Imf::OStream("mypath")
135 EXROStream::~EXROStream()
139 void EXROStream::write(const char c[], int n)
141 if(position + n > data->get_compressed_allocated())
142 data->allocate_compressed_data(MAX(position + n, data->get_compressed_allocated() * 2));
144 memcpy(data->get_data() + position, c, n);
146 data->set_compressed_size(MAX(position, data->get_compressed_size()));
149 Imf::Int64 EXROStream::tellp()
154 void EXROStream::seekp(Imf::Int64 pos)
169 FileEXR::FileEXR(Asset *asset, File *file)
170 : FileList(asset, file, "EXRLIST", ".exr", FILE_EXR, FILE_EXR_LIST)
172 native_cmodel = BC_RGB_FLOAT;
181 if(temp_y) delete [] temp_y;
182 if(temp_u) delete [] temp_u;
183 if(temp_v) delete [] temp_v;
186 const char* FileEXR::compression_to_str(int compression)
190 case FileEXR::NONE: return "None"; break;
191 case FileEXR::PIZ: return "PIZ"; break;
192 case FileEXR::ZIP: return "ZIP"; break;
193 case FileEXR::ZIPS: return "ZIPS"; break;
194 case FileEXR::RLE: return "RLE"; break;
195 case FileEXR::PXR24: return "PXR24"; break;
200 int FileEXR::compression_to_exr(int compression)
204 case FileEXR::NONE: return (int)Imf::NO_COMPRESSION; break;
205 case FileEXR::PIZ: return (int)Imf::PIZ_COMPRESSION; break;
206 case FileEXR::ZIP: return (int)Imf::ZIP_COMPRESSION; break;
207 case FileEXR::ZIPS: return (int)Imf::ZIPS_COMPRESSION; break;
208 case FileEXR::RLE: return (int)Imf::RLE_COMPRESSION; break;
209 case FileEXR::PXR24: return (int)Imf::PXR24_COMPRESSION; break;
211 return Imf::NO_COMPRESSION;
214 int FileEXR::str_to_compression(char *string)
216 if(!strcmp(compression_to_str(FileEXR::NONE), string))
217 return FileEXR::NONE;
218 if(!strcmp(compression_to_str(FileEXR::PIZ), string))
220 if(!strcmp(compression_to_str(FileEXR::ZIP), string))
222 if(!strcmp(compression_to_str(FileEXR::ZIPS), string))
223 return FileEXR::ZIPS;
224 if(!strcmp(compression_to_str(FileEXR::RLE), string))
226 if(!strcmp(compression_to_str(FileEXR::PXR24), string))
228 return FileEXR::NONE;
231 int FileEXR::check_sig(Asset *asset, char *test)
233 if(Imf::isImfMagic(test)) return 1;
234 if(test[0] == 'E' && test[1] == 'X' && test[2] == 'R' &&
235 test[3] == 'L' && test[4] == 'I' && test[5] == 'S' && test[6] == 'T')
243 void FileEXR::get_parameters(BC_WindowBase *parent_window,
245 BC_WindowBase* &format_window,
251 EXRConfigVideo *window = new EXRConfigVideo(parent_window, asset);
252 format_window = window;
253 window->create_objects();
254 window->run_window();
259 int FileEXR::colormodel_supported(int colormodel)
261 return native_cmodel;
264 int FileEXR::get_best_colormodel(Asset *asset, int driver)
266 if(asset->exr_use_alpha)
267 return BC_RGBA_FLOAT;
272 int64_t FileEXR::get_memory_usage()
274 int64_t result = FileList::get_memory_usage();
275 if(temp_y) result += (int64_t)asset->width * asset->height * 3 / 2;
280 int FileEXR::read_frame_header(char *path)
284 // This may have been used by VFS
287 // if(!(stream = fopen(path, "rb")))
289 // perror("FileEXR::read_frame_header");
292 // int size = FileSystem::get_size(path);
293 // char *buffer = new char[size];
294 // fread(buffer, size, 1, stream);
297 // EXRIStream exr_stream(buffer, size);
298 // Imf::InputFile file(exr_stream);
301 Imf::InputFile file(path);
303 Imath::Box2i dw = file.header().dataWindow();
305 asset->width = dw.max.x - dw.min.x + 1;
306 asset->height = dw.max.y - dw.min.y + 1;
308 const Imf::ChannelList &channels = file.header().channels();
310 if(channels.findChannel("A"))
311 native_cmodel = BC_RGBA_FLOAT;
313 native_cmodel = BC_RGB_FLOAT;
315 if(channels.findChannel("Y"))
317 // for (Imf::ChannelList::ConstIterator i = channels.begin(); i != channels.end(); ++i)
319 // printf("%s\n", i.name());
326 int FileEXR::read_frame(VFrame *frame, VFrame *data)
328 EXRIStream exr_stream((char*)data->get_data(), data->get_compressed_size());
329 Imf::InputFile file(exr_stream);
330 Imath::Box2i dw = file.header().dataWindow();
333 Imf::FrameBuffer framebuffer;
334 float **rows = (float**)frame->get_rows();
335 int components = cmodel_components(frame->get_color_model());
339 if(!temp_y) temp_y = new float[asset->width * asset->height];
340 if(!temp_u) temp_u = new float[asset->width * asset->height / 4];
341 if(!temp_v) temp_v = new float[asset->width * asset->height / 4];
342 framebuffer.insert("Y", Imf::Slice(Imf::FLOAT,
343 (char*)(temp_y - dy * asset->width - dx),
345 sizeof(float) * frame->get_w()));
346 framebuffer.insert("BY", Imf::Slice(Imf::FLOAT,
347 (char*)(temp_u - dy * asset->width / 4 - dx / 2),
349 sizeof(float) * frame->get_w() / 2,
352 framebuffer.insert("RY", Imf::Slice(Imf::FLOAT,
353 (char*)(temp_v - dy * asset->width / 4 - dx / 2),
355 sizeof(float) * frame->get_w() / 2,
361 framebuffer.insert("R", Imf::Slice(Imf::FLOAT,
362 (char*)(&rows[-dy][-dx * components]),
363 sizeof(float) * components,
364 sizeof(float) * components * frame->get_w()));
365 framebuffer.insert("G", Imf::Slice(Imf::FLOAT,
366 (char*)(&rows[-dy][-dx * components + 1]),
367 sizeof(float) * components,
368 sizeof(float) * components * frame->get_w()));
369 framebuffer.insert("B", Imf::Slice(Imf::FLOAT,
370 (char*)(&rows[-dy][-dx * components + 2]),
371 sizeof(float) * components,
372 sizeof(float) * components * frame->get_w()));
375 // Alpha always goes directly to the output frame
378 framebuffer.insert("A", Imf::Slice(Imf::FLOAT,
379 (char*)(&rows[-dy][-dx * components + 3]),
380 sizeof(float) * components,
381 sizeof(float) * components * frame->get_w()));
384 file.setFrameBuffer(framebuffer);
385 file.readPixels (dw.min.y, dw.max.y);
389 // Convert to RGB using crazy ILM equations
391 Imf::Chromaticities cr;
392 yw = Imf::RgbaYca::computeYw(cr);
394 for(int i = 0; i < asset->height - 1; i += 2)
396 float *y_row1 = temp_y + i * asset->width;
397 float *y_row2 = temp_y + (i + 1) * asset->width;
398 float *u_row = temp_u + (i * asset->width / 4);
399 float *v_row = temp_v + (i * asset->width / 4);
400 float *out_row1 = rows[i];
401 float *out_row2 = rows[i + 1];
402 for(int j = 0; j < asset->width - 1; j += 2)
412 g = (y - r * yw.x - b * yw.z) / yw.y;
416 if(components == 4) out_row1++;
421 g = (y - r * yw.x - b * yw.z) / yw.y;
425 if(components == 4) out_row1++;
430 g = (y - r * yw.x - b * yw.z) / yw.y;
434 if(components == 4) out_row1++;
439 g = (y - r * yw.x - b * yw.z) / yw.y;
443 if(components == 4) out_row1++;
453 int FileEXR::write_frame(VFrame *frame, VFrame *data, FrameWriterUnit *unit)
455 EXRUnit *exr_unit = (EXRUnit*)unit;
457 VFrame *output_frame;
458 data->set_compressed_size(0);
461 int native_cmodel = asset->exr_use_alpha ? BC_RGBA_FLOAT : BC_RGB_FLOAT;
462 int components = cmodel_components(native_cmodel);
464 if(frame->get_color_model() != native_cmodel)
466 if(!exr_unit->temp_frame) exr_unit->temp_frame = new VFrame(0,
472 BC_CModels::transfer(exr_unit->temp_frame->get_rows(), /* Leave NULL if non existent */
474 exr_unit->temp_frame->get_y(), /* Leave NULL if non existent */
475 exr_unit->temp_frame->get_u(),
476 exr_unit->temp_frame->get_v(),
477 frame->get_y(), /* Leave NULL if non existent */
480 0, /* Dimensions to capture from input frame */
484 0, /* Dimensions to project on output frame */
488 frame->get_color_model(),
490 0, /* When transfering BC_RGBA8888 to non-alpha this is the background color in 0xRRGGBB hex */
491 asset->width, /* For planar use the luma rowspan */
493 output_frame = exr_unit->temp_frame;
496 output_frame = frame;
498 Imf::Header header(output_frame->get_w(), output_frame->get_h());
499 header.compression() = (Imf::Compression)compression_to_exr(
500 asset->exr_compression);
501 header.channels().insert("R", Imf::Channel(Imf::FLOAT));
502 header.channels().insert("G", Imf::Channel(Imf::FLOAT));
503 header.channels().insert("B", Imf::Channel(Imf::FLOAT));
504 if(asset->exr_use_alpha) header.channels().insert("A", Imf::Channel(Imf::FLOAT));
506 EXROStream exr_stream(data);
507 Imf::OutputFile file(exr_stream, header);
508 Imf::FrameBuffer framebuffer;
509 float **rows = (float**)output_frame->get_rows();
510 framebuffer.insert("R",
511 Imf::Slice(Imf::FLOAT,
513 sizeof(float) * components,
514 sizeof(float) * components * output_frame->get_w()));
515 framebuffer.insert("G",
516 Imf::Slice(Imf::FLOAT,
517 (char*)(rows[0] + 1),
518 sizeof(float) * components,
519 sizeof(float) * components * output_frame->get_w()));
520 framebuffer.insert("B",
521 Imf::Slice(Imf::FLOAT,
522 (char*)(rows[0] + 2),
523 sizeof(float) * components,
524 sizeof(float) * components * output_frame->get_w()));
525 if(asset->exr_use_alpha)
526 framebuffer.insert("A",
527 Imf::Slice(Imf::FLOAT,
528 (char*)(rows[0] + 3),
529 sizeof(float) * components,
530 sizeof(float) * components * output_frame->get_w()));
531 file.setFrameBuffer(framebuffer);
532 file.writePixels(asset->height);
536 FrameWriterUnit* FileEXR::new_writer_unit(FrameWriter *writer)
538 return new EXRUnit(this, writer);
552 EXRUnit::EXRUnit(FileEXR *file, FrameWriter *writer)
553 : FrameWriterUnit(writer)
561 if(temp_frame) delete temp_frame;
574 EXRConfigVideo::EXRConfigVideo(BC_WindowBase *parent_window, Asset *asset)
575 : BC_Window(_(PROGRAM_NAME ": Video Compression"),
576 parent_window->get_abs_cursor_x(1),
577 parent_window->get_abs_cursor_y(1),
579 BC_OKButton::calculate_h() + 100)
581 this->parent_window = parent_window;
585 EXRConfigVideo::~EXRConfigVideo()
589 void EXRConfigVideo::create_objects()
591 lock_window("EXRConfigVideo::create_objects");
593 add_subwindow(new EXRUseAlpha(this, x, y));
595 EXRCompression *menu;
596 add_subwindow(new BC_Title(x, y, _("Compression:")));
598 add_subwindow(menu = new EXRCompression(this, x, y, 100));
599 menu->create_objects();
600 add_subwindow(new BC_OKButton(this));
605 int EXRConfigVideo::close_event()
612 EXRUseAlpha::EXRUseAlpha(EXRConfigVideo *gui, int x, int y)
613 : BC_CheckBox(x, y, gui->asset->exr_use_alpha, _("Use alpha"))
618 int EXRUseAlpha::handle_event()
620 gui->asset->exr_use_alpha = get_value();
626 EXRCompression::EXRCompression(EXRConfigVideo *gui, int x, int y, int w)
630 FileEXR::compression_to_str(gui->asset->exr_compression))
634 void EXRCompression::create_objects()
636 add_item(new EXRCompressionItem(gui, FileEXR::NONE));
637 add_item(new EXRCompressionItem(gui, FileEXR::PIZ));
638 add_item(new EXRCompressionItem(gui, FileEXR::ZIP));
639 add_item(new EXRCompressionItem(gui, FileEXR::ZIPS));
640 add_item(new EXRCompressionItem(gui, FileEXR::RLE));
641 add_item(new EXRCompressionItem(gui, FileEXR::PXR24));
644 int EXRCompression::handle_event()
649 EXRCompressionItem::EXRCompressionItem(EXRConfigVideo *gui, int value)
650 : BC_MenuItem(FileEXR::compression_to_str(value))
656 int EXRCompressionItem::handle_event()
658 gui->asset->exr_compression = value;