Einzelnen Beitrag anzeigen

TiGü

Registriert seit: 6. Apr 2011
Ort: Berlin
3.060 Beiträge
 
Delphi 10.4 Sydney
 
#2

AW: Anzeigen von DDS Dateien

  Alt 28. Nov 2017, 16:48
Da wirst du um das Laden per Stream und dann händisch auseinander basteln wohl nicht drum rum kommen.
Außer natürlich, jemand hat noch irgendwo eine fertige Unit parat.

Bist du mit dem Programm ILSpy und Paint.Net vertraut?
Mit dem Ersten kannst du im Zweiten nachschauen, wie im Namespace PaintDotNet.Data.Dds -> DdsFile diese Texturen geladen werden.

Beispiel:
Code:
      public void Load(Stream input)
      {
         uint ddsTag = (uint)input.ReadUInt32();
         if (ddsTag != 542327876u)
         {
            throw new FormatException("File does not appear to be a DDS image");
         }
         this.m_header.Read(input);
         if (this.m_header.m_pixelFormat.m_flags == 0u || (this.m_header.m_pixelFormat.m_flags & 4u) != 0u)
         {
            uint fourCC = this.m_header.m_pixelFormat.m_fourCC;
            int squishFlags;
            if (fourCC != 827611204u)
            {
               if (fourCC != 861165636u)
               {
                  if (fourCC != 894720068u)
                  {
                     throw new FormatException("File is not a supported DDS format");
                  }
                  squishFlags = 4;
               }
               else
               {
                  squishFlags = 2;
               }
            }
            else
            {
               squishFlags = 1;
            }
            int blockCount = (this.GetWidth() + 3) / 4 * ((this.GetHeight() + 3) / 4);
            int blockSize = ((squishFlags & 1) != 0) ? 8 : 16;
            byte[] compressedBlocks = new byte[blockCount * blockSize];
            input.Read(compressedBlocks, 0, compressedBlocks.GetLength(0));
            this.m_pixelData = DdsSquish.DecompressImage(compressedBlocks, this.GetWidth(), this.GetHeight(), squishFlags);
            return;
         }
         DdsFileFormat fileFormat;
         if (this.m_header.m_pixelFormat.m_flags == 65u && this.m_header.m_pixelFormat.m_rgbBitCount == 32u && this.m_header.m_pixelFormat.m_rBitMask == 16711680u && this.m_header.m_pixelFormat.m_gBitMask == 65280u && this.m_header.m_pixelFormat.m_bBitMask == 255u && this.m_header.m_pixelFormat.m_aBitMask == 4278190080u)
         {
            fileFormat = DdsFileFormat.DDS_FORMAT_A8R8G8B8;
         }
         else if (this.m_header.m_pixelFormat.m_flags == 64u && this.m_header.m_pixelFormat.m_rgbBitCount == 32u && this.m_header.m_pixelFormat.m_rBitMask == 16711680u && this.m_header.m_pixelFormat.m_gBitMask == 65280u && this.m_header.m_pixelFormat.m_bBitMask == 255u && this.m_header.m_pixelFormat.m_aBitMask == 0u)
         {
            fileFormat = DdsFileFormat.DDS_FORMAT_X8R8G8B8;
         }
         else if (this.m_header.m_pixelFormat.m_flags == 65u && this.m_header.m_pixelFormat.m_rgbBitCount == 32u && this.m_header.m_pixelFormat.m_rBitMask == 255u && this.m_header.m_pixelFormat.m_gBitMask == 65280u && this.m_header.m_pixelFormat.m_bBitMask == 16711680u && this.m_header.m_pixelFormat.m_aBitMask == 4278190080u)
         {
            fileFormat = DdsFileFormat.DDS_FORMAT_A8B8G8R8;
         }
         else if (this.m_header.m_pixelFormat.m_flags == 64u && this.m_header.m_pixelFormat.m_rgbBitCount == 32u && this.m_header.m_pixelFormat.m_rBitMask == 255u && this.m_header.m_pixelFormat.m_gBitMask == 65280u && this.m_header.m_pixelFormat.m_bBitMask == 16711680u && this.m_header.m_pixelFormat.m_aBitMask == 0u)
         {
            fileFormat = DdsFileFormat.DDS_FORMAT_X8B8G8R8;
         }
         else if (this.m_header.m_pixelFormat.m_flags == 65u && this.m_header.m_pixelFormat.m_rgbBitCount == 16u && this.m_header.m_pixelFormat.m_rBitMask == 31744u && this.m_header.m_pixelFormat.m_gBitMask == 992u && this.m_header.m_pixelFormat.m_bBitMask == 31u && this.m_header.m_pixelFormat.m_aBitMask == 32768u)
         {
            fileFormat = DdsFileFormat.DDS_FORMAT_A1R5G5B5;
         }
         else if (this.m_header.m_pixelFormat.m_flags == 65u && this.m_header.m_pixelFormat.m_rgbBitCount == 16u && this.m_header.m_pixelFormat.m_rBitMask == 3840u && this.m_header.m_pixelFormat.m_gBitMask == 240u && this.m_header.m_pixelFormat.m_bBitMask == 15u && this.m_header.m_pixelFormat.m_aBitMask == 61440u)
         {
            fileFormat = DdsFileFormat.DDS_FORMAT_A4R4G4B4;
         }
         else if (this.m_header.m_pixelFormat.m_flags == 64u && this.m_header.m_pixelFormat.m_rgbBitCount == 24u && this.m_header.m_pixelFormat.m_rBitMask == 16711680u && this.m_header.m_pixelFormat.m_gBitMask == 65280u && this.m_header.m_pixelFormat.m_bBitMask == 255u && this.m_header.m_pixelFormat.m_aBitMask == 0u)
         {
            fileFormat = DdsFileFormat.DDS_FORMAT_R8G8B8;
         }
         else
         {
            if (this.m_header.m_pixelFormat.m_flags != 64u || this.m_header.m_pixelFormat.m_rgbBitCount != 16u || this.m_header.m_pixelFormat.m_rBitMask != 63488u || this.m_header.m_pixelFormat.m_gBitMask != 2016u || this.m_header.m_pixelFormat.m_bBitMask != 31u || this.m_header.m_pixelFormat.m_aBitMask != 0u)
            {
               throw new FormatException("File is not a supported DDS format");
            }
            fileFormat = DdsFileFormat.DDS_FORMAT_R5G6B5;
         }
         int srcPixelSize = (int)(this.m_header.m_pixelFormat.m_rgbBitCount / 8u);
         int rowPitch;
         if ((this.m_header.m_headerFlags & 8u) != 0u)
         {
            rowPitch = (int)this.m_header.m_pitchOrLinearSize;
         }
         else if ((this.m_header.m_headerFlags & 524288u) != 0u)
         {
            rowPitch = (int)(this.m_header.m_pitchOrLinearSize / this.m_header.m_height);
         }
         else
         {
            rowPitch = (int)(this.m_header.m_width * (uint)srcPixelSize);
         }
         byte[] readPixelData = new byte[(long)rowPitch * (long)((ulong)this.m_header.m_height)];
         input.Read(readPixelData, 0, readPixelData.GetLength(0));
         this.m_pixelData = new byte[this.m_header.m_width * this.m_header.m_height * 4u];
         for (int destY = 0; destY < (int)this.m_header.m_height; destY++)
         {
            for (int destX = 0; destX < (int)this.m_header.m_width; destX++)
            {
               int srcPixelOffset = destY * rowPitch + destX * srcPixelSize;
               uint pixelColour = 0u;
               uint pixelRed = 0u;
               uint pixelGreen = 0u;
               uint pixelBlue = 0u;
               uint pixelAlpha = 0u;
               for (int loop = 0; loop < srcPixelSize; loop++)
               {
                  pixelColour |= (uint)((uint)readPixelData[srcPixelOffset + loop] << 8 * loop);
               }
               if (fileFormat == DdsFileFormat.DDS_FORMAT_A8R8G8B8)
               {
                  pixelAlpha = (pixelColour >> 24 & 255u);
                  pixelRed = (pixelColour >> 16 & 255u);
                  pixelGreen = (pixelColour >> 8 & 255u);
                  pixelBlue = (pixelColour & 255u);
               }
               else if (fileFormat == DdsFileFormat.DDS_FORMAT_X8R8G8B8)
               {
                  pixelAlpha = 255u;
                  pixelRed = (pixelColour >> 16 & 255u);
                  pixelGreen = (pixelColour >> 8 & 255u);
                  pixelBlue = (pixelColour & 255u);
               }
               else if (fileFormat == DdsFileFormat.DDS_FORMAT_A8B8G8R8)
               {
                  pixelAlpha = (pixelColour >> 24 & 255u);
                  pixelRed = (pixelColour & 255u);
                  pixelGreen = (pixelColour >> 8 & 255u);
                  pixelBlue = (pixelColour >> 16 & 255u);
               }
               else if (fileFormat == DdsFileFormat.DDS_FORMAT_X8B8G8R8)
               {
                  pixelAlpha = 255u;
                  pixelRed = (pixelColour & 255u);
                  pixelGreen = (pixelColour >> 8 & 255u);
                  pixelBlue = (pixelColour >> 16 & 255u);
               }
               else if (fileFormat == DdsFileFormat.DDS_FORMAT_A1R5G5B5)
               {
                  pixelAlpha = (pixelColour >> 15) * 255u;
                  pixelRed = (pixelColour >> 10 & 31u);
                  pixelGreen = (pixelColour >> 5 & 31u);
                  pixelBlue = (pixelColour & 31u);
                  pixelRed = (pixelRed << 3 | pixelRed >> 2);
                  pixelGreen = (pixelGreen << 3 | pixelGreen >> 2);
                  pixelBlue = (pixelBlue << 3 | pixelBlue >> 2);
               }
               else if (fileFormat == DdsFileFormat.DDS_FORMAT_A4R4G4B4)
               {
                  pixelAlpha = (pixelColour >> 12 & 255u);
                  pixelRed = (pixelColour >> 8 & 15u);
                  pixelGreen = (pixelColour >> 4 & 15u);
                  pixelBlue = (pixelColour & 15u);
                  pixelAlpha = (pixelAlpha << 4 | pixelAlpha);
                  pixelRed = (pixelRed << 4 | pixelRed);
                  pixelGreen = (pixelGreen << 4 | pixelGreen);
                  pixelBlue = (pixelBlue << 4 | pixelBlue);
               }
               else if (fileFormat == DdsFileFormat.DDS_FORMAT_R8G8B8)
               {
                  pixelAlpha = 255u;
                  pixelRed = (pixelColour >> 16 & 255u);
                  pixelGreen = (pixelColour >> 8 & 255u);
                  pixelBlue = (pixelColour & 255u);
               }
               else if (fileFormat == DdsFileFormat.DDS_FORMAT_R5G6B5)
               {
                  pixelAlpha = 255u;
                  pixelRed = (pixelColour >> 11 & 31u);
                  pixelGreen = (pixelColour >> 5 & 63u);
                  pixelBlue = (pixelColour & 31u);
                  pixelRed = (pixelRed << 3 | pixelRed >> 2);
                  pixelGreen = (pixelGreen << 2 | pixelGreen >> 4);
                  pixelBlue = (pixelBlue << 3 | pixelBlue >> 2);
               }
               int destPixelOffset = destY * (int)this.m_header.m_width * 4 + destX * 4;
               this.m_pixelData[destPixelOffset] = (byte)pixelRed;
               this.m_pixelData[destPixelOffset + 1] = (byte)pixelGreen;
               this.m_pixelData[destPixelOffset + 2] = (byte)pixelBlue;
               this.m_pixelData[destPixelOffset + 3] = (byte)pixelAlpha;
            }
         }
      }
  Mit Zitat antworten Zitat