Gdiplus Bitmap.lockbits problem
Hi,
We have tried to invoke a native dll method using JNI in java.(Dll Created in VC++)
While invoking the dll method(below native code dll) we face the "Access violation error".
But the very same native implementation code is working successfully when invoked from VC++ Exe application.
We have given the Java class file, header file, CPP implementation of the method and the error report below.
Can anybody let us know if there is some fault in the way code we have coded?
**
*
*Source Code:
*
---------------------
_______________________________________WsqLib.java __________________________________________________ __________________
class WsqLib
{
private native int BitmapToWSQ();
private native int WsqToBitmap();
public static void main(String[] args)
{
WsqLib wsqlib=new WsqLib();
System.out.println("wsqlib.BitmapToWSQ() ==> "+wsqlib.BitmapToWSQ());
}
static
{
System.loadLibrary("WsqLib");
}
}
*
# Create a class (WsqLib.java) that declares the native method.
# We use javac to compile the WsqLib source file, resulting in the class file WsqLib.class.
# We use javah to generate a C header file (WsqLib.h) containing the function prototype for the native method implementation.*
*_______________________________________WsqLib.h__ __________________________________________________ ___________________
*
/* DO NOT EDIT THIS FILE - it is machine generated */
#include "jni.h"
/* Header for class WsqLib */
#ifndef _Included_WsqLib
#define _Included_WsqLib
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: WsqLib
* Method: BitmapToWSQ
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_WsqLib_BitmapToWSQ
(JNIEnv *, jobject);
/*
Class: WsqLib
Method: WsqToBitmap
Signature: ()I
*/
JNIEXPORT jint JNICALL Java_WsqLib_WsqToBitmap
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
*
# CPP implementation (WsqLib.cpp) of the native method is as below.
# We compiled the cpp implementation into a native library, creating WsqLib.dll.
# We use the WsqLib.dll in WsqLib.java.
*
_______________________________________WsqLib.cpp_ __________________________________________________ ___________________
#include "stdafx.h"
#include "WsqLib.h"
#include "_vcclrit.h"
#include "gdiplus.h"
#include <afxstr.h>
#include <atlimage.h>
#include "WsqLib1.h"
static const GUID guidBmp =
{ 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e } };
extern "C"
{
int debug = 0;
}
JNIEXPORT jint JNICALL Java_WsqLib_BitmapToWSQ(JNIEnv *, jobject)
{
Gdiplus::Bitmap *pBitmap = Gdiplus::Bitmap::FromFile(L"temp.bmp", TRUE);
Gdiplus::BitmapData lockedBitmapData;
Gdiplus::Rect rect(0, 0,pBitmap->GetWidth(),pBitmap->GetHeight());
//Error occured in this Line.
pBitmap->LockBits( &rect, Gdiplus::ImageLockModeRead, PixelFormat8bppIndexed, &lockedBitmapData );
int byteCount = pBitmap->GetWidth() * pBitmap->GetHeight() * (int)(8 / 8);
unsigned char *pPixelData2 = new unsigned char[byteCount];
memset(pPixelData2, 0, (size_t)byteCount);
unsigned char *pBytes = (unsigned char*) lockedBitmapData.Scan0;
for (int index = 0; index < byteCount; index++)
{
pPixelData2[index] = pBytes[index];
}
pBitmap->UnlockBits(&lockedBitmapData);
unsigned char *pPixelData = pPixelData2;
int pixelsPerInch = (int)pBitmap->GetHorizontalResolution();
float compressionFactor = (float)( (float)((int)75) / 100.0);
unsigned char *pWsqBytes = NULL;
int wsqBytesCount = 0;
int result = 0;
result = wsq_encode_mem(
&pWsqBytes, // Output data buffer
&wsqBytesCount, // Length of output data buffer
compressionFactor, // Determines the amount of lossy compression.
pPixelData, // Input pixel data
pBitmap->GetWidth(), // Pixel width of the pixmap
pBitmap->GetHeight(), // Pixel height of the pixmap
8, // Depth (bits) of the pixmap
pixelsPerInch, // Scan resolution of the image in integer units of pixels per inch
(char *)NULL // Optional (we do not want to use this - we may even want to remove the implementation)
);
if (result == 0)
{
FILE *fptr1;
fptr1=fopen("C:
saran2.wsq_1.0","wb");
fwrite(pWsqBytes,wsqBytesCount,1,fptr1);
fclose(fptr1);
}
return 10;
}
JNIEXPORT jint JNICALL Java_WsqLib_WsqToBitmap(JNIEnv *, jobject)
{
BYTE gData[242998];
Gdiplus::Bitmap* image;
CFile file;
file.Open("temp.wsq_1.0",CFile::modeRead,NULL);
file.Read(gData,242998);
unsigned char *pByteArray =(unsigned char*)gData;
unsigned char *pPixelData = NULL;
int bitmapWidth = 0;
int bitmapHeight = 0;
int pixelDepth = 0;
int pixelsPerInch = 0;
int lossyFlag = 1;
int byteArrayLength = 242998;
int result = 0;
result = wsq_decode_mem(
&pPixelData, // Output image buffer
&bitmapWidth, // Pixel width of the pixmap
&bitmapHeight, // Pixel height of the pixmap
&pixelDepth, // Depth (bits) of the pixmap
&pixelsPerInch, // Scan resolution of the image in integer units of pixels per inch
&lossyFlag, // Is the image lossy? F.I.I.K.
pByteArray, // Input data buffer
byteArrayLength // Input data buffer length
);
if (result == 0 && pixelDepth == 8)
{
image = new Gdiplus::Bitmap(bitmapWidth, bitmapHeight);
Gdiplus::Rect bound( 0, 0,bitmapWidth,bitmapHeight);
Gdiplus::BitmapData lockedBitmapData;
image->LockBits( &bound, Gdiplus::ImageLockModeWrite, PixelFormat8bppIndexed, &lockedBitmapData );
int imageRowSize = bitmapWidth * (8/8);
unsigned char *pBitmapPixelData = (unsigned char *)lockedBitmapData.Scan0;
int byteCount = bitmapWidth * bitmapHeight;
for (int index = 0; index < byteCount; index++)
{
pBitmapPixelData[index] = pPixelData[index];
}
image->UnlockBits( &lockedBitmapData );
}
else
{
return result;
}
image->Save(L"C:
temp.bmp", &guidBmp);
return 0;
}
*
Error report:
*
-------------------
# An unexpected error has been detected by HotSpot Virtual Machine:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x03920780, pid=380, tid=3572
#
# Java VM: Java HotSpot(TM) Client VM (1.5.0_12-b04 mixed mode, sharing)
# Problematic frame:
# C 0x03920780
#
T H R E A D
Current thread (0x00036d88): JavaThread "main" [_thread_in_native, id=3572]
siginfo: ExceptionCode=0xc0000005, reading address 0x00000004
Registers:
EAX=0x00000000, EBX=0x00000000, ECX=0x00000000, EDX=0x00000000
ESP=0x0007f9a8, EBP=0x0007fa70, ESI=0x00000000, EDI=0x00000000
EIP=0x03920780, EFLAGS=0x00010246
Top of Stack: (sp=0x0007f9a8)
0x0007f9a8: 00000000 00000000 00000000 00000000
0x0007f9b8: 039204ba 00000001 00000000 000be780
0x0007f9c8: 03726248 0007fa10 00000000 00000000
0x0007f9d8: 03920430 0007fa2c 7a32b4fa ffffffff
0x0007f9e8: 0007fa38 79e7bba9 79e7bbb1 cce41966
0x0007f9f8: ffffffff 0007fa68 000a4b30 00000000
0x0007fa08: 00000000 00000000 00000000 00000000
0x0007fa18: 00000000 00000000 00000000 00000000
Instructions: (pc=0x03920780)
0x03920770: 03 00 74 05 e8 85 1b 77 76 33 d2 89 14 24 8b fe
0x03920780: 8b 4e 04 8d 14 24 e8 05 65 e0 ff 8b d8 8b d3 8b
Stack: [0x00040000,0x00080000), sp=0x0007f9a8, free space=254k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
C 0x03920780
C 0x031e3171
j WsqLib.BitmapToWSQ()I+0
j WsqLib.main([Ljava/lang/String;)V+24
v ~StubRoutines::call_stub
V [jvm.dll+0x87599]
V [jvm.dll+0xdfbb2]
V [jvm.dll+0x8746a]
V [jvm.dll+0x8e6ac]
C [java.exe+0x14c5]
C [java.exe+0x69cd]
C [kernel32.dll+0x16d4f]
Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j WsqLib.BitmapToWSQ()I+0
j WsqLib.main([Ljava/lang/String;)V+24
v ~StubRoutines::call_stub
P R O C E S S
Java Threads: ( => current thread )
0x009f2b28 JavaThread "Low Memory Detector" daemon [_thread_blocked, id=3996]
0x009f1820 JavaThread "CompilerThread0" daemon [_thread_blocked, id=2832]
0x009f0a68 JavaThread "Signal Dispatcher" daemon [_thread_blocked, id=404]
0x009eb330 JavaThread "Finalizer" daemon [_thread_blocked, id=3304]
0x009e9eb0 JavaThread "Reference Handler" daemon [_thread_blocked, id=1648]
=>0x00036d88 JavaThread "main" [_thread_in_native, id=3572]
Other Threads:
0x009c8508 VMThread [id=492]
0x009f0528 WatcherThread [id=2924]
VM state:not at safepoint (normal execution)
VM Mutex/Monitor currently owned by a thread: None
Heap
def new generation total 576K, used 497K [0x22a50000, 0x22af0000, 0x22f30000)
eden space 512K, 84% used [0x22a50000, 0x22abc5b8, 0x22ad0000)
from space 64K, 99% used [0x22ae0000, 0x22aefff8, 0x22af0000)
to space 64K, 0% used [0x22ad0000, 0x22ad0000, 0x22ae0000)
tenured generation total 1408K, used 115K [0x22f30000, 0x23090000, 0x26a50000)
the space 1408K, 8% used [0x22f30000, 0x22f4cc28, 0x22f4ce00, 0x23090000)
compacting perm gen total 8192K, used 20K [0x26a50000, 0x27250000, 0x2aa50000)
the space 8192K, 0% used [0x26a50000, 0x26a553b8, 0x26a55400, 0x27250000)
ro space 8192K, 63% used [0x2aa50000, 0x2af60590, 0x2af60600, 0x2b250000)
rw space 12288K, 46% used [0x2b250000, 0x2b7f21b0, 0x2b7f2200, 0x2be50000)
Dynamic libraries:
0x00400000 - 0x0040d000 C:\WINDOWS\system32\java.exe
0x7c900000 - 0x7c9b0000 C:\WINDOWS\system32\ntdll.dll
0x7c800000 - 0x7c8f4000 C:\WINDOWS\system32\kernel32.dll
0x77dd0000 - 0x77e6b000 C:\WINDOWS\system32\ADVAPI32.dll
0x77e70000 - 0x77f01000 C:\WINDOWS\system32\RPCRT4.dll
0x77c10000 - 0x77c68000 C:\WINDOWS\system32\MSVCRT.dll
0x6d640000 - 0x6d7dd000 C:\Program Files\Java\jre1.5.0_12\bin\client\jvm.dll
0x77d40000 - 0x77dd0000 C:\WINDOWS\system32\USER32.dll
0x77f10000 - 0x77f56000 C:\WINDOWS\system32\GDI32.dll
0x76b40000 - 0x76b6d000 C:\WINDOWS\system32\WINMM.dll
0x6d290000 - 0x6d298000 C:\Program Files\Java\jre1.5.0_12\bin\hpi.dll
0x76bf0000 - 0x76bfb000 C:\WINDOWS\system32\PSAPI.DLL
0x6d610000 - 0x6d61c000 C:\Program Files\Java\jre1.5.0_12\bin\verify.dll
0x6d310000 - 0x6d32d000 C:\Program Files\Java\jre1.5.0_12\bin\java.dll
0x6d630000 - 0x6d63f000 C:\Program Files\Java\jre1.5.0_12\bin\zip.dll
0x10000000 - 0x10075000 D:\Jagadeesan\JNI_NEW\WSQ\WsqLib.dll
0x79000000 - 0x79045000 C:\WINDOWS\system32\mscoree.dll
0x7c140000 - 0x7c357000 C:\WINDOWS\system32\MFC71D.DLL
0x10200000 - 0x10287000 C:\WINDOWS\system32\MSVCR71D.dll
0x77f60000 - 0x77fd6000 C:\WINDOWS\system32\SHLWAPI.dll
0x77120000 - 0x771ac000 C:\WINDOWS\system32\OLEAUT32.dll
0x774e0000 - 0x7761c000 C:\WINDOWS\system32\ole32.dll
0x4ec50000 - 0x4edf3000 C:\WINDOWS\WinSxS\x86_Microsoft.Windows.GdiPlus_65 95b64144ccf1df_1.0.2600.2180_x-ww_522f9f82\gdiplus.dll
0x5d360000 - 0x5d36e000 C:\WINDOWS\system32\MFC71ENU.DLL
0x79e70000 - 0x7a3d1000 C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\msco rwks.dll
0x78130000 - 0x781cb000 C:\WINDOWS\WinSxS\x86_Microsoft.VC80.CRT_1fc8b3b9a 1e18e3b_8.0.50727.42_x-ww_0de06acd\MSVCR80.dll
0x7c9c0000 - 0x7d1d4000 C:\WINDOWS\system32\shell32.dll
0x773d0000 - 0x774d2000 C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-Controls_6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9\comctl32.dll
0x5d090000 - 0x5d127000 C:\WINDOWS\system32\comctl32.dll
0x790c0000 - 0x79ba6000 C:\WINDOWS\assembly\NativeImages_v2.0.50727_32\msc orlib\bf3b003e3ac7e449bdf8fc9375318452\mscorlib.ni .dll
0x79060000 - 0x790b3000 C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\msco rjit.dll
0x5e380000 - 0x5e409000 C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\dias ymreader.dll
VM Arguments:
java_command: WsqLib
Launcher Type: SUN_STANDARD
Environment Variables:
JAVA_HOME=C:\Program Files\Java\jdk1.5.0_12;
CLASSPATH=C:\Program Files\Apache Software Foundation\Tomcat 5.5\shared\lib\*.jar;C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\axis\WEB-INF\classes\org\apache\axis\transport\http\*.class s;D:\JONAS_4_6_6\lib\ext\axis.jar;C:\Sun\jwsdp-1.6\jaxp\lib\endorsed\sax.jar;C:\tomcat50-jwsdp\jaxp\lib\endorsed\sax.jar;C:\Files\Java\jdk1 .5.0_01\jre\lib;C:\Program Files\Java\jdk1.5.0_12\jre\lib\ext;
PATH=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\Sys tem32\Wbem;D:\Jagadeesan Backup\DotNet\DynamicLibrary\MathFuncsDll\Debug;C: \Program Files\Microsoft SQL Server\80\Tools\Binn\;C:\Program Files\Microsoft SQL Server\90\Tools\binn\;C:\Program Files\Microsoft SQL Server\90\DTS\Binn\;C:\Program Files\Microsoft SQL Server\90\Tools\Binn\VSShell\Common7\IDE\;C:\Progr am Files\Microsoft Visual Studio 8\Common7\IDE\PrivateAssemblies;C:\Sun\jwsdp-1.6\jaxrpc\bin;C:\Sun\jwsdp-1.6\jaxp\lib;C:\Sun\jwsdp-1.6\jaxp\lib\endorsed;C:\Program Files\Java\jdk1.5.0_12\bin;C:\Sun\jwsdp-1.6\jwsdp-shared\bin;C:\Program Files\Microsoft Visual Studio\Common\Tools\WinNT;C:\Sun\jwsdp-1.6\jaxrpc\bin;C:\Sun\jwsdp-1.6\jaxp\lib;C:\Sun\jwsdp-1.6\jaxp\lib\endorsed;C:\Program Files\Java\jdk1.5.0_12\bin;
USERNAME=Administrator
OS=Windows_NT
PROCESSOR_IDENTIFIER=x86 Family 15 Model 1 Stepping 2, GenuineIntel
S Y S T E M
OS: Windows XP Build 2600 Service Pack 2
CPU:total 1 (cores per cpu 1, threads per core 1) family 15 model 1 stepping 2, cmov, cx8, fxsr, mmx, sse, sse2
Memory: 4k page, physical 259888k(81380k free), swap 636388k(342588k free)
vm_info: Java HotSpot(TM) Client VM (1.5.0_12-b04) for windows-x86, built on May 2 2007 02:07:59 by "java_re" with MS VC++ 6.0
|