< Summary

Class:TextureLoader
Assembly:PluginScripts
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Plugins/OBJImport/TextureLoader.cs
Covered lines:0
Uncovered lines:85
Coverable lines:85
Total lines:181
Line coverage:0% (0 of 85)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
LoadTGA(...)0%6200%
LoadDDSManual(...)0%20400%
SetNormalMap(...)0%6200%
LoadTexture(...)0%90900%
LoadTGA(...)0%42600%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Plugins/OBJImport/TextureLoader.cs

#LineLine coverage
 1/*
 2(C) 2015 AARO4130
 3PARTS OF TGA LOADING CODE (C) 2013 mikezila
 4DO NOT USE PARTS OF, OR THE ENTIRE SCRIPT, AND CLAIM AS YOUR OWN WORK
 5*/
 6
 7using System;
 8using System.IO;
 9using UnityEngine;
 10
 11public class TextureLoader : MonoBehaviour
 12{
 13    public static Texture2D LoadTGA(string fileName)
 14    {
 015        using (var imageFile = File.OpenRead(fileName))
 16        {
 017            return LoadTGA(imageFile);
 18        }
 019    }
 20
 21    public static Texture2D LoadDDSManual(string ddsPath)
 22    {
 23        try
 24        {
 25
 026            byte[] ddsBytes = File.ReadAllBytes(ddsPath);
 27
 028            byte ddsSizeCheck = ddsBytes[4];
 029            if (ddsSizeCheck != 124)
 30            {
 031                throw new System.Exception("Invalid DDS DXTn texture. Unable to read"); //this header byte should be 124
 32            }
 33
 034            int height = ddsBytes[13] * 256 + ddsBytes[12];
 035            int width = ddsBytes[17] * 256 + ddsBytes[16];
 36
 037            byte DXTType = ddsBytes[87];
 038            TextureFormat textureFormat = TextureFormat.DXT5;
 039            if (DXTType == 49)
 40            {
 041                textureFormat = TextureFormat.DXT1;
 42                //  Debug.Log ("DXT1");
 43            }
 44
 045            if (DXTType == 53)
 46            {
 047                textureFormat = TextureFormat.DXT5;
 48                //  Debug.Log ("DXT5");
 49            }
 050            int DDS_HEADER_SIZE = 128;
 051            byte[] dxtBytes = new byte[ddsBytes.Length - DDS_HEADER_SIZE];
 052            Buffer.BlockCopy(ddsBytes, DDS_HEADER_SIZE, dxtBytes, 0, ddsBytes.Length - DDS_HEADER_SIZE);
 53
 054            System.IO.FileInfo finf = new System.IO.FileInfo(ddsPath);
 055            Texture2D texture = new Texture2D(width, height, textureFormat, false);
 056            texture.LoadRawTextureData(dxtBytes);
 057            texture.Apply();
 058            texture.name = finf.Name;
 59
 060            return (texture);
 61        }
 062        catch (System.Exception ex)
 63        {
 064            Debug.LogError("Error: Could not load DDS " + ex.ToString());
 065            return new Texture2D(8, 8);
 66        }
 067    }
 68
 69    public static void SetNormalMap(ref Texture2D tex)
 70    {
 071        Color[] pixels = tex.GetPixels();
 072        for (int i = 0; i < pixels.Length; i++)
 73        {
 074            Color temp = pixels[i];
 075            temp.r = pixels[i].g;
 076            temp.a = pixels[i].r;
 077            pixels[i] = temp;
 78        }
 079        tex.SetPixels(pixels);
 080    }
 81    public static Texture2D LoadTexture(string fn, bool normalMap = false)
 82    {
 083        if (!File.Exists(fn))
 84        {
 085            return null;
 86        }
 87
 088        string ext = Path.GetExtension(fn).ToLower();
 089        if (ext == ".png" || ext == ".jpg")
 90        {
 091            Texture2D t2d = new Texture2D(1, 1);
 092            t2d.LoadImage(File.ReadAllBytes(fn));
 093            if (normalMap)
 94            {
 095                SetNormalMap(ref t2d);
 96            }
 97
 098            return t2d;
 99        }
 0100        else if (ext == ".dds")
 101        {
 0102            Texture2D returnTex = LoadDDSManual(fn);
 0103            if (normalMap)
 104            {
 0105                SetNormalMap(ref returnTex);
 106            }
 107
 0108            return returnTex;
 109        }
 0110        else if (ext == ".tga")
 111        {
 0112            Texture2D returnTex = LoadTGA(fn);
 0113            if (normalMap)
 114            {
 0115                SetNormalMap(ref returnTex);
 116            }
 117
 0118            return returnTex;
 119        }
 120        else
 121        {
 0122            Debug.Log("texture not supported : " + fn);
 123        }
 0124        return null;
 125    }
 126
 127    public static Texture2D LoadTGA(Stream TGAStream)
 128    {
 129
 0130        using (BinaryReader r = new BinaryReader(TGAStream))
 131        {
 132            // Skip some header info we don't care about.
 133            // Even if we did care, we have to move the stream seek point to the beginning,
 134            // as the previous method in the workflow left it at the end.
 0135            r.BaseStream.Seek(12, SeekOrigin.Begin);
 136
 0137            short width = r.ReadInt16();
 0138            short height = r.ReadInt16();
 0139            int bitDepth = r.ReadByte();
 140
 141            // Skip a byte of header information we don't care about.
 0142            r.BaseStream.Seek(1, SeekOrigin.Current);
 143
 0144            Texture2D tex = new Texture2D(width, height);
 0145            Color32[] pulledColors = new Color32[width * height];
 146
 0147            if (bitDepth == 32)
 148            {
 0149                for (int i = 0; i < width * height; i++)
 150                {
 0151                    byte red = r.ReadByte();
 0152                    byte green = r.ReadByte();
 0153                    byte blue = r.ReadByte();
 0154                    byte alpha = r.ReadByte();
 155
 0156                    pulledColors[i] = new Color32(blue, green, red, alpha);
 157                }
 0158            }
 0159            else if (bitDepth == 24)
 160            {
 0161                for (int i = 0; i < width * height; i++)
 162                {
 0163                    byte red = r.ReadByte();
 0164                    byte green = r.ReadByte();
 0165                    byte blue = r.ReadByte();
 166
 0167                    pulledColors[i] = new Color32(blue, green, red, 1);
 168                }
 0169            }
 170            else
 171            {
 0172                throw new Exception("TGA texture had non 32/24 bit depth.");
 173            }
 174
 0175            tex.SetPixels32(pulledColors);
 0176            tex.Apply();
 0177            return tex;
 178
 179        }
 0180    }
 181}