/*
*Copyright(c)2016, Jeffrey Lee
*Allrightsreserved.
*
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met: 
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef ZRLE_H
#define ZRLE_H

#include <stdbool.h>

#include "vncserv.h"

/* zlib/ZRLE encoding handling */

#define ZRLE_TILE 64
#define ZRLE_TILE_SHIFT 6

typedef struct {
  uint32_t pixel;
  uint32_t count;
} zrle_rle_ent;

#define ZRLE_TILE_SIZE (ZRLE_TILE*ZRLE_TILE*4 + 4096) /* Assumed worst-case size for a ZRLE tile. Must also be large enough for worst-case input. */

#define ZRLE_RLE_SIZE (sizeof(zrle_rle_ent)*ZRLE_TILE*ZRLE_TILE) /* Memory needed for a worst-case RLE list */

#define ZRLE_BUFFER_SIZE (ZRLE_RLE_SIZE) /* Bigger of the two */

typedef struct {
  uint8_t buf[2][ZRLE_BUFFER_SIZE];
} zrle_temp_buffer;

#define ZRLE_MAX_PACKET (ZRLE_TILE_SIZE*4) /* zrle_send tries to do 4 tiles at once */

extern zrle_state *zrle_init(int level); /* TODO: More parameters */
extern void zrle_destroy(zrle_state *state);

/* Return number of rectangles needed */
extern int zrle_num_rects(vncserv *serv, const area *rect);

/* Returns true if progress has been made */
extern bool zrle_send(vncserv *serv, send_rect_state *state, zrle_temp_buffer *temp);

extern bool zlib_send(vncserv *serv, send_rect_state *state, zrle_temp_buffer *temp);

#endif
