diff --git a/core/subtitle_format_prs.cpp b/core/subtitle_format_prs.cpp index 0023f171a..96b20599f 100644 --- a/core/subtitle_format_prs.cpp +++ b/core/subtitle_format_prs.cpp @@ -163,6 +163,10 @@ void PRSSubtitleFormat::WriteFile(wxString filename,wxString encoding) { wxImage curImage; if (rects[i].x == 0 && rects[i].y == 0 && rects[i].width == bmp.GetWidth() && rects[i].height == bmp.GetHeight()) curImage = bmp; else curImage = SubImageWithAlpha(bmp,rects[i]); + if (!curImage.Ok()) continue; + + // Optimize image + //OptimizeImage(curImage); // Insert the image useFrameN = framen; @@ -178,7 +182,7 @@ void PRSSubtitleFormat::WriteFile(wxString filename,wxString encoding) { // Save file file.Save((const char*)filename.mb_str(wxConvLocal)); wxString filename2 = filename + _T(".prsa"); - file.SaveText((const char*)filename2.mb_str(wxConvLocal)); + //file.SaveText((const char*)filename2.mb_str(wxConvLocal)); #endif } @@ -315,7 +319,7 @@ void PRSSubtitleFormat::InsertFrame(PRSFile &file,int &framen,std::vector & /////////////////////////////////// // Get rectangles of useful glyphs -void PRSSubtitleFormat::GetSubPictureRectangles(wxImage image,std::vector &rects) { +void PRSSubtitleFormat::GetSubPictureRectangles(wxImage &image,std::vector &rects) { // Boundaries int w = image.GetWidth(); int h = image.GetHeight(); @@ -453,6 +457,160 @@ std::vector PRSSubtitleFormat::GetFrameRanges() { } +///////////////////////////////////////////// +// Optimize the image by tweaking the colors +#define ERROR_OF_ALPHA(a) ((a) == 0 ? 255 : 512/((int)(a))) +#define IN_ERROR_MARGIN(col1,col2,error) ((col1 > col2 ? ((int)(col1-col2)) : ((int)(col2-col1))) <= (error)) + +void PRSSubtitleFormat::OptimizeImage(wxImage &image) { + // Get the raw data + unsigned char *data = (unsigned char*) image.GetData(); + unsigned char *alpha = (unsigned char*) image.GetAlpha(); + int w = image.GetWidth(); + int h = image.GetHeight(); + int len = w*h; + + // Create mask for status and fill with zeroes + char *status = new char[len]; + for (int i=0;i highAlpha) highAlpha = alpha[i]; + } + + // Fill mask of "correct" pixels with 2 on highAlpha pixels + for (int i=0;i= w && status[i-w] == 2) { + // Get colors + d1 = *(cur-w*3-3); + d2 = *(cur-w*3-2); + d3 = *(cur-w*3-1); + + // Compare error + if (IN_ERROR_MARGIN(d1,c1,error) && IN_ERROR_MARGIN(d2,c2,error) && IN_ERROR_MARGIN(d3,c3,error)) { + *(cur-3) = d1; + *(cur-2) = d2; + *(cur-1) = d3; + status[i] = 2; + modified++; + continue; + } + } + + // Bottom pixel + if (i < len-w && status[i+w] == 2) { + // Get colors + d1 = *(cur+w*3-3); + d2 = *(cur+w*3-2); + d3 = *(cur+w*3-1); + + // Compare error + if (IN_ERROR_MARGIN(d1,c1,error) && IN_ERROR_MARGIN(d2,c2,error) && IN_ERROR_MARGIN(d3,c3,error)) { + *(cur-3) = d1; + *(cur-2) = d2; + *(cur-1) = d3; + status[i] = 2; + modified++; + continue; + } + } + } + + // End repetion + totalModified += modified; + if (!modified) doRepeat = false; + } + + // End outer loop + if (!totalModified) outerLoop = false; + + // Copy values 1 to 2 + int changes = 0; + for (int i=0;i=0) && (rect.GetTop()>=0) && (rect.GetRight()<=source.GetWidth()) && (rect.GetBottom()<=source.GetHeight()), image, wxT("invalid subimage size") ); diff --git a/core/subtitle_format_prs.h b/core/subtitle_format_prs.h index 377d95be4..a8d00c7a1 100644 --- a/core/subtitle_format_prs.h +++ b/core/subtitle_format_prs.h @@ -59,10 +59,11 @@ private: int optimizer; void InsertFrame(PRSFile &file,int &framen,std::vector &frames,wxImage &bmp,int x,int y,int maxalpha); - wxImage SubImageWithAlpha(wxImage src,const wxRect &area); + wxImage SubImageWithAlpha(wxImage &src,const wxRect &area); wxImage CalculateAlpha(const unsigned char* frame1, const unsigned char* frame2, int w, int h, int pitch, int *x=NULL, int *y=NULL, int *maxalpha=NULL); - void GetSubPictureRectangles(wxImage image,std::vector &rects); + void GetSubPictureRectangles(wxImage &image,std::vector &rects); std::vector GetFrameRanges(); + void OptimizeImage(wxImage &image); public: bool CanWriteFile(wxString filename);