/*
======================================================================
qv.h

Ernie Wright  14 Apr 05
MSVC 4.0

Constants, data structures and function prototypes.
====================================================================== */

#ifndef QV_H
#define QV_H

#include <windows.h>

/* symbols shared with the resource script */

/* toolbar button bitmap */

#define IDB_TOOLBUTTONS   201

/* text dialog controls */

#define TIDC_TEXT      100

#define TIDC_FONT      101
#define TIDC_COLOR     102
#define TIDC_SIZE      103
#define TIDC_BOLD      104
#define TIDC_ITALIC    105

#define TIDC_LEFT      106
#define TIDC_CENTER    107
#define TIDC_RIGHT     108
#define TIDC_X         109
#define TIDC_Y         110

#define TIDC_DROP      111
#define TIDC_DX        112
#define TIDC_SPINDX    113
#define TIDC_DY        114
#define TIDC_SPINDY    115

#define TIDC_LABELY    800
#define TIDC_LABELDX   801
#define TIDC_LABELDY   802

#define TIDC_FONTGRP   900
#define TIDC_POSGRP    901
#define TIDC_DROPGRP   902

/* notes and settings dialog controls */

#define NIDC_NAME      100
#define NIDC_CREATED   200
#define NIDC_NUMBER    201
#define NIDC_RENDTIME  202
#define NIDC_NOTES     101
#define NIDC_SCNINFO   102

#define NIDC_BLACK     102
#define NIDC_WHITE     103
#define NIDC_FPIXEL    104
#define NIDC_LEVELALL  105
#define NIDC_LEVELDEF  106

#define NIDC_PEDESTAL  110
#define NIDC_COMLIMIT  111
#define NIDC_CHRLIMIT  112
#define NIDC_NTSC      113
#define NIDC_PAL       114
#define NIDC_HOTDEF    115
#define NIDC_HOTALL    116

#define NIDC_ASPECT    120
#define NIDC_MARGIN    121
#define NIDC_VIDEO     122
#define NIDC_BOX       123
#define NIDC_OUTLINE   124
#define NIDC_SOLID     125
#define NIDC_SAFEDEF   126
#define NIDC_SAFEALL   127

/* the resource compiler can ignore the rest */

#ifndef RC_INVOKED

#include <lwserver.h>
#include <lwio.h>
#include <lwrender.h>

typedef struct st_Image {
   int                  width;
   int                  height;
   int                  index;
   time_t               created;
   int                  rendertime;
   int                  framenum;
   LWSceneInfo          scene;
   float                black;
   float                white;
   float               *r;
   float               *g;
   float               *b;
   float               *a;
} Image;

typedef struct st_Bitmap1 {
   BITMAPINFOHEADER     dib;
   RGBQUAD              pal[ 2 ];
   unsigned char       *bits;
   int                  w, h;
   int                  rowbytes;
   int                  flags;
} Bitmap1;

typedef struct st_Bitmap8 {
   BITMAPINFOHEADER     dib;
   RGBQUAD              pal[ 256 ];
   unsigned char       *bits;
   int                  w, h;
   int                  rowbytes;
   int                  flags;
} Bitmap8;

typedef struct st_Bitmap24 {
   BITMAPINFOHEADER     dib;
   unsigned char       *bits;
   int                  w, h;
   int                  rowbytes;
   int                  flags;
} Bitmap24;

typedef struct st_HotInfo {
   int                  encoding;
   float                pedestal;
   float                limComposite;
   float                limChroma;
} HotInfo;

#define HOT_NTSC 0
#define HOT_PAL  1

typedef struct {
   float                aspect;
   float                margin;
   int                  types;
   int                  style;
} SafeInfo;

#define SAFE_VIDEO 1
#define SAFE_BOX   2
#define SAFE_OUTLINE 0
#define SAFE_SOLID   1

typedef struct st_QVText {
   LOGFONT              lf;
   COLORREF             color;
   int                  x, y;
   int                  lcr, lx;
   char                 text[ 256 ];
   int                  dropshadow;
   int                  dx, dy;
} QVText;

#define LCR_LEFT   0
#define LCR_CENTER 1
#define LCR_RIGHT  2

typedef struct st_UndoText {
   Image               *image;
   int                  x, y;
} UndoText;

typedef struct st_QVConfig {
   char                 filename[ 260 ];
   QVText               text;
   HotInfo              hot;
   SafeInfo             safe;
   float                black;
   float                white;
   int                  pixelDisplayType;
   int                  closeRS;
} QVConfig;

#define SHOWPEL_BYTE   0
#define SHOWPEL_FLOAT  1

typedef struct st_QVInstance {
   Image               *image;
   int                  width;
   int                  height;
   int                  y;
   int                  index;
   int                  starttime;
   float                frametime;
   uintptr_t						thread;
} QVInstance;

typedef struct st_Displayable {
   Image               *image;
   Bitmap24            *rgb;
   Bitmap8             *alpha;
   Bitmap1             *hot;
   Bitmap1             *safe;
   char                 title[ 256 ];
   char                 notes[ 1024 ];
   HotInfo              hotInfo;
   SafeInfo             safeInfo;
   UndoText             undo;
} Displayable;


/* The ImageInfo structure is used internally to describe an image.
   It's attached to the draw window using SetProp(), referenced by the
   IMAGEINFO_POINTER string. */

#define IMAGEINFO_POINTER "QVImageInfoPointer"

typedef struct st_ImageInfo {
   Displayable         *disp;          // image bitmaps
   int                  width;         // pixel width of the image
   int                  height;        // pixel height of the image
   int                  channel;       // image channel to display
   int                  mask;          // hot and safe overlay flags
   char *               caption;       // typically the image name
   POINTS               curScroll;     // current scroll amount, in pixels
   POINTS               maxScroll;     // maximum scroll for the current zoom
   float                zoom;          // image scale factor
   int                  izoom;         // the scale factor as an index
   POINTS               mouse;         // mouse position
   BOOL                 dragging;      // currently dragging
   BOOL                 addingtext;    // displaying I-beam, waiting for click
} ImageInfo;

#define CH_IMAGE  0
#define CH_ALPHA  1
#define CH_RED    2
#define CH_GREEN  3
#define CH_BLUE   4

#define MASK_HOT  1
#define MASK_SAFE 2

/* The zoom index is incremented and decremented when the zoom is
   changed.  The ZOOMI2F() macro maps indexes to scale factors.  An
   index of 64 corresponds to a scale of 1.0.  Smaller indexes are for
   zooms < 1.0, and larger for zooms > 1.0.

   The zoom steps were chosen so that an integral number of screen
   pixels are used for each source pixel (zoom > 1.0), or an integral
   number are omitted from the source (zoom < 1.0).  This looks better
   than arbitrary zooms with most display device drivers, since we're
   just calling StretchDIBits().  Doing our own filtered scaling would
   be better, but is expensive. */

#define IZOOM_MIN    0
#define IZOOM_ONE    64
#define IZOOM_MAX    128
#define ZOOMI2F( iz )  ( iz < IZOOM_ONE ? \
                       1.0f / ( float )( IZOOM_ONE - iz + 1 ) : \
                       ( float )( iz - IZOOM_ONE + 1 ))

/* QV sends new images to the main window by sending this message.
   The LPARAM is a pointer to an Image. */

#define UM_ADDIMAGE (WM_USER + 1)

/* The draw window is the canvas on which the image is actually
   rendered.  It's a child window of the main viewer window. */

#define IDW_DRAWWND  2
#define IDW_STATWND  3

/* toolbar and button IDs */

#define IDC_TOOLBAR  20

#define IDC_SAVE     101
#define IDC_COPY     102
#define IDC_PRINT    103
#define IDC_DELETE   104
#define IDC_IMAGE    105
#define IDC_ALPHA    106
#define IDC_RED      107
#define IDC_GREEN    108
#define IDC_BLUE     109
#define IDC_HOT      110
#define IDC_SAFE     111
#define IDC_FIT      112
#define IDC_NOTES    113
#define IDC_TEXT     114
#define IDC_UNDO     115

#define IDC_RADIO    200

/* margin and window item sizes used for window size and position
   calculations */

#define RADIO_L      4
#define RADIO_W      21
#define RADIO_H      16
#define TOOLBAR_L    8
#define DRAWWND_L    4
#define DRAWWND_R    4
#define DRAWWND_T    0
#define DRAWWND_B    4

/* reasonable minimums for the main window dimensions */

#define MIN_MAINWND_X 320
#define MIN_MAINWND_Y 240

/* floating-point versions of POINT and RECT */

typedef struct st_FPoint { float x, y; } FPoint;
typedef struct st_FRect { float left, top, right, bottom; } FRect;

/* argument to make_textimage() */

typedef struct {
   int            width;
   int            height;
   int            depth;
   int            rowbytes;
   int            ascent;
   unsigned char *bits;
} QVTextImage;

/* FileRequest() return type */

typedef struct {
   char    *filename;
   char    *title;
   int      type;
} QVFileReq;

/* ----- config load and save */

int  initConfigIO( GlobalFunc *global );
void loadQVConfig( QVConfig *config, const LWLoadState *ls );
void loadQVConfigFile( HINSTANCE hinst, QVConfig *config );
void saveQVConfig( QVConfig *config, const LWSaveState *ss );
void saveQVConfigFile( QVConfig *config );

/* ----- QV logo */

void aboutDialog( HINSTANCE hinst, HWND hwnd, int timeout );

/* ----- main window */

void imvInit( HINSTANCE hInstance );
int  imvInitLW( GlobalFunc *global );
void imvCleanup( void );
QVConfig *imvGetConfig( void );
HWND imvCreateWindow( HWND hwndParent, Image *image );
void imvBusyCursor( HWND hwnd, int busy );
void set_cursor( HWND hwnd, const char *shape );

/* ----- image child window */

void imvUpdateImage( HWND hwnd );
void imvUpdateZoom( HWND hwnd, POINTS *pmouse, UINT message );
void imvFitWindow( HWND hwnd );

/* ----- file dialog, config file finder */

int  initFileReq( GlobalFunc *global );
QVFileReq *FileRequest( HWND hwnd, char *iname );
int  findConfigFile( HINSTANCE hinst, char *cfgname, char *buf, int bufsize );

/* ----- copy a Windows DIB to the clipboard */

int  copyBitmap( HWND hwnd, BITMAPINFOHEADER *dib, unsigned char *bits,
        int rowbytes );

/* ----- print a Windows DIB */

void printCleanup( void );
void printBitmap( HWND hwnd, BITMAPINFOHEADER *dib, unsigned char *bits );

/* ----- QV's internal IFF ILBM saver */

int saveIFF( char *filename, char *buf, int w, int h, int span,
   int nchannels );

/* ----- image I/O */

int  initSave( GlobalFunc *global );
int  saverFormatCount( void );
const char *saverFormatName( int format );
int  saveRGBA( Displayable *disp, char *filename, int format );
int  saveAlpha( Displayable *disp, char *filename, int format );
int  saveImage( HWND hwnd );
int  copyImage( HWND hwnd );
void printImage( HWND hwnd );

/* ----- toolbar functions */

HWND toolbarCreate( HWND hwnd );
int  toolbarResize( HWND hwnd );
void toolbarSetChannel( HWND hwnd, int channel );
void toolbarSetMask( HWND hwnd, int mask );
void toolbarSet( HWND hwnd, int channel, int mask );
void toolbarSetText( HWND hwnd, int state );
int  toolbarTipText( LPARAM lparam );
int  toolbarHeight( HWND hwnd );

/* ----- status bar */

HWND statusCreate( HWND hwnd );
void statusFree( void );
int  statusResize( int parent_w, int parent_h );
int  statusHeight( void );
void statusCaption( char *caption );
void statusZoom( int izoom );
void setPixelDisplayType( int type );
void statusPixel( Displayable *disp, int x, int y, int channel );
void init_progress( int atotal );
void show_progress( int acurrent );
void image_progress( int y );

/* ----- radio buttons, used to select images */

void radioCreate( HWND hwnd );
int  radioResize( HWND hwnd, int w, int h, int sh );
void radioSelect( HWND hwnd, int i );
void radioEnable( HWND hwnd, int i, int state );
int  radioHeight( void );

/* ----- image storage and user interface synchronization */

void frameSyncInfo( int i );
void frameSyncUI( HWND hwnd, int current, int redraw );
int  frameInit( Image *image, QVConfig *config, int i );
void frameClear( int i );
void frameClearAll( void );
int  frameIsEmpty( int i );
int  frameOldest( void );
int  frameNewest( void );
int  frameNextAvailable( void );
ImageInfo *frameGetInfo( void );
Displayable *frameGetDisplayable( int i );
void frameClone( Displayable *disp, int parts );

/* ----- calculating hot pixels */

void init_hot( int encoding, double ped, double com, double chr );
int  hot( int r, int g, int b );

/* ----- text */

INT_PTR textDialog( HWND hwnd, QVText *t );
void addText( HWND hwnd, int x, int y, int draw );
int  compositeText( QVText *text, Displayable *disp );
void undoText( Displayable *disp );
int  make_textimage( LOGFONT *lf, char *text, QVTextImage *dst );

/* ----- notes and settings */

int propertiesDialog( HWND hwnd, QVConfig *config, Displayable *disp );
int floatChanged( float old, float new, int threshold );

/* ----- creating and initializing images */

Image *createImage( int width, int height, int index, int rendertime );
Bitmap1 *createBitmap1( int width, int height );
Bitmap8 *createBitmap8( int width, int height );
Bitmap24 *createBitmap24( int width, int height );

void freeImage( Image *image );
void freeBitmap1( Bitmap1 *bitmap );
void freeBitmap8( Bitmap8 *bitmap );
void freeBitmap24( Bitmap24 *bitmap );

void writeImageRow( Image *image, int y,
        const float *r, const float *g, const float *b, const float *a );
Image *copyImageRect( Image *source, int x0, int y0, int w, int h );
void pasteImageRect( Image *source, Image *dest, int x0, int y0 );

Displayable *freeDisplayable( Displayable *disp );
Displayable *createDisplayable( Image *image );
void realizeDisplayable( Displayable *disp );
void realizeDisplayableRect( Displayable *disp, int x0, int y0, int w, int h );
void realizeDisplayableParts( Displayable *disp, int parts );

#define CHANGED_LEVELS 1   /* realizeDisplayableParts() bits */
#define CHANGED_HOT    2
#define CHANGED_SAFE   4

void drawRGB( Image *image, Bitmap24 *bitmap );
void drawAlpha( Image *image, Bitmap8 *bitmap );
void drawRGBARect( Displayable *disp, int x0, int y0, int w, int h );
void drawSafe( Displayable *disp );
void drawHot( Displayable *disp );
void drawHotRect( Displayable *disp, int x0, int y0, int w, int h );

void getPixelByte( Displayable *disp, int x, int y, int channel,
   unsigned char pixel[] );
void getPixelFloat( Displayable *disp, int x, int y, int channel,
   float pixel[] );


#endif      /* ifndef RC_INVOKED */
#endif      /* ifndef QV_H   */
