From 02cfa685fcd50d4ac0f4ce0fd3055f01556eb741 Mon Sep 17 00:00:00 2001
From: Rodrigo Braz Monteiro <zeratul@cellosoft.com>
Date: Sat, 1 Apr 2006 12:43:22 +0000
Subject: [PATCH] Avisynth filter won't crash anymore, but shows a bunch of
 glitches instead, now

Originally committed to SVN as r277.
---
 avisynth_prs/draw_prs.cpp |  5 +++--
 prs/prs_display.cpp       | 36 ++++++++++++++++++++++++++----------
 prs/prs_image.cpp         |  7 ++++---
 prs/prs_video_frame.cpp   |  6 +++---
 4 files changed, 36 insertions(+), 18 deletions(-)

diff --git a/avisynth_prs/draw_prs.cpp b/avisynth_prs/draw_prs.cpp
index 3f8580945..5dd24e056 100644
--- a/avisynth_prs/draw_prs.cpp
+++ b/avisynth_prs/draw_prs.cpp
@@ -71,13 +71,14 @@ DrawPRS::~DrawPRS() {
 // Get frame
 PVideoFrame __stdcall DrawPRS::GetFrame(int n, IScriptEnvironment* env) {
 	// Get frame
-	PVideoFrame avsFrame = child->GetFrame(n,env);
+	//PVideoFrame avsFrame = child->GetFrame(n,env);
+	PVideoFrame avsFrame = env->NewVideoFrame(vi);
 
 	try {
 		// Create the PRSFrame structure
 		PRSVideoFrame frame;
 		frame.data[0] = (char*) avsFrame->GetWritePtr();
-		frame.w = avsFrame->GetRowSize();
+		frame.w = avsFrame->GetRowSize()/4;
 		frame.h = avsFrame->GetHeight();
 		frame.pitch = avsFrame->GetPitch();
 		frame.colorSpace = ColorSpace_RGB32;
diff --git a/prs/prs_display.cpp b/prs/prs_display.cpp
index 580c73600..9c3383f4d 100644
--- a/prs/prs_display.cpp
+++ b/prs/prs_display.cpp
@@ -65,7 +65,7 @@ PRSDisplay::~PRSDisplay() {
 // Write data
 void PRSDisplay::WriteData(std::vector<char> &vec) {
 	// Set length
-	unsigned __int32 size = 4 + 4 + 4 + 4 + 2 + 2 + 1 + 1;
+	unsigned __int32 size = 4 + 4 + 4 + 4 + 4 + 4 + 2 + 2 + 1 + 1;
 	vec.resize(size+8);
 	size_t pos = 0;
 
@@ -78,6 +78,14 @@ void PRSDisplay::WriteData(std::vector<char> &vec) {
 	memcpy(&vec[pos],&size,4);
 	pos += 4;
 
+	// Write start frame
+	memcpy(&vec[pos],&startFrame,4);
+	pos += 4;
+
+	// Write end frame
+	memcpy(&vec[pos],&endFrame,4);
+	pos += 4;
+
 	// Write start time
 	memcpy(&vec[pos],&start,4);
 	pos += 4;
@@ -116,39 +124,47 @@ void PRSDisplay::WriteData(std::vector<char> &vec) {
 // Read data
 void PRSDisplay::ReadData(std::vector<char> &vec) {
 	// Set length
-	unsigned __int32 size = 4 + 4 + 4 + 4 + 2 + 2 + 1 + 1;
+	unsigned __int32 size = 4 + 4 + 4 + 4 + 4 + 4 + 2 + 2 + 1 + 1;
 	if (size != vec.size()) return;
 	size_t pos = 0;
 
-	// Write start time
+	// Read start frame
+	memcpy(&startFrame,&vec[pos],4);
+	pos += 4;
+
+	// Read end frame
+	memcpy(&endFrame,&vec[pos],4);
+	pos += 4;
+
+	// Read start time
 	memcpy(&start,&vec[pos],4);
 	pos += 4;
 
-	// Write end time
+	// Read end time
 	memcpy(&end,&vec[pos],4);
 	pos += 4;
 
-	// Write image identifier
+	// Read image identifier
 	memcpy(&id,&vec[pos],4);
 	pos += 4;
 
-	// Write layer
+	// Read layer
 	memcpy(&layer,&vec[pos],4);
 	pos += 4;
 
-	// Write x
+	// Read x
 	memcpy(&x,&vec[pos],2);
 	pos += 2;
 
-	// Write y
+	// Read y
 	memcpy(&y,&vec[pos],2);
 	pos += 2;
 
-	// Write alpha multiplier
+	// Read alpha multiplier
 	memcpy(&alpha,&vec[pos],1);
 	pos += 1;
 
-	// Write blend mode
+	// Read blend mode
 	memcpy(&blend,&vec[pos],1);
 	pos += 1;
 }
diff --git a/prs/prs_image.cpp b/prs/prs_image.cpp
index 3ab6566d5..741a57a9c 100644
--- a/prs/prs_image.cpp
+++ b/prs/prs_image.cpp
@@ -108,15 +108,16 @@ void PRSImage::ReadData(std::vector<char> &vec) {
 	memcpy(&id,&vec[pos],4);
 	pos += 4;
 
-	// Write image format
+	// Read image format
 	memcpy(&imageType,&vec[pos],4);
 	pos += 4;
 
-	// Write data length
+	// Read data length
 	memcpy(&dataLen,&vec[pos],4);
 	pos += 4;
 
-	// Write data
+	// Read data
+	data = new char[dataLen];
 	memcpy(data,&vec[pos],dataLen);
 	pos += dataLen;
 }
diff --git a/prs/prs_video_frame.cpp b/prs/prs_video_frame.cpp
index bd93a2005..172a609e5 100644
--- a/prs/prs_video_frame.cpp
+++ b/prs/prs_video_frame.cpp
@@ -85,8 +85,8 @@ void PRSVideoFrame::Overlay(PRSVideoFrame *dstFrame,int x,int y,unsigned char al
 	// Get boundaries
 	int srcBpp = 4;
 	int dstBpp = 4;
-	int srcRowLen = w * srcBpp;
-	int dstRowLen = dstFrame->w * dstBpp;
+	int srcRowLen = pitch;
+	int dstRowLen = dstFrame->pitch;
 	int dstStarty = MAX(0,y);
 	int dstEndy = MIN(y+h,dstFrame->h);
 	int height = dstEndy - dstStarty;
@@ -117,7 +117,7 @@ void PRSVideoFrame::Overlay(PRSVideoFrame *dstFrame,int x,int y,unsigned char al
 			dc3 = *(dst+3);
 
 			// Write colors
-			*dst++ = da;
+			*dst++ = 255-(ia*(255-da)/255);
 			*dst++ = (sc1*a + dc1*ia)/255;
 			*dst++ = (sc2*a + dc2*ia)/255;
 			*dst++ = (sc3*a + dc3*ia)/255;