/* This file is part of the KDE project
 *   Copyright (c) 2004 Casper Boemann <cbr@boemann.dkt>
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#ifndef KIS_TILED_ITERATOR_H_
#define KIS_TILED_ITERATOR_H_

#include <tqglobal.h>

#include <ksharedptr.h>

#include <kis_tile.h>
#include <kis_tileddatamanager.h>
#include <koffice_export.h>
/**
 * The KisIterator class iterates through the pixels of a KisPaintDevice hiding the tile structure
 */
class KRITACORE_EXPORT KisTiledIterator : public TDEShared {

protected:
    KisTiledDataManager *m_ktm;
    TQ_INT32 m_pixelSize;        // bytes per pixel
    TQ_INT32 m_x;        // current x position
    TQ_INT32 m_y;        // cirrent y position
    TQ_INT32 m_row;    // row in tilemgr
    TQ_INT32 m_col;    // col in tilemgr
    TQ_UINT8 *m_data;
    TQ_UINT8 *m_oldData;
    TQ_INT32 m_offset;
    KisTile *m_tile;
    KisTile* m_oldTile;
    bool m_writable;

protected:
    inline TQ_UINT32 xToCol(TQ_UINT32 x) const { if (m_ktm) return m_ktm->xToCol(x); else return 0; };
    inline TQ_UINT32 yToRow(TQ_UINT32 y) const { if (m_ktm) return m_ktm->yToRow(y); else return 0; };
    void fetchTileData(TQ_INT32 col, TQ_INT32 row);

public:
    KisTiledIterator( KisTiledDataManager *ktm);
    KisTiledIterator(const KisTiledIterator&);
    KisTiledIterator& operator=(const KisTiledIterator&);
    ~KisTiledIterator();

public:
    // current x position
    TQ_INT32 x() const { return m_x; };

    // cirrent y position
    TQ_INT32 y() const { return m_y; };

    /// Returns a pointer to the pixel data. Do NOT interpret the data - leave that to a colorstrategy
    TQ_UINT8 *rawData() const;

    /// Returns a pointer to the pixel data as it was at the moment tof he last memento creation.
    const TQ_UINT8 * oldRawData() const;
};

/**
 * The KisRectIterator class iterates through the pixels of a rect in a KisPaintDevice hiding the
 * tile structure
 */
class KRITACORE_EXPORT KisTiledRectIterator : public KisTiledIterator
{

public:
    /// do not call constructor directly use factory method in KisDataManager instead.
    KisTiledRectIterator( KisTiledDataManager *dm, TQ_INT32  x, TQ_INT32  y, TQ_INT32  w, TQ_INT32  h, bool writable);
    KisTiledRectIterator(const KisTiledRectIterator&);
    KisTiledRectIterator& operator=(const KisTiledRectIterator&);
    ~KisTiledRectIterator();

public:
    TQ_INT32 nConseqPixels() const;
    
    /// Advances a number of pixels until it reaches the end of the rect
    KisTiledRectIterator & operator+=(int n);
    
    /// Advances one pixel. Going to the beginning of the next line when it reaches the end of a line
    KisTiledRectIterator & operator++();

    /// Goes back one pixel. Going to the end of the line above when it reaches the beginning of a line
    //KisTiledRectIterator & operator--();

    /// returns true when the iterator has reached the end
    inline bool isDone() const { return m_beyondEnd; }


protected:
     TQ_INT32 m_left;
     TQ_INT32 m_top;
     TQ_INT32 m_w;
     TQ_INT32 m_h;
     TQ_INT32 m_topRow;
     TQ_INT32 m_bottomRow;
     TQ_INT32 m_leftCol;
     TQ_INT32 m_rightCol;
     TQ_INT32 m_xInTile;
     TQ_INT32 m_yInTile;
     TQ_INT32 m_leftInTile;
     TQ_INT32 m_rightInTile;
     TQ_INT32 m_topInTile;
     TQ_INT32 m_bottomInTile;
     bool m_beyondEnd;

private:
     void nextTile();
};

/**
 * The KisHLineIterator class iterates through the pixels of a horizontal line in a KisPaintDevice hiding the
 * tile structure
 */
class KRITACORE_EXPORT KisTiledHLineIterator : public KisTiledIterator
{

public:
    /// do not call constructor directly use factory method in KisDataManager instead.
    KisTiledHLineIterator( KisTiledDataManager *dm, TQ_INT32  x, TQ_INT32  y, TQ_INT32 w, bool writable);
    KisTiledHLineIterator(const KisTiledHLineIterator&);
    KisTiledHLineIterator& operator=(const KisTiledHLineIterator&);
    ~KisTiledHLineIterator();

public:
    /// Advances one pixel. Going to the beginning of the next line when it reaches the end of a line
    KisTiledHLineIterator & operator++();

    /// Returns the number of consequtive horizontal pixels that we point at
    /// This is useful for optimizing
    TQ_INT32 nConseqHPixels() const;

    /// Advances a number of pixels until it reaches the end of the line
    KisTiledHLineIterator & operator+=(int);

    /// Goes back one pixel. Going to the end of the line above when it reaches the beginning of a line
    KisTiledHLineIterator & operator--();

    /// returns true when the iterator has reached the end
    bool isDone() const { return m_x > m_right; }

    /// increment to the next row and rewind to the begining
    void nextRow();

protected:
     TQ_INT32 m_right;
     TQ_INT32 m_left;
     TQ_INT32 m_leftCol;
     TQ_INT32 m_rightCol;
     TQ_INT32 m_xInTile;
     TQ_INT32 m_yInTile;
     TQ_INT32 m_leftInTile;
     TQ_INT32 m_rightInTile;

private:
     void nextTile();
     void prevTile();
};

/**
 * The KisVLineIterator class iterates through the pixels of a vertical line in a KisPaintDevice hiding the
 * tile structure
 */
class KRITACORE_EXPORT KisTiledVLineIterator : public KisTiledIterator
{

public:
    /// do not call constructor directly use factory method in KisDataManager instead.
    KisTiledVLineIterator( KisTiledDataManager *dm, TQ_INT32  x, TQ_INT32 y, TQ_INT32 h, bool writable);
    KisTiledVLineIterator(const KisTiledVLineIterator&);
    KisTiledVLineIterator& operator=(const KisTiledVLineIterator&);
    ~KisTiledVLineIterator();

public:
    /// Advances one pixel. Going to the beginning of the next line when it reaches the end of a line
    KisTiledVLineIterator & operator++();

    /// Goes back one pixel. Going to the end of the line above when it reaches the beginning of a line
    //KisTiledVLineIterator & operator--();

    /// returns true when the iterator has reached the end
    bool isDone() const { return m_y > m_bottom; }

    /// increment to the next column and rewind to the begining
    void nextCol();

protected:
    TQ_INT32 m_top;
    TQ_INT32 m_bottom;
    TQ_INT32 m_topRow;
    TQ_INT32 m_bottomRow;
    TQ_INT32 m_xInTile;
    TQ_INT32 m_yInTile;
    TQ_INT32 m_topInTile;
    TQ_INT32 m_bottomInTile;

private:
     void nextTile();
};

#endif // KIS_TILED_ITERATOR_H_
