From 6a811254e30004f938606f2d6932ae16c421ec38 Mon Sep 17 00:00:00 2001 From: Rodrigo Braz Monteiro Date: Mon, 21 Jan 2008 20:35:31 +0000 Subject: [PATCH] Complete (but untested) .sup generation. Originally committed to SVN as r1805. --- aegisub/subtitle_format.cpp | 8 +- aegisub/subtitle_format.h | 2 +- aegisub/subtitle_format_dvd.cpp | 119 +++++++++++++++++++++++---- aegisub/subtitle_format_dvd.h | 1 + aegisub/subtitle_format_encore.cpp | 2 +- aegisub/subtitle_format_microdvd.cpp | 2 +- aegisub/subtitle_format_srt.cpp | 2 +- aegisub/subtitle_format_ttxt.cpp | 2 +- 8 files changed, 116 insertions(+), 22 deletions(-) diff --git a/aegisub/subtitle_format.cpp b/aegisub/subtitle_format.cpp index 32b8a1ea5..3581a0a3f 100644 --- a/aegisub/subtitle_format.cpp +++ b/aegisub/subtitle_format.cpp @@ -365,7 +365,7 @@ void SubtitleFormat::ConvertTags(int format,wxString lineEnd) { //////////////////////////////////////////// // Merge identical and/or overlapping lines -void SubtitleFormat::Merge(bool identical,bool overlaps,bool stripComments) { +void SubtitleFormat::Merge(bool identical,bool overlaps,bool stripComments,bool stripNonDialogue) { using std::list; list::iterator next; list::iterator prev = Line->end(); @@ -407,8 +407,10 @@ void SubtitleFormat::Merge(bool identical,bool overlaps,bool stripComments) { // Other line, delete it else { - delete *cur; - Line->erase(cur); + if (stripNonDialogue) { + delete *cur; + Line->erase(cur); + } } } } diff --git a/aegisub/subtitle_format.h b/aegisub/subtitle_format.h index 381a4f440..ac7d3ade5 100644 --- a/aegisub/subtitle_format.h +++ b/aegisub/subtitle_format.h @@ -71,7 +71,7 @@ protected: void ClearCopy(); void SortLines(); void ConvertTags(int format,wxString lineEnd); - void Merge(bool identical,bool overlaps,bool stripComments); + void Merge(bool identical,bool overlaps,bool stripComments,bool stripNonDialogue); void Clear(); void LoadDefault(bool defline=true); diff --git a/aegisub/subtitle_format_dvd.cpp b/aegisub/subtitle_format_dvd.cpp index f11635e00..d1ce9875e 100644 --- a/aegisub/subtitle_format_dvd.cpp +++ b/aegisub/subtitle_format_dvd.cpp @@ -47,6 +47,7 @@ #ifdef _OPENMP #include #endif +#include /////////////// @@ -82,11 +83,6 @@ void DVDSubtitleFormat::GetSubPictureList(std::vector &pics) { AegiVideoFrame srcFrame = video->GetFrame(0); delete video; - // Prepare subtitles - CreateCopy(); - SortLines(); - //Merge(true,true,true); - // Count and index lines using std::list; int count = 0; @@ -192,7 +188,7 @@ void DVDSubtitleFormat::GetSubPictureList(std::vector &pics) { } } - // Save image + // Save image data if (startX > endX) endX = startX; if (startY > endY) endY = startY; int sw = endX-startX+1; @@ -201,6 +197,8 @@ void DVDSubtitleFormat::GetSubPictureList(std::vector &pics) { pics[i].y = startY; pics[i].w = sw; pics[i].h = sh; + pics[i].start = current->Start.GetMS(); + pics[i].end = current->End.GetMS(); // RLE to memory for (int j=0;j<2;j++) { @@ -208,7 +206,9 @@ void DVDSubtitleFormat::GetSubPictureList(std::vector &pics) { int col; int temp; int len = 0; + //wxImage subPic = img.GetSubImage(wxRect(startX,startY,sw,sh)); dataRead = data + ((startY+j)*w+startX)*3; + //dataRead = subPic.GetData(); std::vector groups; groups.reserve(1024); @@ -226,7 +226,7 @@ void DVDSubtitleFormat::GetSubPictureList(std::vector &pics) { if (col == curCol) { len++; if (len == 255) { - if (len) groups.push_back(RLEGroup(curCol,len,false)); + groups.push_back(RLEGroup(curCol,len,false)); len = 0; } } @@ -257,21 +257,35 @@ void DVDSubtitleFormat::GetSubPictureList(std::vector &pics) { std::vector &data = pics[i].data[j]; unsigned char last = 0; for (size_t m=0;m> 2; - nibble[0] = groups[m].col | ((groups[m].len & 0x3) << 2); - nibble[1] = (groups[m].len & 0x3C) >> 2; - nibble[2] = (groups[m].len & 0xC0) >> 6; - for (int n=0;n> 2; + nibble[2] = (len & 0xC0) >> 6; + for (int n=nibbles;--n>=0;) { + wxASSERT(nibble[n] >= 0 && nibble[n] < 16); + wxASSERT(n >= 0 && n < 4); if (!off) { - last = nibble[nibbles-n-1] << 4; + last = nibble[n] << 4; data.push_back(last); } - else data.back() = data.back() | last; + else data.back() = nibble[n] | last; off = !off; } } + last = 0; + off = false; data.resize(data.size()); } } @@ -285,7 +299,84 @@ void DVDSubtitleFormat::GetSubPictureList(std::vector &pics) { /////////////////////// // Actually write them void DVDSubtitleFormat::WriteFile(wxString filename,wxString encoding) { + // Prepare subtitles + CreateCopy(); + SortLines(); + Merge(true,true,true,false); + // Get subpictures std::vector pics; GetSubPictureList(pics); + + // Open file for writing + wxFile fp(filename,wxFile::write); + if (!fp.IsOpened()) throw _T("Could not open file for writing."); + + // Write each subpicture + size_t pos = 0; + for (size_t i=0;i> 8; + unsigned char comm2_b2 = comm2add & 0xFF; + unsigned char pix0_b1 = (line0pos & 0xFF00) >> 8; + unsigned char pix0_b2 = line0pos & 0xFF; + unsigned char pix1_b1 = (line1pos & 0xFF00) >> 8; + unsigned char pix1_b2 = line1pos & 0xFF; + int delay = (pics[i].end - pics[i].start)/10; + unsigned char delay_b1 = (delay & 0xFF00) >> 8; + unsigned char delay_b2 = delay & 0xFF; + int sx = pics[i].x; + int sy = pics[i].y; + int ex = pics[i].w + sx; + int ey = pics[i].h + sy; + unsigned char dispx_b1 = (sx & 0xFF0) >> 4; + unsigned char dispx_b2 = ((sx & 0x0F) << 4) | ((ex & 0xF00) >> 8); + unsigned char dispx_b3 = (sx & 0xFF); + unsigned char dispy_b1 = (sy & 0xFF0) >> 4; + unsigned char dispy_b2 = ((sy & 0x0F) << 4) | ((ey & 0xF00) >> 8); + unsigned char dispy_b3 = (sy & 0xFF); + + // Write control group + unsigned char control[] = { + 0x00, 0x00, // Delay + comm2_b1, comm2_b2, // Next command + 0x01, // Start display + 0x03, 0x01, 0x23, // Set colours + 0x04, 0x0F, 0xFF, // Alpha blend + 0x05, dispx_b1, dispx_b2, dispx_b3, dispy_b1, dispy_b2, dispy_b3, // Display area + 0x06, pix0_b1, pix0_b2, pix1_b1, pix1_b2, // Pixel pointers + 0xFF, // End block 1 + delay_b1, delay_b2, // Delay + comm2_b1, comm2_b2, // This command + 0x02, // Stop display + 0xFF // End + }; + pos += fp.Write(control,controlLen); + } } diff --git a/aegisub/subtitle_format_dvd.h b/aegisub/subtitle_format_dvd.h index ab0c6e6e7..58e01e772 100644 --- a/aegisub/subtitle_format_dvd.h +++ b/aegisub/subtitle_format_dvd.h @@ -49,6 +49,7 @@ struct SubPicture { std::vector data[2]; int x,y; int w,h; + int start,end; }; struct RLEGroup { diff --git a/aegisub/subtitle_format_encore.cpp b/aegisub/subtitle_format_encore.cpp index 77fd145b3..a0a270261 100644 --- a/aegisub/subtitle_format_encore.cpp +++ b/aegisub/subtitle_format_encore.cpp @@ -77,7 +77,7 @@ void EncoreSubtitleFormat::WriteFile(wxString _filename,wxString encoding) { // Convert to encore CreateCopy(); SortLines(); - Merge(true,true,true); + Merge(true,true,true,false); ConvertTags(1,_T("\r\n")); // Write lines diff --git a/aegisub/subtitle_format_microdvd.cpp b/aegisub/subtitle_format_microdvd.cpp index de65d9b8b..a138692fc 100644 --- a/aegisub/subtitle_format_microdvd.cpp +++ b/aegisub/subtitle_format_microdvd.cpp @@ -178,7 +178,7 @@ void MicroDVDSubtitleFormat::WriteFile(wxString filename,wxString encoding) { // Convert file CreateCopy(); SortLines(); - Merge(true,true,true); + Merge(true,true,true,false); ConvertTags(1,_T("|")); // Open file diff --git a/aegisub/subtitle_format_srt.cpp b/aegisub/subtitle_format_srt.cpp index 7e4301b5d..0e1254d59 100644 --- a/aegisub/subtitle_format_srt.cpp +++ b/aegisub/subtitle_format_srt.cpp @@ -182,7 +182,7 @@ void SRTSubtitleFormat::WriteFile(wxString _filename,wxString encoding) { // Convert to SRT CreateCopy(); SortLines(); - Merge(true,true,true); + Merge(true,true,true,false); ConvertTags(2,_T("\r\n")); // Write lines diff --git a/aegisub/subtitle_format_ttxt.cpp b/aegisub/subtitle_format_ttxt.cpp index b21cfdcab..b073e05a7 100644 --- a/aegisub/subtitle_format_ttxt.cpp +++ b/aegisub/subtitle_format_ttxt.cpp @@ -322,7 +322,7 @@ void TTXTSubtitleFormat::WriteLine(wxXmlNode *root, AssDialogue *line) { void TTXTSubtitleFormat::ConvertToTTXT () { // Convert SortLines(); - Merge(true,true,true); + Merge(true,true,true,false); ConvertTags(1,_T("\r\n")); // Find last line