Shinya Kitaoka 810553
#pragma once
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef TTIO_TZL_INCLUDED
Toshihiro Shimizu 890ddd
#define TTIO_TZL_INCLUDED
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tlevel_io.h"
Toshihiro Shimizu 890ddd
#include <set></set>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class TImageWriterTzl;
Toshihiro Shimizu 890ddd
class TImageReaderTzl;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===========================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*!
Toshihiro Shimizu 890ddd
  TLevelWriterTzl:
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class TzlChunk {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  TINT32 m_offs;
Shinya Kitaoka 120a6e
  TINT32 m_length;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TzlChunk(TINT32 offs, TINT32 length) : m_offs(offs), m_length(length) {}
Shinya Kitaoka 120a6e
  TzlChunk() : m_offs(0), m_length(0) {}
Shinya Kitaoka 120a6e
  bool operator<(const TzlChunk &c) const { return m_offs < c.m_offs; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
typedef std::map<tframeid, tzlchunk=""> TzlOffsetMap;</tframeid,>
Toshihiro Shimizu 890ddd
class TRasterCodecLZO;
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class TLevelWriterTzl final : public TLevelWriter {
Shinya Kitaoka 120a6e
  // bool m_paletteWritten;
Shinya Kitaoka 120a6e
  bool m_headerWritten;
Shinya Kitaoka 120a6e
  bool m_creatorWritten;
Shinya Kitaoka 120a6e
  FILE *m_chan;
Shinya Kitaoka 120a6e
  TLevelP m_level;
Shinya Kitaoka 120a6e
  TFilePath m_path, m_palettePath;
Shinya Kitaoka 120a6e
  long m_frameCountPos;
Shinya Kitaoka 120a6e
  int m_frameCount;
Shinya Kitaoka 120a6e
  TzlOffsetMap m_frameOffsTable;
Shinya Kitaoka 120a6e
  TzlOffsetMap m_iconOffsTable;
Shinya Kitaoka 120a6e
  std::set<tzlchunk> m_freeChunks;</tzlchunk>
Shinya Kitaoka 120a6e
  bool m_exists;
Shinya Kitaoka 120a6e
  TPalette *m_palette;
Shinya Kitaoka 120a6e
  TDimension m_res;
Shinya Kitaoka 120a6e
  TINT32 m_offsetTablePos;
Shinya Kitaoka 120a6e
  TINT32 m_iconOffsetTablePos;
Shinya Kitaoka 120a6e
  std::map<tframeid, tframeid=""> m_renumberTable;</tframeid,>
Shinya Kitaoka 120a6e
  const char *m_magic;
Shinya Kitaoka 120a6e
  int m_version;
Shinya Kitaoka 120a6e
  bool m_updatedIconsSize;
Shinya Kitaoka 120a6e
  TDimension m_userIconSize;  // IconSize settata dall'utente
Shinya Kitaoka 120a6e
  TDimension
Shinya Kitaoka 120a6e
      m_iconSize;  // IconSize in the file according to image aspect ratio.
Shinya Kitaoka 120a6e
  TDimension m_currentIconSize;  // If file exists this is the current IconSize
Shinya Kitaoka 120a6e
                                 // in the file
Shinya Kitaoka 120a6e
  TRasterCodecLZO *m_codec;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  bool m_overwritePaletteFlag;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  TLevelWriterTzl(const TFilePath &path, TPropertyGroup *winfo);
Shinya Kitaoka 120a6e
  ~TLevelWriterTzl();
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  void setPalette(TPalette *palette) override;
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  void setOverwritePaletteFlag(bool overwrite) override {
Shinya Kitaoka 120a6e
    m_overwritePaletteFlag = overwrite;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  void renumberFids(const std::map<tframeid, tframeid=""> &renumberTable) override;</tframeid,>
Shinya Kitaoka 120a6e
  /*!
Shinya Kitaoka 120a6e
                  Setta le dimensioni dell'iconcina nel file.
Shinya Kitaoka 120a6e
                  ATTENZIONE: va necessariamente richiamata prima di
Shinya Kitaoka 120a6e
     renumberFids()!
Shinya Kitaoka 120a6e
          */
Shinya Kitaoka 473e70
  void setIconSize(TDimension iconSize) override;
Shinya Kitaoka 473e70
  TDimension getIconSize() const override { return m_iconSize; }
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  TImageWriterP getFrameWriter(TFrameId fid) override;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // friend class TImageWriterTzl;
Shinya Kitaoka 120a6e
  // Save Image and icon
Shinya Kitaoka 120a6e
  void save(const TImageP &img);
Shinya Kitaoka 120a6e
  void save(const TImageP &img, const TFrameId &fid);
Shinya Kitaoka 120a6e
  // save only icon
Shinya Kitaoka 120a6e
  void saveIcon(const TImageP &img, const TFrameId &fid);
Shinya Kitaoka 120a6e
  // check icon size. Return True if newSize is not equal to currentIconSize
Shinya Kitaoka 120a6e
  // (the iconSize in File)
Shinya Kitaoka 120a6e
  bool checkIconSize(const TDimension &newSize);
Shinya Kitaoka 120a6e
  // Update all icons with new size. Return true if success.
Shinya Kitaoka 120a6e
  bool resizeIcons(const TDimension &newSize);
Shinya Kitaoka 120a6e
  void remove(const TFrameId &fid);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /*!
Shinya Kitaoka 120a6e
           Da informazioni relativamente allo spazio libero presente su file.
Shinya Kitaoka 120a6e
           Ritorna un numero compreso tra 0 e 1: 0 nessuno spazio libero, 1
Shinya Kitaoka 120a6e
     tutto lo spazio รจ libero.
Shinya Kitaoka 120a6e
   */
Shinya Kitaoka 120a6e
  float getFreeSpace();
Shinya Kitaoka 120a6e
  /*!
Shinya Kitaoka 120a6e
     Save the file without freeSpace.
Shinya Kitaoka 120a6e
     Salva tutti i frame in maniera continua, senza buchi.
Shinya Kitaoka 120a6e
     Return TRUE if successfully.
Shinya Kitaoka 120a6e
   */
Shinya Kitaoka 120a6e
  bool optimize();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  static TLevelWriter *create(const TFilePath &f, TPropertyGroup *winfo) {
Shinya Kitaoka 120a6e
    return new TLevelWriterTzl(f, winfo);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Shinya Kitaoka 120a6e
  bool m_adjustRatio;
Shinya Kitaoka 120a6e
  void doSave(const TImageP &img, const TFrameId &fid);
Shinya Kitaoka 120a6e
  // Save image on disk. If isIcon is true save image as icon.
Shinya Kitaoka 120a6e
  void saveImage(const TImageP &img, const TFrameId &fid, bool isIcon = false);
Shinya Kitaoka 120a6e
  void createIcon(const TImageP &imgIn, TImageP &imgOut);
Shinya Kitaoka 120a6e
  bool convertToLatestVersion();
Shinya Kitaoka 120a6e
  void writeHeader(const TDimension &size);
Shinya Kitaoka 120a6e
  void buildFreeChunksTable();
Shinya Kitaoka 120a6e
  void addFreeChunk(TINT32 offs, TINT32 length);
Shinya Kitaoka 120a6e
  TINT32 findSavingChunk(const TFrameId &fid, TINT32 length,
Shinya Kitaoka 120a6e
                         bool isIcon = false);
Shinya Kitaoka 120a6e
  // not implemented
Shinya Kitaoka 120a6e
  TLevelWriterTzl(const TLevelWriterTzl &);
Shinya Kitaoka 120a6e
  TLevelWriterTzl &operator=(const TLevelWriterTzl &);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===========================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*!
Toshihiro Shimizu 890ddd
  TLevelReaderTzl:
Toshihiro Shimizu 890ddd
 */
Shinya Kitaoka d1f6c4
class TLevelReaderTzl final : public TLevelReader {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  TLevelReaderTzl(const TFilePath &path);
Shinya Kitaoka 120a6e
  ~TLevelReaderTzl();
Shinya Kitaoka 473e70
  void doReadPalette(bool doReadIt) override;
Shinya Kitaoka 120a6e
  /*!
Shinya Kitaoka 120a6e
Return info about current tzl
Shinya Kitaoka 120a6e
*/
Shinya Kitaoka 473e70
  TLevelP loadInfo() override;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /*!
Shinya Kitaoka 120a6e
Return an image with Reader information
Shinya Kitaoka 120a6e
*/
Shinya Kitaoka 473e70
  TImageReaderP getFrameReader(TFrameId fid) override;
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  QString getCreator() override;
Shinya Kitaoka 120a6e
  friend class TImageReaderTzl;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /*!
Shinya Kitaoka 120a6e
                  Return TLV version
Shinya Kitaoka 120a6e
          */
Shinya Kitaoka 120a6e
  int getVersion() { return m_version; }
Shinya Kitaoka 120a6e
  /*!
Shinya Kitaoka 120a6e
                  Get the iconSize in the file. Return TRUE if icon exists,
Shinya Kitaoka 120a6e
     return FALSE if it not exists.
Shinya Kitaoka 120a6e
          */
Shinya Kitaoka 120a6e
  bool getIconSize(TDimension &iconSize);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Shinya Kitaoka 120a6e
  FILE *m_chan;
Shinya Kitaoka 120a6e
  TLevelP m_level;
Shinya Kitaoka 120a6e
  TDimension m_res;
Shinya Kitaoka 120a6e
  double m_xDpi, m_yDpi;
Shinya Kitaoka 120a6e
  // int m_frameIndex;
Shinya Kitaoka 120a6e
  // TzlOffsetMap m_frameOffset;//per le vecchie tzl
Shinya Kitaoka 120a6e
  TzlOffsetMap m_frameOffsTable;
Shinya Kitaoka 120a6e
  TzlOffsetMap m_iconOffsTable;
Shinya Kitaoka 120a6e
  int m_version;
Shinya Kitaoka 120a6e
  QString m_creator;
Shinya Kitaoka 120a6e
  bool m_readPalette;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  static TLevelReader *create(const TFilePath &f) {
Shinya Kitaoka 120a6e
    return new TLevelReaderTzl(f);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Shinya Kitaoka 120a6e
  void readPalette();
Shinya Kitaoka 120a6e
  // not implemented
Shinya Kitaoka 120a6e
  TLevelReaderTzl(const TLevelReaderTzl &);
Shinya Kitaoka 120a6e
  TLevelReaderTzl &operator=(const TLevelReaderTzl &);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
#endif  // TTIO_TZL_INCLUDED