Exemple #1
0
ezResult ezBmpFileFormat::WriteImage(ezStreamWriterBase& stream, const ezImage& image, ezLogInterface* pLog) const
{
  // Technically almost arbitrary formats are supported, but we only use the common ones.
  ezImageFormat::Enum compatibleFormats[] =
  {
    ezImageFormat::B8G8R8X8_UNORM,
    ezImageFormat::B8G8R8A8_UNORM,
    ezImageFormat::B8G8R8_UNORM,
    ezImageFormat::B5G5R5X1_UNORM,
    ezImageFormat::B5G6R5_UNORM,
  };

  // Find a compatible format closest to the one the image currently has
  ezImageFormat::Enum format = ezImageConversionBase::FindClosestCompatibleFormat(image.GetImageFormat(), compatibleFormats);

  if (format == ezImageFormat::UNKNOWN)
  {
    ezLog::Error(pLog, "No conversion from format '%s' to a format suitable for BMP files known.", ezImageFormat::GetName(image.GetImageFormat()));
    return EZ_FAILURE;
  }

  // Convert if not already in a compatible format
  if (format != image.GetImageFormat())
  {
    ezImage convertedImage;
    if (ezImageConversionBase::Convert(image, convertedImage, format) != EZ_SUCCESS)
    {
      // This should never happen
      EZ_ASSERT_DEV(false, "ezImageConversion::Convert failed even though the conversion was to the format returned by FindClosestCompatibleFormat.");
      return EZ_FAILURE;
    }

    return WriteImage(stream, convertedImage, pLog);
  }
  
  ezUInt32 uiRowPitch = image.GetRowPitch(0);

  ezUInt32 uiHeight = image.GetHeight(0);

  int dataSize = uiRowPitch * uiHeight;

  ezBmpFileInfoHeader fileInfoHeader;
  fileInfoHeader.m_width = image.GetWidth(0);
  fileInfoHeader.m_height = uiHeight;
  fileInfoHeader.m_planes = 1;
  fileInfoHeader.m_bitCount = ezImageFormat::GetBitsPerPixel(format);

  fileInfoHeader.m_sizeImage = 0; // Can be zero unless we store the data compressed

  fileInfoHeader.m_xPelsPerMeter = 0;
  fileInfoHeader.m_yPelsPerMeter = 0;
  fileInfoHeader.m_clrUsed = 0;
  fileInfoHeader.m_clrImportant = 0;

  bool bWriteColorMask = false;

  // Prefer to write a V3 header
  ezUInt32 uiHeaderVersion = 3;

  switch (format)
  {
  case ezImageFormat::B8G8R8X8_UNORM:
  case ezImageFormat::B5G5R5X1_UNORM:
  case ezImageFormat::B8G8R8_UNORM:
    fileInfoHeader.m_compression = RGB;
    break;

  case ezImageFormat::B8G8R8A8_UNORM:
    fileInfoHeader.m_compression = BITFIELDS;
    uiHeaderVersion = 4;
    break;

  case ezImageFormat::B5G6R5_UNORM:
    fileInfoHeader.m_compression = BITFIELDS;
    bWriteColorMask = true;
    break;

  default:
    return EZ_FAILURE;
  }

  EZ_ASSERT_DEV(!bWriteColorMask || uiHeaderVersion <= 3, "Internal bug");

  ezUInt32 uiFileInfoHeaderSize = sizeof(ezBmpFileInfoHeader);
  ezUInt32 uiHeaderSize = sizeof(ezBmpFileHeader);

  if (uiHeaderVersion >= 4)
  {
    uiFileInfoHeaderSize += sizeof(ezBmpFileInfoHeaderV4);
  }
  else if (bWriteColorMask)
  {
    uiHeaderSize += 3 * sizeof(ezUInt32);
  }

  uiHeaderSize += uiFileInfoHeaderSize;

  fileInfoHeader.m_size = uiFileInfoHeaderSize;

  ezBmpFileHeader header;
  header.m_type = ezBmpFileMagic;
  header.m_size = uiHeaderSize + dataSize;
  header.m_reserved1 = 0;
  header.m_reserved2 = 0;
  header.m_offBits = uiHeaderSize;


  const void* dataPtr = image.GetDataPointer<void>();
  
  // Write all data
  if (stream.WriteBytes(&header, sizeof(header)) != EZ_SUCCESS)
  {
    ezLog::Error(pLog, "Failed to write header.");
    return EZ_FAILURE;
  }

  if (stream.WriteBytes(&fileInfoHeader, sizeof(fileInfoHeader)) != EZ_SUCCESS)
  {
    ezLog::Error(pLog, "Failed to write fileInfoHeader.");
    return EZ_FAILURE;
  }

  if (uiHeaderVersion >= 4)
  {
    ezBmpFileInfoHeaderV4 fileInfoHeaderV4;
    memset(&fileInfoHeaderV4, 0, sizeof(fileInfoHeaderV4));

    fileInfoHeaderV4.m_redMask = ezImageFormat::GetRedMask(format);
    fileInfoHeaderV4.m_greenMask = ezImageFormat::GetGreenMask(format);
    fileInfoHeaderV4.m_blueMask = ezImageFormat::GetBlueMask(format);
    fileInfoHeaderV4.m_alphaMask = ezImageFormat::GetAlphaMask(format);

    if (stream.WriteBytes(&fileInfoHeaderV4, sizeof(fileInfoHeaderV4)) != EZ_SUCCESS)
    {
      ezLog::Error(pLog, "Failed to write fileInfoHeaderV4.");
      return EZ_FAILURE;
    }
  }
  else if (bWriteColorMask)
  {
    struct
    {
      ezUInt32 m_red;
      ezUInt32 m_green;
      ezUInt32 m_blue;
    } colorMask;


    colorMask.m_red = ezImageFormat::GetRedMask(format);
    colorMask.m_green = ezImageFormat::GetGreenMask(format);
    colorMask.m_blue = ezImageFormat::GetBlueMask(format);

    if (stream.WriteBytes(&colorMask, sizeof(colorMask)) != EZ_SUCCESS)
    {
      ezLog::Error(pLog, "Failed to write colorMask.");
      return EZ_FAILURE;
    }
  }

  const ezUInt32 uiPaddedRowPitch = ((uiRowPitch - 1) / 4 + 1) * 4;
  // Write rows in reverse order
  for (ezInt32 iRow = uiHeight - 1; iRow >= 0; iRow--)
  {
    if (stream.WriteBytes(image.GetPixelPointer<void>(0, 0, 0, 0, iRow, 0), uiPaddedRowPitch) != EZ_SUCCESS)
    {
      ezLog::Error(pLog, "Failed to write data.");
      return EZ_FAILURE;
    }
  }
  
  return EZ_SUCCESS;
}
ezResult ezImageConversion::ConvertSingleStepDecompress(const ezImageView& source, ezImage& target, ezImageFormat::Enum sourceFormat,
                                                        ezImageFormat::Enum targetFormat, const ezImageConversionStep* pStep)
{
  for (ezUInt32 arrayIndex = 0; arrayIndex < source.GetNumArrayIndices(); arrayIndex++)
  {
    for (ezUInt32 face = 0; face < source.GetNumFaces(); face++)
    {
      for (ezUInt32 mipLevel = 0; mipLevel < source.GetNumMipLevels(); mipLevel++)
      {
        const ezUInt32 width = target.GetWidth(mipLevel);
        const ezUInt32 height = target.GetHeight(mipLevel);

        const ezUInt32 blockSizeX = ezImageFormat::GetBlockWidth(sourceFormat);
        const ezUInt32 blockSizeY = ezImageFormat::GetBlockHeight(sourceFormat);

        const ezUInt32 numBlocksX = source.GetNumBlocksX(mipLevel);
        const ezUInt32 numBlocksY = source.GetNumBlocksY(mipLevel);

        const ezUInt32 targetRowPitch = target.GetRowPitch(mipLevel);
        const ezUInt32 targetBytesPerPixel = ezImageFormat::GetBitsPerPixel(targetFormat) / 8;

        const ezUInt32 blockSizeInBytes = ezImageFormat::GetBitsPerBlock(sourceFormat) / 8;

        // Decompress into a temp memory block so we don't have to explicitly handle the case where the image is not a multiple of the block
        // size
        ezHybridArray<ezUInt8, 256> tempBuffer;
        tempBuffer.SetCount(numBlocksX * blockSizeX * blockSizeY * targetBytesPerPixel);

        for (ezUInt32 slice = 0; slice < source.GetDepth(mipLevel); slice++)
        {
          for (ezUInt32 blockY = 0; blockY < numBlocksY; blockY++)
          {
            ezImageView sourceRowView = source.GetRowView(mipLevel, face, arrayIndex, blockY, slice);

            if (static_cast<const ezImageConversionStepDecompressBlocks*>(pStep)
                    ->DecompressBlocks(sourceRowView.GetArrayPtr<void>(), ezArrayPtr<void>(tempBuffer.GetData(), tempBuffer.GetCount()),
                                       numBlocksX, sourceFormat, targetFormat)
                    .Failed())
            {
              return EZ_FAILURE;
            }

            for (ezUInt32 blockX = 0; blockX < numBlocksX; blockX++)
            {
              ezUInt8* targetPointer =
                  target.GetPixelPointer<ezUInt8>(mipLevel, face, arrayIndex, blockX * blockSizeX, blockY * blockSizeY, slice);

              // Copy into actual target, clamping to image dimensions
              ezUInt32 copyWidth = ezMath::Min(blockSizeX, width - blockX * blockSizeX);
              ezUInt32 copyHeight = ezMath::Min(blockSizeY, height - blockY * blockSizeY);
              for (ezUInt32 row = 0; row < copyHeight; row++)
              {
                memcpy(targetPointer, &tempBuffer[(blockX * blockSizeX + row) * blockSizeY * targetBytesPerPixel],
                       copyWidth * targetBytesPerPixel);
                targetPointer += targetRowPitch;
              }
            }
          }
        }
      }
    }
  }

  return EZ_SUCCESS;
}