GithubHelp home page GithubHelp logo

Comments (3)

miccTronic avatar miccTronic commented on June 12, 2024

I can say that I had similar issues with some DDS files, where Luminance-type DDS files (I think that's similar to your BC4_linear_grayscale) which should have the luminance in the alpha channel ended up having it in the red channel. I'm not sure if it's an ambiguity of the DDS format, or the parser not interpreting all header fields properly (especially the R/G/B/A bit mask fields), but in any case the culprit is probably in the code from SharpDX's DDS utilities, which HelixToolkit just uses for DDS reading.
I ended up changing the shader to user a different channel, but you may be easier off if you can get your exporter to save the DDS in a compatible format...

from helix-toolkit.

softinitializing avatar softinitializing commented on June 12, 2024

That's looks about right, maybe it's sharpdx. But modifying the shader is not in my capabilities and I can't change the Dds format since they are needed like this somewhere else,
I could do a work around so that instead of loading the Dds to texture model I could convert it to color4[] first, then use it for the texture model
but this would increase the time needed for rendering the image and my conversion creates a memory overhead {from 800mb to 2GB in 15-30s}

 public static TextureModel DDsToTextureModel(string filepath)
 {
    

     using (var image = Pfimage.FromFile(filepath))
     {
         if (image.Compressed)
             image.Decompress();

         var carray = ConvertIImageToColor4Array(image);
         

         return new TextureModel(carray, image.Width, image.Height);
     }
 }
   public static Color4[] ConvertIImageToColor4Array(IImage image)
  {
      byte[] imageData = image.Data;
      int width = image.Width;
      int height = image.Height;

      // Number of pixels
      int pixelCount = width * height;
      Color4[] colors = new Color4[pixelCount];

      for (int i = 0; i < pixelCount; i++)
      {
          // Calculate pixel byte offset in imageData
          int row = i / width;
          int col = i % width;
          int offset = row * image.Stride + col * (image.BitsPerPixel / 8);

          // Extract Color4 based on image format
          colors[i] = ExtractColorFromPixel(imageData, offset, image.Format);
      }

      return colors;
  }

  private static Color4 ExtractColorFromPixel(byte[] imageData, int offset, ImageFormat format)
  {
      Color4 color = new Color4();
      switch (format)
      {
          case ImageFormat.Rgb8:
              // Assuming it might be grayscale where R=G=B with 8 bits.
              byte gray = imageData[offset];
              color.Red = color.Green = color.Blue = gray / 255f;
              color.Alpha = 1f;
              break;
          case ImageFormat.R5g5b5:
              // Assuming data is little-endian; adjust if big-endian.
              ushort pixelValue = BitConverter.ToUInt16(imageData, offset);
              color.Red = ((pixelValue >> 10) & 0x1F) / 31f;
              color.Green = ((pixelValue >> 5) & 0x1F) / 31f;
              color.Blue = (pixelValue & 0x1F) / 31f;
              color.Alpha = 1f;
              break;
          case ImageFormat.R5g6b5:
              pixelValue = BitConverter.ToUInt16(imageData, offset);
              color.Red = ((pixelValue >> 11) & 0x1F) / 31f;
              color.Green = ((pixelValue >> 5) & 0x3F) / 63f;
              color.Blue = (pixelValue & 0x1F) / 31f;
              color.Alpha = 1f;
              break;
          case ImageFormat.R5g5b5a1:
              pixelValue = BitConverter.ToUInt16(imageData, offset);
              color.Red = ((pixelValue >> 11) & 0x1F) / 31f;
              color.Green = ((pixelValue >> 6) & 0x1F) / 31f;
              color.Blue = ((pixelValue >> 1) & 0x1F) / 31f;
              color.Alpha = (pixelValue & 0x1) != 0 ? 1f : 0f;
              break;
          case ImageFormat.Rgba16:
              // 4 bits for each channel
              ushort rgbaValue = BitConverter.ToUInt16(imageData, offset);
              color.Red = ((rgbaValue >> 12) & 0xF) / 15f;
              color.Green = ((rgbaValue >> 8) & 0xF) / 15f;
              color.Blue = ((rgbaValue >> 4) & 0xF) / 15f;
              color.Alpha = (rgbaValue & 0xF) / 15f;
              break;
          case ImageFormat.Rgb24:
              color.Red = imageData[offset + 2] / 255f;
              color.Green = imageData[offset + 1] / 255f;
              color.Blue = imageData[offset] / 255f;
              color.Alpha = 1f;
              break;
          case ImageFormat.Rgba32:
              color.Red = imageData[offset + 2] / 255f;
              color.Green = imageData[offset + 1] / 255f;
              color.Blue = imageData[offset + 0] / 255f;
              color.Alpha = imageData[offset + 3] / 255f;
              break;
          default:
              throw new ArgumentOutOfRangeException(nameof(format), "Unsupported image format.");
      }

      return color;
  }

from helix-toolkit.

holance avatar holance commented on June 12, 2024

If you are interested in debugging the issue, the source is in https://github.com/helix-toolkit/helix-toolkit/blob/develop/Source/HelixToolkit.SharpDX.Shared/SharpDX.Toolkit/Graphics/DDSHelper.cs

from helix-toolkit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.