[Player-cvs] kit/jsr135 Umakefil, 1.11, 1.12 myplay.java, 1.16, 1.17 videocontrol.cpp, 1.10, 1.11

[Player-cvs] kit/jsr135 Umakefil, 1.11, 1.12 myplay.java, 1.16, 1.17 videocontrol.cpp, 1.10, 1.11

cdunn at helixcommunity.org cdunn at helixcommunity.org
Thu Apr 13 19:01:27 PDT 2006


Update of /cvsroot/player/kit/jsr135
In directory cvs02.internal.helixcommunity.org:/tmp/cvs-serv19777

Modified Files:
	Umakefil myplay.java videocontrol.cpp 
Log Message:
Add conversion to PNG

Index: videocontrol.cpp
===================================================================
RCS file: /cvsroot/player/kit/jsr135/videocontrol.cpp,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -d -r1.10 -r1.11
--- videocontrol.cpp	12 Apr 2006 18:18:45 -0000	1.10
+++ videocontrol.cpp	14 Apr 2006 02:01:24 -0000	1.11
@@ -41,6 +41,7 @@
 #include "hxengin.h"
 #include "hxsite2.h"
 #include "ciddefs.h"
+#include "netbyte.h"
 
 #include "hxthread.h"
 
@@ -56,6 +57,7 @@
 
 
 const int _VideoControl::USE_DIRECT_VIDEO = 1;
+UINT32    _VideoControl::m_dSnapshotSize = 0;
 
 _VideoControl::_VideoControl(IHXPlayer* pPlayer)
     : m_lRefCount(0)
@@ -74,7 +76,7 @@
     , m_pEvent(NULL)
     , m_pCaptureDoneEvent(NULL)
     , m_pCaptureBuffer(NULL)
-    , m_dSnapshotSize(0)
+    , m_pConvertedBuffer(NULL)
     , m_hWnd(NULL)
 {
     if (m_pPlayer)
@@ -90,6 +92,7 @@
 	if (m_pCCF)
 	{
 	    m_pCCF->CreateInstance(CLSID_IHXBuffer,(void**)&m_pCaptureBuffer);
+	    m_pCCF->CreateInstance(CLSID_IHXBuffer,(void**)&m_pConvertedBuffer);
 	}
     }
 
@@ -588,57 +591,67 @@
 	return NULL;
     }
 
-    if (m_pSiteCapture == NULL)
-    {
-	if (m_pSite && m_pSite->QueryInterface(IID_IHXSiteCapture, (void**)&m_pSiteCapture) == HXR_OK)
-	{
-	    m_pSiteCapture->AddRef();
-	}
-    }
-
     //if (!Permissions(imageType))
     // throw _SecurityException();
 
-    HXBOOL bCanCapture = FALSE;
     INT32 nOutputCID;
     HXxSize outputSize;
 
+    // Check the image type, set the output size and color ID (defined in video/include ciddefs.h)
     if (setOutputFormat(imageType, outputSize, nOutputCID) == HXR_FAIL)
     {
     	throw _MediaException("Image format not supported.");
     }
 
-    memset(&m_bmiOutputFormat, 0, sizeof(HXBitmapInfoHeader));
-
-    if (m_pSiteCapture && m_pSiteCapture->CanCapture(bCanCapture) == HXR_OK && bCanCapture)
+    // Get the IHXSiteCapture object from the site
+    if (m_pSiteCapture == NULL)
     {
-	m_pSiteCapture->Capture((IHXSiteCaptureResponse*)this, m_pCaptureBuffer, &outputSize, nOutputCID);
+	if (m_pSite && m_pSite->QueryInterface(IID_IHXSiteCapture, (void**)&m_pSiteCapture) == HXR_OK)
+	{
+	    m_pSiteCapture->AddRef();
+	}
+    }
 
-	m_pCaptureDoneEvent->Wait(); // block until Capture is done
+    // Create the buffer that will receive the image data
+    if (m_pCaptureBuffer == NULL)
+    {
+	m_pCCF->CreateInstance(CLSID_IHXBuffer,(void**)&m_pCaptureBuffer);
+    }
 
-	printf("Returned from CaptureDone: %ld\n", (m_pCaptureBuffer) ? m_pCaptureBuffer->GetSize() : -1);
-	printf("Bitmap info header: \n");
-	printf("    size  : %ld\n", m_bmiOutputFormat.biSizeImage);
-	printf("    width : %ld\n", m_bmiOutputFormat.biWidth);
-	printf("    height: %ld\n", m_bmiOutputFormat.biHeight);
+    // Clear out the bitmat info header, set in CaptureDone
+    memset(&m_bmiOutputFormat, 0, sizeof(HXBitmapInfoHeader));
 
+    HXBOOL bCanCapture = FALSE;
 
-	// Do some formatting
-	formatImage(imageType);
+    // Check if the site is available to capture an image
+    if (m_pSiteCapture && m_pSiteCapture->CanCapture(bCanCapture) == HXR_OK && bCanCapture)
+    {
+	HX_RESULT res;
+        // Asynchronous call. CaptureDone() will be called with result.
+	res = m_pSiteCapture->Capture((IHXSiteCaptureResponse*)this, m_pCaptureBuffer, &outputSize, nOutputCID);
 
-	m_dSnapshotSize = m_pCaptureBuffer->GetSize();
+	if (res == HXR_OK)
+	{
+	    m_pCaptureDoneEvent->Wait(); // block until Capture is done
 
-#ifdef TEST_PNG
-	//***** TEST
-	    FILE* fp = fopen("e:\\src\\image_in.png", "wb");
-	    int x = fwrite(m_pCaptureBuffer->GetBuffer(), 1, m_dSnapshotSize, fp);
-	    fclose(fp);
-#endif
+	    // Convert capture buffer into PNG
+	    res = formatImage();
+	    if (SUCCEEDED(res))
+	    {
+		return m_pConvertedBuffer->GetBuffer();
+	    }
+	}
     }
 
-    return (m_pCaptureBuffer) ? m_pCaptureBuffer->GetBuffer() : NULL;
+    // Need to set the snapshot size, for JNI, so that it can allocate the return buffer
+    m_dSnapshotSize = m_pCaptureBuffer->GetSize();
+    return m_pCaptureBuffer->GetBuffer();
+
 }
 
+//
+// IHXSiteCaptureResponse::CaptureDone
+//
 // Called when the site has captured the next frame.
 // bmiOutputFormat points to image format description which
 // is valid until the completion of CaptureDone.
@@ -649,9 +662,7 @@
 // data.
 //
 // status may be:
-//
-//   HXR_FAIL  -- No capture was done. General Error.
-//                All data is invalid.
+//   HXR_FAIL  -- No capture was done. General Error. All data is invalid.
 //   HXR_OK    -- Capture was done. Both variables are valid.
 STDMETHODIMP
 _VideoControl::CaptureDone(REF(HX_RESULT) status,
@@ -674,14 +685,81 @@
 {
     HX_RESULT res=HXR_OK;
 
-    if (!strcmp(imageType, "encoding=png"))
+    outputSize.cx  = 0;
+    outputSize.cy  = 0;
+
+    if (imageType == NULL)
     {
-        outputSize.cx  = 0;  // just default to native image size
-        outputSize.cy  = 0;
-        nOutputCID = CID_RGB32;
+	// Default is to convert to PNG
+        nOutputCID = CID_RGB24;
+	m_pEncoding = (const char*) "png";
+	return res;
     }
-    else
+
+    nOutputCID = CID_UNKNOWN;
+    m_pEncoding = (const char*)"";
+
+    // extract the params
+    char* c= imageType;
+    char enc[80], colors[80];
+    int i;
+    
+    while (c && *c)
     {
+	char* next = strchr(c, '&');
+	if (next)
+	{
+	    *next++ = '\0';
+	}
+
+	char* val = strchr(c, '=');
+	if (val)
+	{
+	   *val++ = '\0';
+	}
+
+	if (!strcmp(c, "encoding"))
+	{
+	    strcpy(enc, val);
+	}
+	else if (!strcmp(c, "colors"))
+	{
+	    strcpy(colors, val);
+	}
+	else if (!strcmp(c, "width"))
+	{
+	    if (sscanf(val, "%d", &i) == 1)
+	    {
+		outputSize.cx = i;
+	    }
+	}
+	else if (!strcmp(c, "height"))
+	{
+	    if (sscanf(val, "%d", &i) == 1)
+	    {
+		outputSize.cy = i;
+	    }
+	}
+	c = next;
+    }
+
+    m_pEncoding = (const char*) enc;
+
+    if (!strcmp(enc, "rgb888"))
+    {
+	nOutputCID = CID_RGB24;
+    }
+    else if (!strcmp(enc, "png"))
+    {
+	nOutputCID = CID_RGB24;
+    }
+    else if (!strcmp(enc, "yuv420"))
+    {
+	nOutputCID = CID_I420;
+    }
+    else //if (!strcmp(enc, "jpeg"))
+    {
+	// Format is unsupported
 	res = HXR_FAIL;
     }
 
@@ -689,10 +767,38 @@
 }
 
 HX_RESULT
-_VideoControl::formatImage(char* imageType)
+_VideoControl::formatImage()
 {
-	//XXXctd TODO add png encoding
-	return HXR_FAIL;
+    HX_RESULT res=HXR_OK;
+	
+    if (!strcmp(m_pEncoding, "png"))
+    {
+	m_pConvertedBuffer->SetSize(m_pCaptureBuffer->GetSize());
+	m_dSnapshotSize = 0;
+	res = writePNG(m_pCaptureBuffer,
+			m_bmiOutputFormat.biWidth,
+			m_bmiOutputFormat.biHeight,
+			PNG_COLOR_TYPE_RGB,
+			8,
+			m_pConvertedBuffer
+			);
+	printf("snapshotsize: %ld\n", m_dSnapshotSize);
+#ifdef TEST_PNG
+	if (res == HXR_OK)
+	{
+		//***** TEST
+		FILE* fp = fopen("e:\\src\\image_in.png", "wb");
+		int x = fwrite(m_pConvertedBuffer->GetBuffer(), 1, m_dSnapshotSize, fp);
+		fclose(fp);
+	}
+#endif
+    }
+    else
+    {
+	res = HXR_FAIL;
+    }
+
+    return res;
 }
 
 
@@ -848,6 +954,97 @@
     return HXR_OK;
 }
 
+/////////////////////////////////////////
+// PNG Encoding
+/////////////////////////////////////////
+void
+_VideoControl::png_write_data_buffer(png_structp png_ptr, png_bytep data, png_size_t length)
+{
+   IHXBuffer* pBuffer = (IHXBuffer*) png_get_io_ptr(png_ptr);
+
+   if ((m_dSnapshotSize + length) > pBuffer->GetSize())
+   {
+	if (pBuffer->SetSize( m_dSnapshotSize + length ) != HXR_OK)
+	{
+	    printf("ERROR could not change buffer size\n");
+	    return;
+	}
+   }
+
+   memcpy( pBuffer->GetBuffer() + m_dSnapshotSize, data, length);
+
+   m_dSnapshotSize += length;
+
+}
+
+//
+// Convert an IHXBuffer into PNG encoded IHXBuffer
+//
+HX_RESULT
+_VideoControl::writePNG(IHXBuffer* pImageBuffer, INT32 dWidth, INT32 dHeight, INT32 dColorType, INT32 dBitDepth, IHXBuffer* pPNGBuffer)
+{
+   HX_RESULT res=HXR_OK;
+   png_structp png_ptr;
+   png_infop info_ptr;
+
+   png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+   if (png_ptr == NULL)
+   {
+      return HXR_FAIL;
+   }
+
+   info_ptr = png_create_info_struct(png_ptr);
+   if (info_ptr == NULL)
+   {
+      png_destroy_write_struct(&png_ptr,  png_infopp_NULL);
+      return HXR_FAIL;
+   }
+
+   png_set_write_fn(png_ptr, (png_voidp) m_pConvertedBuffer,  (png_rw_ptr) png_write_data_buffer, png_flush_ptr_NULL);
+
+   png_set_IHDR(png_ptr, info_ptr,
+	   dWidth,
+	   dHeight,
+	   dBitDepth,
+           dColorType,
+	   PNG_INTERLACE_NONE,
+	   PNG_COMPRESSION_TYPE_DEFAULT,
+	   PNG_FILTER_TYPE_DEFAULT);
+
+   HXBOOL   bBigEndian  = TestBigEndian();
+    // Set BGR vs. RGB
+    if (!bBigEndian)
+    {
+	png_set_bgr(png_ptr);
+    }
+
+    png_write_info(png_ptr, info_ptr);
+
+    int k;
+    unsigned char** row_pointers = (unsigned char**)malloc(sizeof(unsigned char*) * dHeight);
+    int bytes_per_pixel = ( pImageBuffer->GetSize() / (dWidth * dHeight));
+
+    for (k = 0; k < dHeight; k++)
+    {
+	row_pointers[k] = pImageBuffer->GetBuffer() + (dHeight - k - 1) * dWidth * bytes_per_pixel;
+    }
+
+    // write out the entire image data in one call
+    png_write_image(png_ptr, row_pointers);
+
+    png_write_end(png_ptr, info_ptr);
+
+    png_destroy_write_struct(&png_ptr, &info_ptr);
+
+    if (row_pointers)
+    {
+	free(row_pointers);
+    }
+
+    return res;
+}
+
+////////////////////////////////////////////////////
 #include "jawt.h"
 #include "jawt_md.h"
 #include <jni.h>

Index: myplay.java
===================================================================
RCS file: /cvsroot/player/kit/jsr135/myplay.java,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -d -r1.16 -r1.17
--- myplay.java	12 Apr 2006 18:18:45 -0000	1.16
+++ myplay.java	14 Apr 2006 02:01:24 -0000	1.17
@@ -635,7 +635,16 @@
 
 	    Thread.currentThread().sleep(1000);
 
-	    byte [] snap = video.getSnapshot("encoding=png");
+	    // Double the size of the original 
+	    int width = video.getSourceWidth() * 2;
+	    int height = video.getSourceHeight() * 2;
+
+	    String imageType = "encoding=png";
+	    imageType += "&width=" + width;
+	    imageType += "&height=" + height;
+
+	    byte [] snap = video.getSnapshot(imageType);
+
 	    if (snap != null) {
 		//Image im = Image.createImage(snap, 0, snap.length);
 		System.out.println("Created image");

Index: Umakefil
===================================================================
RCS file: /cvsroot/player/kit/jsr135/Umakefil,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -d -r1.11 -r1.12
--- Umakefil	12 Apr 2006 18:18:45 -0000	1.11
+++ Umakefil	14 Apr 2006 02:01:24 -0000	1.12
@@ -44,9 +44,8 @@
                           'common/system/pub',
                           'common/util/pub',
                           'video/include',
-                          #'datatype/image/png/import/libpng',
-			  #"common/import/zlib/pub",
-                          #"datatype/image/png/common/pub",
+			  'common/import/zlib/pub',
+                          'datatype/image/png/import/libpng',
                           'client/include')
 
 project.AddIncludes('pub')
@@ -84,7 +83,8 @@
                            "client/common/container[contclntlib]",
                            "client/common/util[utlclntlib]",
                            "client/core[clntcorelib]",
-			   #"datatype/image/png/import/libpng[libpng]"
+			   "common/import/zlib[zlib]",
+			   "datatype/image/png/import/libpng[libpng]"
 )
 
 




More information about the Player-cvs mailing list
 

Site Map   |   Terms of Use   |   Privacy Policy   |   Contact Us

Copyright © 1995-2007 RealNetworks, Inc. All rights reserved. RealNetworks and Helix are trademarks of RealNetworks.
All other trademarks or registered trademarks are the property of their respective holders.