Example #1
0
    private TextureFormat Format2Format(OIIOAPI.BASETYPE format, int nchannels)
    {
        TextureFormat defaultFormat = TextureFormat.RGBA32;

        switch (format)
        {
        case OIIOAPI.BASETYPE.UCHAR:
        case OIIOAPI.BASETYPE.CHAR:
        {
            switch (nchannels)
            {
            case 1: return(TextureFormat.R8);

            case 2: return(TextureFormat.RG16);

            case 3: return(TextureFormat.RGB24);

            case 4: return(TextureFormat.RGBA32);

            default: return(defaultFormat);
            }
        }

        case OIIOAPI.BASETYPE.HALF:
        {
            switch (nchannels)
            {
            case 1: return(TextureFormat.RHalf);

            case 2: return(TextureFormat.RGHalf);

            case 3: return(TextureFormat.RGBAHalf);        // RGBHalf is NOT SUPPORTED

            case 4: return(TextureFormat.RGBAHalf);

            default: return(defaultFormat);
            }
        }

        case OIIOAPI.BASETYPE.FLOAT:
        {
            switch (nchannels)
            {
            case 1: return(TextureFormat.RFloat);

            case 2: return(TextureFormat.RGFloat);

            case 3: return(TextureFormat.RGBAFloat);        // RGBFloat is NOT SUPPORTED

            case 4: return(TextureFormat.RGBAFloat);

            default: return(defaultFormat);
            }
        }

        default: return(defaultFormat);
        }
    }
Example #2
0
        public static Texture2D LoadTextureOIIO(string filePath, bool isLinear)
        {
            // TODO: need to flip? Repere bottom left, Y-up
            int ret = OIIOAPI.oiio_open_image(filePath);

            if (ret == 0)
            {
                Debug.LogWarning("Could not open image " + filePath + " with OIIO.");
                return(null);
            }

            int width     = -1;
            int height    = -1;
            int nchannels = -1;

            OIIOAPI.BASETYPE format = OIIOAPI.BASETYPE.NONE;
            ret = OIIOAPI.oiio_get_image_info(ref width, ref height, ref nchannels, ref format);
            if (ret == 0)
            {
                Debug.LogWarning("Could not get info about image " + filePath + " with OIIO");
                return(null);
            }

            TexConv conv       = new TexConv();
            bool    canConvert = Format2Format(format, nchannels, ref conv);

            if (!canConvert)
            {
                Debug.LogWarning("Could not create image from format: " + conv.format + " with option: " + conv.options);
                return(CreateSmallImage());
            }
            // TMP
            else if (conv.options.HasFlag(TextureConversionOptions.SHORT_TO_FLOAT) || conv.options.HasFlag(TextureConversionOptions.SHORT_TO_INT))
            {
                Debug.LogWarning("Could not create image from format: " + conv.format + " with option: " + conv.options);
                return(CreateSmallImage());
            }

            Texture2D image = new Texture2D(width, height, conv.format, true, isLinear); // with mips

            var      pixels = image.GetRawTextureData();
            GCHandle handle = GCHandle.Alloc(pixels, GCHandleType.Pinned);

            ret = OIIOAPI.oiio_fill_image_data(handle.AddrOfPinnedObject(), conv.options.HasFlag(TextureConversionOptions.RGB_TO_RGBA) ? 1 : 0);
            if (ret == 1)
            {
                image.LoadRawTextureData(pixels);
                image.Apply();
            }
            else
            {
                Debug.LogWarning("Could not fill texture data of " + filePath + " with OIIO.");
                return(null);
            }

            return(image);
        }
Example #3
0
        private static bool Format2Format(OIIOAPI.BASETYPE format, int nchannels, ref TexConv result)
        {
            // TODO: handle compressed formats.

            result.format  = TextureFormat.RGBA32;
            result.options = TextureConversionOptions.NO_CONV;

            switch (format)
            {
            case OIIOAPI.BASETYPE.UCHAR:
            case OIIOAPI.BASETYPE.CHAR:
                switch (nchannels)
                {
                case 1: result.format = TextureFormat.R8; break;

                case 2: result.format = TextureFormat.RG16; break;

                case 3: result.format = TextureFormat.RGB24; break;

                case 4: result.format = TextureFormat.RGBA32; break;

                default: return(false);
                }
                break;

            case OIIOAPI.BASETYPE.USHORT:
                switch (nchannels)
                {
                case 1: result.format = TextureFormat.R16; break;

                case 2: result.format = TextureFormat.RGFloat; result.options = TextureConversionOptions.SHORT_TO_FLOAT; break;

                case 3: result.format = TextureFormat.RGBAFloat; result.options = TextureConversionOptions.SHORT_TO_FLOAT | TextureConversionOptions.RGB_TO_RGBA; break;

                case 4: result.format = TextureFormat.RGBAFloat; result.options = TextureConversionOptions.SHORT_TO_FLOAT; break;

                // R16_G16, R16_G16_B16 and R16_G16_B16_A16 do not exist
                default: return(false);
                }
                break;

            case OIIOAPI.BASETYPE.HALF:
                switch (nchannels)
                {
                case 1: result.format = TextureFormat.RHalf; break;

                case 2: result.format = TextureFormat.RGHalf; break;

                case 3: result.format = TextureFormat.RGBAHalf; result.options = TextureConversionOptions.NO_CONV | TextureConversionOptions.RGB_TO_RGBA; break;         // RGBHalf is NOT SUPPORTED

                case 4: result.format = TextureFormat.RGBAHalf; break;

                default: return(false);
                }
                break;

            case OIIOAPI.BASETYPE.FLOAT:
                switch (nchannels)
                {
                case 1: result.format = TextureFormat.RFloat; break;

                case 2: result.format = TextureFormat.RGFloat; break;

                case 3: result.format = TextureFormat.RGBAFloat; result.options = TextureConversionOptions.NO_CONV | TextureConversionOptions.RGB_TO_RGBA; break;        // RGBFloat is NOT SUPPORTED

                case 4: result.format = TextureFormat.RGBAFloat; break;

                default: return(false);
                }
                break;

            default: return(false);
            }

            return(true);
        }
Example #4
0
    public override void OnImportAsset(UnityEditor.AssetImporters.AssetImportContext ctx)
    {
        // NOTE: repere bas gauche, Y up.

        int ret = OIIOAPI.oiio_open_image(assetPath);

        if (ret == 0)
        {
            Debug.Log("could not open " + assetPath);
            return;
        }

        int width     = -1;
        int height    = -1;
        int nchannels = -1;

        OIIOAPI.BASETYPE format = OIIOAPI.BASETYPE.NONE;
        ret = OIIOAPI.oiio_get_image_info(ref width, ref height, ref nchannels, ref format);
        if (ret == 0)
        {
            Debug.Log("Could not get width/height of " + assetPath);
            return;
        }

        imageDimensions.Set(width, height);
        TextureFormat textureFormat = Format2Format(format, nchannels);
        var           image         = new Texture2D(width, height, textureFormat, false, true); // with mips, linear

        int do_rgb_to_rgba = 0;

        if ((format == OIIOAPI.BASETYPE.FLOAT && nchannels == 3) ||
            (format == OIIOAPI.BASETYPE.HALF && nchannels == 3))
        {
            do_rgb_to_rgba = 1;
        }
        //Color[] pixels = image.GetPixels();
        var      pixels = image.GetRawTextureData();
        GCHandle handle = GCHandle.Alloc(pixels, GCHandleType.Pinned);

        ret = OIIOAPI.oiio_fill_image_data(handle.AddrOfPinnedObject(), do_rgb_to_rgba);
        if (ret == 1)
        {
            image.LoadRawTextureData(pixels);
            //image.SetPixels(pixels);
            image.Apply();
        }
        else
        {
            Debug.Log("Could not fill texture data of " + assetPath);
            return;
        }



#if UNITY_2017_3_OR_NEWER
        var filename = Path.GetFileNameWithoutExtension(assetPath);
        ctx.AddObjectToAsset(filename, image);
        ctx.SetMainObject(image);
#else
        ctx.SetMainObject(image);
#endif
    }