Hello Guest

Creating a texture from a certain area of image

  • 2 Replies
  • 9097 Views
Creating a texture from a certain area of image
« on: May 10, 2006, 16:18:56 »
So I'm trying to create from a specific part of an image (So I don't have to chop all large background images to tiny pictures). Here's my DevIL texture loader. It loads the first 128x128 of the image succesfully, but if I try to load some other region I get a DevIL crash. So if someone has some idea what I'm doing wrong then I'd like to know. My loader prolly messed up, but how?

   // Creates A Texture From A Specific Region Of Image
   private Texture GenerateAreaTexture(String path, int sx, int sy) {

      // Check If File Exists
      File file = new File(path);
      if (!file.exists())
         Core.Warning("File " + path + " not found!");
      
      // Load Given Image With DevIL
      int pf = 0;
      IntBuffer image = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
      IL.ilGenImages(image);
      IL.ilBindImage(image.get(0));
      IL.ilLoadImage(path);
        IL.ilConvertImage(IL.ilGetInteger(IL.IL_IMAGE_FORMAT), IL.IL_BYTE);
        ByteBuffer TextureBuffer = ByteBuffer.allocateDirect(TEXTURE_SIZE * TEXTURE_SIZE * 4 + 8);
        IL.ilCopyPixels(sx, sy, 0, sx + TEXTURE_SIZE, sy + TEXTURE_SIZE, 1, IL.ilGetInteger(IL.IL_IMAGE_FORMAT), IL.IL_BYTE, TextureBuffer);

      // Create a New Texture Object
        int TextureID = CreateTextureID();
        Texture t = new Texture(GL11.GL_TEXTURE_2D, TextureID);
        t.SetWidth(TEXTURE_SIZE);
        t.SetHeight(TEXTURE_SIZE);
      GL11.glBindTexture(GL11.GL_TEXTURE_2D, TextureID);
        t.SetTextureHeight(TEXTURE_SIZE);
        t.SetTextureWidth(TEXTURE_SIZE);

      // Apply Linear Filtering
      GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
      GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);

      // Determine Pixel Format
        if(IL.ilGetInteger(IL.IL_IMAGE_FORMAT) == IL.IL_RGB) {
            pf = GL11.GL_RGB;
        } else if(IL.ilGetInteger(IL.IL_IMAGE_FORMAT) == IL.IL_RGBA) {
            pf = GL11.GL_RGBA;
      }
      
      // Generate The Texture
      GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, pf, TEXTURE_SIZE, TEXTURE_SIZE,
                    0, pf, GL11.GL_UNSIGNED_BYTE, TextureBuffer);

      // Return Generated OGL Texture
      return t;
   }






#
# An unexpected error has been detected by HotSpot Virtual Machine:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x144d750c, pid=3344, tid=3348
#
# Java VM: Java HotSpot(TM) Client VM (1.5.0-b64 mixed mode)
# Problematic frame:
# C  [DevIL.dll+0x1750c]
#

---------------  T H R E A D  ---------------

Current thread (0x000362c8):  JavaThread "main" [_thread_in_native, id=3348]

siginfo: ExceptionCode=0xc0000005, writing address 0x0adc3000

Registers:
EAX=0x00012000, EBX=0x06d15c58, ECX=0x000984e5, EDX=0x0adb1000
ESP=0x0007f894, EBP=0x0007f8c0, ESI=0x147c0020, EDI=0x000362c8
EIP=0x144d750c, EFLAGS=0x00010206

Top of Stack: (sp=0x0007f894)
0x0007f894:   00090000 00000100 00000600 147c0020
0x0007f8a4:   00000030 00000400 00000000 0adb1000
0x0007f8b4:   00000400 00000000 00000004 0007f8f4
0x0007f8c4:   144d786e 00000100 00000100 00000180
0x0007f8d4:   00000180 0adb1000 000362c8 06d15c58
0x0007f8e4:   00090000 00090000 00000000 0adb1000
0x0007f8f4:   0007f970 14491969 00000100 00000100
0x0007f904:   00000000 00000180 00000180 00000001

Instructions: (pc=0x144d750c)
0x144d74fc:   dc 03 45 ec 03 45 f8 8b 55 f0 8b 75 e0 8a 0c 0e
0x144d750c:   88 0c 02 eb bb eb 9f eb 83 8b 15 34 ca 58 14 8b


Stack: [0x00040000,0x00080000),  sp=0x0007f894,  free space=254k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
C  [DevIL.dll+0x1750c]
C  [DevIL.dll+0x1786e]
C  [lwjgl-devil.dll+0x1969]
j  org.lwjgl.devil.IL.ilCopyPixels(IIIIIIIILjava/nio/ByteBuffer;)I+24
j  Relay.OpenGL.TextureLoader.GenerateAreaTexture(Ljava/lang/String;II)LRelay/OpenGL/Texture;+139
j  Relay.OpenGL.TextureLoader.GetAreaTexture(Ljava/lang/String;II)LRelay/OpenGL/Texture;+25
j  Relay.Core.Core.Run()V+16
j  Relay.Core.Core.Execute()V+5
j  Relay.Core.Core.main([Ljava/lang/String;)V+36
v  ~StubRoutines::call_stub
V  [jvm.dll+0x8168d]
V  [jvm.dll+0xd4179]
V  [jvm.dll+0x8155e]
V  [jvm.dll+0x8844c]
C  [java.exe+0x14c0]
C  [java.exe+0x64cd]
C  [kernel32.dll+0x16d4f]

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j  org.lwjgl.devil.IL.nilCopyPixels(IIIIIIIILjava/nio/ByteBuffer;I)I+0
j  org.lwjgl.devil.IL.ilCopyPixels(IIIIIIIILjava/nio/ByteBuffer;)I+24
j  Relay.OpenGL.TextureLoader.GenerateAreaTexture(Ljava/lang/String;II)LRelay/OpenGL/Texture;+139
j  Relay.OpenGL.TextureLoader.GetAreaTexture(Ljava/lang/String;II)LRelay/OpenGL/Texture;+25
j  Relay.Core.Core.Run()V+16
j  Relay.Core.Core.Execute()V+5
j  Relay.Core.Core.main([Ljava/lang/String;)V+36
v  ~StubRoutines::call_stub

---------------  P R O C E S S  ---------------

Java Threads: ( => current thread )
  0x00a914f0 JavaThread "Low Memory Detector" daemon [_thread_blocked, id=3372]
  0x00a900c8 JavaThread "CompilerThread0" daemon [_thread_blocked, id=3368]
  0x00a8f3f8 JavaThread "Signal Dispatcher" daemon [_thread_blocked, id=3364]
  0x00a86838 JavaThread "Finalizer" daemon [_thread_blocked, id=3360]
  0x00a853a8 JavaThread "Reference Handler" daemon [_thread_blocked, id=3356]
=>0x000362c8 JavaThread "main" [_thread_in_native, id=3348]

Other Threads:
  0x00a829d8 VMThread [id=3352]
  0x00a926c8 WatcherThread [id=3376]

VM state:not at safepoint (normal execution)

VM Mutex/Monitor currently owned by a thread: None

Heap
 def new generation   total 576K, used 174K [0x02ad0000, 0x02b70000, 0x02fb0000)
  eden space 512K,  22% used [0x02ad0000, 0x02aec9c8, 0x02b50000)
  from space 64K,  94% used [0x02b50000, 0x02b5f110, 0x02b60000)
  to   space 64K,   0% used [0x02b60000, 0x02b60000, 0x02b70000)
 tenured generation   total 1408K, used 131K [0x02fb0000, 0x03110000, 0x06ad0000)
   the space 1408K,   9% used [0x02fb0000, 0x02fd0dc0, 0x02fd0e00, 0x03110000)
 compacting perm gen  total 8192K, used 2413K [0x06ad0000, 0x072d0000, 0x0aad0000)
   the space 8192K,  29% used [0x06ad0000, 0x06d2b460, 0x06d2b600, 0x072d0000)
No shared spaces configured.

Dynamic libraries:
0x00400000 - 0x0040c000    D:\Program Files\Java\JDK 1.5.0\bin\java.exe
0x7c900000 - 0x7c9af000    C:\WINDOWS\system32\ntdll.dll
0x7c800000 - 0x7c8f4000    C:\WINDOWS\system32\kernel32.dll
0x77dc0000 - 0x77e6a000    C:\WINDOWS\system32\ADVAPI32.dll
0x77e70000 - 0x77f01000    C:\WINDOWS\system32\RPCRT4.dll
0x77c00000 - 0x77c58000    C:\WINDOWS\system32\MSVCRT.dll
0x6d640000 - 0x6d7c5000    D:\Program Files\Java\JDK 1.5.0\jre\bin\client\jvm.dll
0x77d30000 - 0x77dc0000    C:\WINDOWS\system32\USER32.dll
0x77f10000 - 0x77f56000    C:\WINDOWS\system32\GDI32.dll
0x76b30000 - 0x76b5d000    C:\WINDOWS\system32\WINMM.dll
0x6bd00000 - 0x6bd0d000    C:\WINDOWS\system32\SYNCOR11.DLL
0x6d280000 - 0x6d288000    D:\Program Files\Java\JDK 1.5.0\jre\bin\hpi.dll
0x76be0000 - 0x76beb000    C:\WINDOWS\system32\PSAPI.DLL
0x6d610000 - 0x6d61c000    D:\Program Files\Java\JDK 1.5.0\jre\bin\verify.dll
0x6d300000 - 0x6d31d000    D:\Program Files\Java\JDK 1.5.0\jre\bin\java.dll
0x6d630000 - 0x6d63f000    D:\Program Files\Java\JDK 1.5.0\jre\bin\zip.dll
0x10000000 - 0x10041000    C:\LWJGL\native\lwjgl.dll
0x72270000 - 0x7229c000    C:\WINDOWS\system32\DINPUT.dll
0x5f250000 - 0x5f31c000    C:\WINDOWS\system32\OPENGL32.dll
0x61560000 - 0x61581000    C:\WINDOWS\system32\GLU32.dll
0x73730000 - 0x73779000    C:\WINDOWS\system32\DDRAW.dll
0x73b90000 - 0x73b96000    C:\WINDOWS\system32\DCIMAN32.dll
0x77bf0000 - 0x77bf8000    C:\WINDOWS\system32\VERSION.dll
0x5b2a0000 - 0x5b2d8000    C:\WINDOWS\system32\uxtheme.dll
0x746f0000 - 0x7473b000    C:\WINDOWS\system32\MSCTF.dll
0x69000000 - 0x694fd000    C:\WINDOWS\system32\atioglxx.dll
0x0f040000 - 0x0f6b8000    C:\WINDOWS\system32\atioglx1.dll
0x68e80000 - 0x68e89000    C:\WINDOWS\system32\HID.DLL
0x77910000 - 0x77a05000    C:\WINDOWS\system32\SETUPAPI.DLL
0x76c20000 - 0x76c4e000    C:\WINDOWS\system32\WINTRUST.dll
0x77a70000 - 0x77b04000    C:\WINDOWS\system32\CRYPT32.dll
0x77b10000 - 0x77b22000    C:\WINDOWS\system32\MSASN1.dll
0x76c80000 - 0x76ca8000    C:\WINDOWS\system32\IMAGEHLP.dll
0x14490000 - 0x144ae000    C:\LWJGL\native\lwjgl-devil.dll
0x144c0000 - 0x1464f000    C:\LWJGL\native\DevIL.dll
0x14650000 - 0x14668000    C:\LWJGL\native\ILU.dll
0x14670000 - 0x1467d000    C:\LWJGL\native\ILUT.dll
0x76390000 - 0x763d9000    C:\WINDOWS\system32\comdlg32.dll
0x77f60000 - 0x77fd6000    C:\WINDOWS\system32\SHLWAPI.dll
0x5d5e0000 - 0x5d677000    C:\WINDOWS\system32\COMCTL32.dll
0x7c9c0000 - 0x7d1d5000    C:\WINDOWS\system32\SHELL32.dll
0x771a0000 - 0x77246000    C:\WINDOWS\system32\WININET.dll
0x77110000 - 0x7719c000    C:\WINDOWS\system32\OLEAUT32.dll
0x774d0000 - 0x7760c000    C:\WINDOWS\system32\ole32.dll
0x773c0000 - 0x774c2000    C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-Controls_6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9\comctl32.dll

VM Arguments:
jvm_args: -Djava.library.path=C:\LWJGL\native
java_command: Relay.Core.Core -window

Environment Variables:
PATH=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\ATI Technologies\ATI Control Panel
USERNAME=Default
OS=Windows_NT
PROCESSOR_IDENTIFIER=x86 Family 6 Model 8 Stepping 1, AuthenticAMD


---------------  S Y S T E M  ---------------

OS: Windows XP Build 2600 Service Pack 2

CPU:total 1 family 6, cmov, cx8, fxsr, mmx, sse

Memory: 4k page, physical 523808k(303352k free), swap 1280276k(1071316k free)

vm_info: Java HotSpot(TM) Client VM (1.5.0-b64) for windows-x86, built on Sep 15 2004 03:00:31 by "java_re" with MS VC++ 6.0

hmmmmmm....
« Reply #1 on: May 10, 2006, 17:17:02 »
The IL.ilCopyPixels() call has some wrong parameters...
sx + TEXTURE_SIZE, sy + TEXTURE_SIZE should be the width and the height (i.e. TEXTURE_SIZE, TEXTURE_SIZE).
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

Creating a texture from a certain area of image
« Reply #2 on: May 11, 2006, 15:49:29 »
I thought that the parameters would have been in x1,y1,x2,y2-style instead of x, y, width, height. I even looked the parameters from the DevIL-manual. Oh well. I think I'll get it working now...  :lol: