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"
27 #include "mwindow.inc"
29 #include "mainerror.h"
34 /* Known image types. */
35 #define TGA_TYPE_MAPPED 1
36 #define TGA_TYPE_COLOR 2
37 #define TGA_TYPE_GRAY 3
39 /* Only known compression is RLE */
40 #define TGA_COMP_NONE 0
41 #define TGA_COMP_RLE 1
44 FileTGA::FileTGA(Asset *asset, File *file)
45 : FileList(asset, file, "TGALIST", ".tga", FILE_TGA, FILE_TGA_LIST)
55 int FileTGA::check_sig(Asset *asset)
59 // Test file extension
61 char *ext = strrchr(asset->path, '.');
66 if(!strncasecmp(ext, ".tga", 4)) result = 1;
76 if(!(stream = fopen(asset->path, "rb")))
84 (void)fread(test, 16, 1, stream);
86 if(test[0] == 'T' && test[1] == 'G' && test[2] == 'A' &&
87 test[3] == 'L' && test[4] == 'I' && test[5] == 'S' &&
100 void FileTGA::get_parameters(BC_WindowBase *parent_window,
102 BC_WindowBase* &format_window,
108 TGAConfigVideo *window = new TGAConfigVideo(parent_window, asset);
109 format_window = window;
110 window->create_objects();
111 window->run_window();
118 N_("RGBA compressed")
119 N_("RGB uncompressed")
120 N_("RGBA uncompressed")
123 #define TGA_RGB_RLE "rle "
124 #define TGA_RGBA_RLE "rlea"
125 #define TGA_RGB "raw "
126 #define TGA_RGBA "rawa"
128 #define TGA_RGB_RLE_NAME "RGB compressed"
129 #define TGA_RGBA_RLE_NAME "RGBA compressed"
130 #define TGA_RGB_NAME "RGB uncompressed"
131 #define TGA_RGBA_NAME "RGBA uncompressed"
133 const char* FileTGA::compression_to_str(const char *compression)
135 if(!strcasecmp(compression, TGA_RGB_RLE)) return _(TGA_RGB_RLE_NAME);
136 if(!strcasecmp(compression, TGA_RGBA_RLE)) return _(TGA_RGBA_RLE_NAME);
137 if(!strcasecmp(compression, TGA_RGB)) return _(TGA_RGB_NAME);
138 if(!strcasecmp(compression, TGA_RGBA)) return _(TGA_RGBA_NAME);
142 const char* FileTGA::str_to_compression(const char *string)
144 if(!strcasecmp(compression_to_str(TGA_RGB_RLE), string)) return TGA_RGB_RLE;
145 if(!strcasecmp(compression_to_str(TGA_RGBA_RLE), string)) return TGA_RGBA_RLE;
146 if(!strcasecmp(compression_to_str(TGA_RGB), string)) return TGA_RGB;
147 if(!strcasecmp(compression_to_str(TGA_RGBA), string)) return TGA_RGBA;
151 int FileTGA::can_copy_from(Asset *asset, int64_t position)
153 if(asset->format == FILE_TGA_LIST ||
154 asset->format == FILE_TGA)
161 int FileTGA::colormodel_supported(int colormodel)
166 int FileTGA::get_best_colormodel(Asset *asset, int driver)
168 if(!strcasecmp(asset->vcodec, TGA_RGB_RLE) ||
169 !strcasecmp(asset->vcodec, TGA_RGB)) return BC_RGB888;
170 if(!strcasecmp(asset->vcodec, TGA_RGBA_RLE) ||
171 !strcasecmp(asset->vcodec, TGA_RGBA)) return BC_RGBA8888;
175 int FileTGA::read_frame(VFrame *frame, VFrame *data)
177 read_tga(asset, frame, data, temp);
181 int FileTGA::write_frame(VFrame *frame, VFrame *data, FrameWriterUnit *unit)
183 TGAUnit *tga_unit = (TGAUnit*)unit;
185 write_tga(asset, frame, data, tga_unit->temp);
189 FrameWriterUnit* FileTGA::new_writer_unit(FrameWriter *writer)
191 return new TGAUnit(this, writer);
194 int64_t FileTGA::get_memory_usage()
196 int64_t result = FileList::get_memory_usage();
197 if(temp) result += temp->get_data_size();
208 #define FOOTERSIZE 26
209 #define HEADERSIZE 18
210 int FileTGA::read_frame_header(char *path)
214 //printf("FileTGA::read_frame_header 1\n");
217 if(!(stream = fopen(path, "rb")))
219 eprintf(_("Error while opening \"%s\" for reading. \n%m\n"), asset->path);
223 unsigned char header[HEADERSIZE];
224 (void)fread(header, HEADERSIZE, 1, stream);
227 asset->width = header[12] | (header[13] << 8);
228 asset->height = header[14] | (header[15] << 8);
229 int bpp = header[16];
230 int rle = header[2] & 0x8;
235 strcpy(asset->vcodec, TGA_RGBA_RLE);
237 strcpy(asset->vcodec, TGA_RGBA);
241 strcpy(asset->vcodec, TGA_RGB_RLE);
243 strcpy(asset->vcodec, TGA_RGB);
246 //printf("FileTGA::read_frame_header 2 %d %d\n", asset->width, asset->height);
251 void FileTGA::read_tga(Asset *asset, VFrame *frame, VFrame *data, VFrame* &temp)
254 int64_t file_offset = 0;
256 // unsigned char *footer = data->get_data() +
257 // data->get_compressed_size() - FOOTERSIZE;
258 unsigned char *header = data->get_data();
259 file_offset += HEADERSIZE;
262 int image_compression = 0;
266 image_type = TGA_TYPE_MAPPED;
267 image_compression = TGA_COMP_NONE;
270 image_type = TGA_TYPE_COLOR;
271 image_compression = TGA_COMP_NONE;
274 image_type = TGA_TYPE_GRAY;
275 image_compression = TGA_COMP_NONE;
278 image_type = TGA_TYPE_MAPPED;
279 image_compression = TGA_COMP_RLE;
282 image_type = TGA_TYPE_COLOR;
283 image_compression = TGA_COMP_RLE;
286 image_type = TGA_TYPE_GRAY;
287 image_compression = TGA_COMP_RLE;
291 int idlength = header[0];
292 int colormaptype = header[1];
293 //int colormapindex = header[3] + header[4] * 256;
294 int colormaplength = header[5] + header[6] * 256;
295 int colormapsize = header[7];
296 //int xorigin = header[8] + header[9] * 256;
297 //int yorigin = header[10] + header[11] * 256;
298 int width = header[12] + header[13] * 256;
299 int height = header[14] + header[15] * 256;
300 int bpp = header[16];
301 int bytes = (bpp + 7) / 8;
302 int alphabits = header[17] & 0x0f;
303 int fliphoriz = (header[17] & 0x10) ? 1 : 0;
304 int flipvert = (header[17] & 0x20) ? 0 : 1;
305 int data_size = data->get_compressed_size();
307 if(idlength) file_offset += idlength;
310 unsigned char *tga_cmap;
311 unsigned char colormap[4 * 256];
313 if(colormaptype == 1)
315 int cmap_bytes = (colormapsize + 7) / 8;
316 tga_cmap = data->get_data() + file_offset;
317 file_offset += colormaplength * cmap_bytes;
322 bgr2rgb(colormap, tga_cmap, colormaplength, cmap_bytes, 1);
325 bgr2rgb(colormap, tga_cmap, colormaplength, cmap_bytes, 0);
328 upsample(colormap, tga_cmap, colormaplength, cmap_bytes);
333 int source_cmodel = BC_RGB888;
337 source_cmodel = BC_RGBA8888;
340 source_cmodel = BC_RGB888;
345 VFrame *output_frame;
346 if(frame->get_color_model() == source_cmodel)
348 output_frame = frame;
352 if(temp && temp->get_color_model() != source_cmodel)
360 temp = new VFrame(width, height, source_cmodel, 0);
367 for(int i = height - 1; i >= 0; i--)
369 read_line(output_frame->get_rows()[i],
384 for(int i = 0; i < height; i++)
386 read_line(output_frame->get_rows()[i],
400 if(output_frame != frame)
402 BC_CModels::transfer(frame->get_rows(),
403 output_frame->get_rows(),
407 output_frame->get_y(),
408 output_frame->get_u(),
409 output_frame->get_v(),
418 output_frame->get_color_model(),
419 frame->get_color_model(),
426 void FileTGA::write_tga(Asset *asset, VFrame *frame, VFrame *data, VFrame* &temp)
428 unsigned char header[18];
429 //unsigned char footer[26];
430 int64_t file_offset = 0;
433 int dest_cmodel = BC_RGB888;
435 //printf("FileTGA::write_tga 1\n");
439 if(!strcasecmp(asset->vcodec, TGA_RGBA_RLE))
444 header[16] = 32; /* bpp */
445 header[17] = 0x28; /* alpha + orientation */
446 dest_cmodel = BC_RGBA8888;
449 if(!strcasecmp(asset->vcodec, TGA_RGBA))
454 header[16] = 32; /* bpp */
455 header[17] = 0x28; /* alpha + orientation */
456 dest_cmodel = BC_RGBA8888;
459 if(!strcasecmp(asset->vcodec, TGA_RGB_RLE))
464 header[16] = 24; /* bpp */
465 header[17] = 0x20; /* alpha + orientation */
466 dest_cmodel = BC_RGB888;
473 header[16] = 24; /* bpp */
474 header[17] = 0x20; /* alpha + orientation */
475 dest_cmodel = BC_RGB888;
477 header[3] = header[4] = header[5] = header[6] = header[7] = 0;
478 //printf("FileTGA::write_tga 1\n");
481 if(frame->get_color_model() == dest_cmodel)
487 if(temp && temp->get_color_model() != dest_cmodel)
495 temp = new VFrame(0, -1, frame->get_w(), frame->get_h(), dest_cmodel, -1);
499 BC_CModels::transfer(input_frame->get_rows(),
501 input_frame->get_y(),
502 input_frame->get_u(),
503 input_frame->get_v(),
515 frame->get_color_model(),
516 input_frame->get_color_model(),
521 //printf("FileTGA::write_tga 1\n");
525 header[8] = header[9] = 0;
526 header[10] = header[11] = 0;
528 header[12] = input_frame->get_w() % 256;
529 header[13] = input_frame->get_w() / 256;
531 header[14] = input_frame->get_h() % 256;
532 header[15] = input_frame->get_h() / 256;
533 //printf("FileTGA::write_tga 1\n");
535 write_data(header, data, file_offset, sizeof(header));
536 //printf("FileTGA::write_tga 1\n");
538 unsigned char *output = new unsigned char[out_bpp * input_frame->get_w()];
539 //printf("FileTGA::write_tga 1\n");
540 for(int i = 0; i < input_frame->get_h(); i++)
542 //printf("FileTGA::write_tga 2\n");
543 bgr2rgb(output, input_frame->get_rows()[i], input_frame->get_w(), out_bpp, (out_bpp == 4));
544 //printf("FileTGA::write_tga 3\n");
548 //printf("FileTGA::write_tga 4\n");
550 input_frame->get_w(),
554 //printf("FileTGA::write_tga 5\n");
558 //printf("FileTGA::write_tga 6\n");
562 input_frame->get_w() * out_bpp);
563 //printf("FileTGA::write_tga 7\n");
566 //printf("FileTGA::write_tga 8\n");
568 //printf("FileTGA::write_tga 9\n");
571 void FileTGA::write_data(unsigned char *buffer,
573 int64_t &file_offset,
576 //printf("FileTGA::write_data 1 %d\n", len);
577 if(data->get_compressed_allocated() <= data->get_compressed_size() + len)
579 data->allocate_compressed_data((data->get_compressed_size() + len) * 2);
581 //printf("FileTGA::write_data 1 %d\n", len);
583 bcopy(buffer, data->get_data() + file_offset, len);
584 //printf("FileTGA::write_data 1 %d\n", len);
586 //printf("FileTGA::write_data 1 %d\n", len);
587 data->set_compressed_size(file_offset);
588 //printf("FileTGA::write_data 2 %d\n", len);
591 void FileTGA::read_line(unsigned char *row,
593 int64_t &file_offset,
596 int image_compression,
603 if(file_offset >= data_size) return;
604 if(image_compression == TGA_COMP_RLE)
614 if(file_offset + bytes * width <= data_size)
615 bcopy(data + file_offset, row, bytes * width);
616 file_offset += bytes * width;
621 flip_line(row, bytes, width);
624 if(image_type == TGA_TYPE_COLOR)
628 upsample(row, row, width, bytes);
632 bgr2rgb(row, row, width, bytes, alphabits);
641 void FileTGA::flip_line(unsigned char *row, int bytes, int width)
647 alt = row + (bytes * (width - 1));
649 for (x = 0; x * 2 <= width; x++)
651 for(s = 0; s < bytes; ++s)
663 void FileTGA::rle_read(unsigned char *row,
665 int64_t &file_offset,
671 unsigned char sample[4];
674 for(int x = 0; x < width; x++)
676 if(repeat == 0 && direct == 0)
678 head = data[file_offset++];
687 bcopy(data + file_offset, sample, bytes);
688 file_offset += bytes;
698 for(int k = 0; k < bytes; k++)
707 bcopy(data + file_offset, row, bytes);
708 file_offset += bytes;
717 void FileTGA::rle_write(unsigned char *buffer,
721 int64_t &file_offset)
725 unsigned char *from = buffer;
726 unsigned char output;
729 for(x = 1; x < width; ++x)
731 /* next pixel is different */
732 if(memcmp(buffer, buffer + bytes, bytes))
736 output = 128 + repeat;
737 write_data(&output, frame, file_offset, 1);
738 write_data(from, frame, file_offset, bytes);
739 from = buffer + bytes;
749 /* next pixel is the same */
754 write_data(&output, frame, file_offset, 1);
755 write_data(from, frame, file_offset, bytes * direct);
769 write_data(&output, frame, file_offset, 1);
770 write_data(from, frame, file_offset, bytes);
771 from = buffer + bytes;
779 write_data(&output, frame, file_offset, 1);
780 write_data(from, frame, file_offset, direct * bytes);
781 from = buffer + bytes;
791 output = 128 + repeat;
792 write_data(&output, frame, file_offset, 1);
793 write_data(from, frame, file_offset, bytes);
798 write_data(&output, frame, file_offset, 1);
799 write_data(from, frame, file_offset, bytes * (direct + 1));
804 void FileTGA::bgr2rgb(unsigned char *dest,
811 unsigned char r, g, b;
815 for(x = 0; x < width; x++)
830 for(x = 0; x < width; x++)
844 void FileTGA::upsample(unsigned char *dest,
851 dest += (width - 1) * 3;
852 src += (width - 1) * bytes;
853 for(x = width - 1; x >= 0; x--)
855 dest[0] = ((src[1] << 1) & 0xf8);
856 dest[0] += (dest[0] >> 5);
858 dest[1] = ((src[0] & 0xe0) >> 2) + ((src[1] & 0x03) << 6);
859 dest[1] += (dest[1] >> 5);
861 dest[2] = ((src[0] << 3) & 0xf8);
862 dest[2] += (dest[2] >> 5);
877 TGAUnit::TGAUnit(FileTGA *file, FrameWriter *writer)
878 : FrameWriterUnit(writer)
886 if(temp) delete temp;
901 TGAConfigVideo::TGAConfigVideo(BC_WindowBase *gui, Asset *asset)
902 : BC_Window(_(PROGRAM_NAME ": Video Compression"),
903 gui->get_abs_cursor_x(1),
904 gui->get_abs_cursor_y(1),
911 compression_items.append(new BC_ListBoxItem(FileTGA::compression_to_str(TGA_RGB_RLE)));
912 compression_items.append(new BC_ListBoxItem(FileTGA::compression_to_str(TGA_RGBA_RLE)));
913 compression_items.append(new BC_ListBoxItem(FileTGA::compression_to_str(TGA_RGB)));
914 compression_items.append(new BC_ListBoxItem(FileTGA::compression_to_str(TGA_RGBA)));
917 TGAConfigVideo::~TGAConfigVideo()
919 compression_items.remove_all_objects();
922 void TGAConfigVideo::create_objects()
924 lock_window("TGAConfigVideo::create_objects()");
927 add_subwindow(new BC_Title(x, y, _("Compression:")));
928 TGACompression *textbox = new TGACompression(this,
933 textbox->create_objects();
934 add_subwindow(new BC_OKButton(this));
939 int TGAConfigVideo::close_event()
946 TGACompression::TGACompression(TGAConfigVideo *gui,
950 ArrayList<BC_ListBoxItem*> *compression_items)
951 : BC_PopupTextBox(gui,
953 FileTGA::compression_to_str(gui->asset->vcodec),
961 int TGACompression::handle_event()
963 strcpy(asset->vcodec, FileTGA::str_to_compression(get_text()));