From d32f24f147068e6cbecb31c7f98047f68bc8b58a Mon Sep 17 00:00:00 2001
From: Normand Briere <nbriere@noware.ca>
Date: Sun, 21 Jul 2019 19:03:28 -0400
Subject: [PATCH] Embed texture.

---
 CameraPane.java |  100 +++++++++++++++++++++++++++++---------------------
 1 files changed, 58 insertions(+), 42 deletions(-)

diff --git a/CameraPane.java b/CameraPane.java
index 1364b89..acc49b8 100644
--- a/CameraPane.java
+++ b/CameraPane.java
@@ -2405,6 +2405,45 @@
         return currentGL;
     }
 
+    private BufferedImage CreateBim(TextureData texturedata)
+    {
+        // cache to disk
+        Buffer buffer = texturedata.getBuffer(); // getMipmapData();
+    //buffers[0].
+        ByteBuffer bytebuf = (ByteBuffer)buffer; // ).asIntBuffer();
+        int[] pixels = new int[bytebuf.capacity()/3];
+        int width = texturedata.getWidth(); //(int)Math.sqrt(pixels.length); // squared
+        int height = width;
+        for (int i=pixels.length; --i>=0;)
+        {
+            int i3 = i*3;
+            pixels[i] = 0xFF;
+            pixels[i] <<= 8;
+            pixels[i] |= bytebuf.get(i3+2) & 0xFF;
+            pixels[i] <<= 8;
+            pixels[i] |= bytebuf.get(i3+1) & 0xFF;
+            pixels[i] <<= 8;
+            pixels[i] |= bytebuf.get(i3) & 0xFF;
+        }
+        /*
+        int r=0,g=0,b=0,a=0;
+        for (int i=0; i<width; i++)
+        for (int j=0; j<height; j++)
+        {
+        int index = j*width+i;
+        int p = pixels[index];
+        a = ((p>>24) & 0xFF);
+        r = ((p>>16) & 0xFF);
+        g = ((p>>8) & 0xFF);
+        b = (p & 0xFF);
+        pixels[index] = (a<<24) | (b<<16) | (g<<8) | r;
+        }
+        /**/
+        BufferedImage rendImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // ImageIO.read(infile);
+        rendImage.setRGB(0,0,width,height,pixels,width*(height-1),-width);
+        return rendImage;
+    }
+
     /**/
     class CacheTexture
     {
@@ -8308,9 +8347,9 @@
             {
                 TextureData texturedata = null;
             
-                if (bim == null)
+                if (bim != null)
                 {
-                    
+                    texturecache = new CacheTexture(GetBimTexture(bim, bump), -1);
                 }
                 else
                 if (tex.equals("DEFAULT_TEXTURE")) // ||*/ tex.equals(""))
@@ -8329,6 +8368,8 @@
                         {
                             texturecache = new CacheTexture(GetResourceTexture("default.png", bump),resolution);
                         }
+                        else
+                            new Exception().printStackTrace();
 //                    }
                 } else
                 if (tex.equals("DEFAULT_TEXTURE_BUMP")) // ||*/ tex.equals(""))
@@ -8337,6 +8378,8 @@
                     texturecache = textures.get(tex);
                     if (texturecache == null)
                         texturecache = new CacheTexture(GetResourceTexture("default.png", bump),resolution);
+                    else
+                        new Exception().printStackTrace();
                 } else
                 {
                     //if (tex.equals("IMMORTAL"))
@@ -8349,6 +8392,8 @@
                             texturecache = textures.get(tex);
                             if (texturecache == null)
                                 texturecache = new CacheTexture(GetResourceTexture("whitenoise.png", bump),resolution);
+                            else
+                                new Exception().printStackTrace();
                         } else
                         {
                             if (textureon)
@@ -8417,6 +8462,7 @@
                 if (/*CACHETEXTURE &&*/ texturecache != null && textureon)
                 {
                     //return false;
+                    assert(bim == null);
 
              //       System.out.println("CACHE  +++++++++++++++  TEXTURE : " + texname + " (" + texture.getEstimatedMemorySize() + ")");
                     if (texturedata != null && texname.toLowerCase().endsWith(".jpg"))
@@ -8436,52 +8482,17 @@
                         File cachefile = new File(texname.substring(0, texname.length()-4)+ext+".jpg");
                         if (!cachefile.exists())
                         {
-                            // cache to disk
-                            Buffer buffer = texturedata.getBuffer(); // getMipmapData();
-                        //buffers[0].
-
-                            ByteBuffer bytebuf = (ByteBuffer)buffer; // ).asIntBuffer();
-                            int[] pixels = new int[bytebuf.capacity()/3];
-                            
-                            // squared size heuristic...
-                            if ((int)Math.sqrt(pixels.length) == Math.sqrt(pixels.length))
+                            //if (texturedata.getWidth() == texturedata.getHeight())
                             {
-                                for (int i=pixels.length; --i>=0;)
-                                {
-                                    int i3 = i*3;
-                                    pixels[i] = 0xFF;
-                                    pixels[i] <<= 8;
-                                    pixels[i] |= bytebuf.get(i3+2) & 0xFF;
-                                    pixels[i] <<= 8;
-                                    pixels[i] |= bytebuf.get(i3+1) & 0xFF;
-                                    pixels[i] <<= 8;
-                                    pixels[i] |= bytebuf.get(i3) & 0xFF;
-                                }
-
-                /*
-                int r=0,g=0,b=0,a=0;
-                for (int i=0; i<width; i++)
-                for (int j=0; j<height; j++)
-                {
-                int index = j*width+i;
-                int p = pixels[index];
-                a = ((p>>24) & 0xFF);
-                r = ((p>>16) & 0xFF);
-                g = ((p>>8) & 0xFF);
-                b = (p & 0xFF);
-                pixels[index] = (a<<24) | (b<<16) | (g<<8) | r;
-                }
-                /**/
-                                int width = (int)Math.sqrt(pixels.length); // squared
-                                int height = width;
-                                BufferedImage rendImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // ImageIO.read(infile);
-                                rendImage.setRGB(0,0,width,height,pixels,width*(height-1),-width);
+                                BufferedImage rendImage = CreateBim(texturedata);
+                                
                                 ImageWriter writer = null;
                                 Iterator iter = ImageIO.getImageWritersByFormatName("jpg");
                                 if (iter.hasNext()) {
                                     writer = (ImageWriter)iter.next();
                                 }
-                                float compressionQuality = 0.9f;
+                                
+                                float compressionQuality = 0.85f;
                                 try
                                 {
                                     ImageOutputStream ios = ImageIO.createImageOutputStream(cachefile);
@@ -8528,6 +8539,11 @@
         return texturecache;
     }
     
+    static void EmbedTextures(cTexture tex)
+    {
+        
+    }
+    
     com.sun.opengl.util.texture.Texture GetTexture(java.awt.image.BufferedImage stream, String tex, boolean bump, int resolution) throws Exception
     {
         CacheTexture texture = GetCacheTexture(stream, tex, bump, resolution);

--
Gitblit v1.6.2