From 84ffb67b045ba2d7bc0a6646837f0a3b03b61133 Mon Sep 17 00:00:00 2001 From: Rodrigo Braz Monteiro Date: Thu, 30 Mar 2006 03:32:45 +0000 Subject: [PATCH] Originally committed to SVN as r257. --- core/subtitle_format_prs.cpp | 149 +++++++++++++++++++---------------- core/subtitle_format_prs.h | 1 + 2 files changed, 81 insertions(+), 69 deletions(-) diff --git a/core/subtitle_format_prs.cpp b/core/subtitle_format_prs.cpp index 0b6d7f2c6..6a14b897c 100644 --- a/core/subtitle_format_prs.cpp +++ b/core/subtitle_format_prs.cpp @@ -47,6 +47,7 @@ #include "main.h" #include "frame_main.h" #include "vfr.h" +#include "utils.h" #include "../prs/prs_file.h" #include "../prs/prs_image.h" #include "../prs/prs_display.h" @@ -143,68 +144,21 @@ void PRSSubtitleFormat::WriteFile(wxString filename,wxString encoding) { } + + + +/////////////////////////////////////////////////////////////////////////////////////////////////// +// TODO!! MOVE THE TWO FUNCTIONS BELOW INTO A CLASS OF THEIR OWN, THEY MIGHT FIND USE ELSEWHERE. // +// Obvious choice would be the subtitles_rasterizer.h derivation for vsfilter, when that exists. // +/////////////////////////////////////////////////////////////////////////////////////////////////// + + ////////////////////////////////////////////// // Generates a 32-bit wxImage from two frames // ------------------------------------------ // Frame 1 should have the image on a BLACK background // Frame 2 should have the same image on a WHITE background wxImage PRSSubtitleFormat::CalculateAlpha(const unsigned char* frame1, const unsigned char* frame2, int w, int h, int pitch) { - //// Allocate image data - //unsigned char *data = (unsigned char*) malloc(sizeof(unsigned char)*w*h*4); - - //// Pointers - //const unsigned char *src1 = frame1; - //const unsigned char *src2 = frame2; - //unsigned char *dst = data + ((h-1)*w); - - //// Process - //int r1,g1,b1,r2,g2,b2; - //int r,g,b,a; - //for (int y=0;y=0;) { for (int x=0;x maxx) maxx = x; + if (y < miny) miny = y; + else if (y > maxy) maxy = y; + + // Calculate colour components + int mod; + if (a > 8) mod = 0; + else mod = 256 >> a; + r = MAX(0,r1-mod)*255 / a; + g = MAX(0,g1-mod)*255 / a; + b = MAX(0,b1-mod)*255 / a; } // Write to destination @@ -250,9 +218,6 @@ wxImage PRSSubtitleFormat::CalculateAlpha(const unsigned char* frame1, const uns *(dst++) = g; *(dst++) = b; *(dsta++) = a; - - if (a > maxa) maxa = a; - if (a < mina) mina = a; } // Roll back dst @@ -260,10 +225,56 @@ wxImage PRSSubtitleFormat::CalculateAlpha(const unsigned char* frame1, const uns dsta -= w/2; } - wxLogMessage(wxString::Format(_T("Min: %i, Max: %i"),mina,maxa)); - - // Create the actual image and return it + // Create the actual image wxImage img(w/4,h,data,false); img.SetAlpha(alpha,false); - return img; + + // Return subimage + minx /= 4; + maxx /= 4; + wxImage subimg = SubImageWithAlpha(img,wxRect(minx,miny,maxx-minx+1,maxy-miny+1)); + return subimg; +} + + +//////////////////////////////////////////////// +// Creates a sub image preserving alpha channel +// Modified from wx's source +wxImage PRSSubtitleFormat::SubImageWithAlpha (wxImage source,const wxRect &rect) { + wxImage image; + wxCHECK_MSG(source.Ok(), image, wxT("invalid image") ); + wxCHECK_MSG((rect.GetLeft()>=0) && (rect.GetTop()>=0) && (rect.GetRight()<=source.GetWidth()) && (rect.GetBottom()<=source.GetHeight()), image, wxT("invalid subimage size") ); + + int subwidth=rect.GetWidth(); + const int subheight=rect.GetHeight(); + + image.Create(subwidth, subheight, false); + + image.SetAlpha(); + unsigned char *subdata = image.GetData(); + unsigned char *data = source.GetData(); + unsigned char *subalpha = image.GetAlpha(); + unsigned char *alpha = source.GetAlpha(); + + wxCHECK_MSG(subdata, image, wxT("unable to create image")); + + const int subleft=3*rect.GetLeft(); + const int width=3*source.GetWidth(); + const int afullwidth=source.GetWidth(); + int awidth = subwidth; + subwidth*=3; + + data+=rect.GetTop()*width+subleft; + alpha+=rect.GetTop()*afullwidth+rect.GetLeft(); + + for (long j = 0; j < subheight; ++j) { + memcpy(subdata, data, subwidth); + memcpy(subalpha, alpha, awidth); + subdata+=subwidth; + subalpha+=awidth; + data+=width; + alpha+=afullwidth; + } + + return image; } diff --git a/core/subtitle_format_prs.h b/core/subtitle_format_prs.h index 33bc7835e..29c51353d 100644 --- a/core/subtitle_format_prs.h +++ b/core/subtitle_format_prs.h @@ -46,6 +46,7 @@ // PRS writer class PRSSubtitleFormat : public SubtitleFormat { private: + wxImage SubImageWithAlpha(wxImage src,const wxRect &area); wxImage CalculateAlpha(const unsigned char* frame1, const unsigned char* frame2, int w, int h, int pitch); public: