#include "Lang.h"
#include "tkPort.h"
#include "tkInt.h"
#include "tclMath.h"
#include <ctype.h>
#ifdef __WIN32__
#include "tkWinInt.h"
#endif
extern
int
_XInitImageFuncPtrs _ANSI_ARGS_((XImage *image));
#include "tkImgPhoto.h"
#include <math.h>
#include <ctype.h>
#if defined(__STDC__) || defined(ANSI_SIGNED_CHAR) || defined(_AIX)
typedef
signed
char
schar;
#else
# ifndef __CHAR_UNSIGNED__
typedef
char
schar;
# else
typedef
short
schar;
# endif
#endif
typedef
unsigned
int
pixel;
#define MAX_PIXELS 65536
typedef
struct
{
Display *display;
Colormap colormap;
double
gamma;
Tk_Uid palette;
} ColorTableId;
typedef
struct
ColorTable {
ColorTableId id;
int
flags;
int
refCount;
int
liveRefCount;
int
numColors;
XVisualInfo visualInfo;
pixel redValues[256];
pixel greenValues[256];
pixel blueValues[256];
unsigned
long
*pixelMap;
unsigned
char
colorQuant[3][256];
} ColorTable;
#ifdef COLOR_WINDOW
#undef COLOR_WINDOW
#endif
#define BLACK_AND_WHITE 1
#define COLOR_WINDOW 2
#define DISPOSE_PENDING 4
#define MAP_COLORS 8
typedef
struct
PhotoMaster {
Tk_ImageMaster tkMaster;
Tcl_Interp *interp;
Tcl_Command imageCmd;
int
flags;
int
width, height;
int
userWidth, userHeight;
Tk_Uid palette;
double
gamma;
Tcl_Obj *fileString;
Tcl_Obj *dataString;
Tcl_Obj *format;
unsigned
char
*pix32;
int
ditherX, ditherY;
TkRegion validRegion;
struct
PhotoInstance *instancePtr;
} PhotoMaster;
#define COLOR_IMAGE 1
#define IMAGE_CHANGED 2
#define COMPLEX_ALPHA 4
typedef
struct
PhotoInstance {
PhotoMaster *masterPtr;
Display *display;
Colormap colormap;
struct
PhotoInstance *nextPtr;
int
refCount;
Tk_Uid palette;
double
gamma;
Tk_Uid defaultPalette;
ColorTable *colorTablePtr;
Pixmap pixels;
int
width, height;
schar *error;
XImage *imagePtr;
XVisualInfo visualInfo;
GC gc;
} PhotoInstance;
struct
SubcommandOptions {
int
options;
Tcl_Obj *name;
int
fromX, fromY;
int
fromX2, fromY2;
int
toX, toY;
int
toX2, toY2;
int
zoomX, zoomY;
int
subsampleX, subsampleY;
Tcl_Obj *format;
XColor *background;
int
compositingRule;
};
#define OPT_BACKGROUND 1
#define OPT_COMPOSITE 2
#define OPT_FORMAT 4
#define OPT_FROM 8
#define OPT_GRAYSCALE 0x10
#define OPT_SHRINK 0x20
#define OPT_SUBSAMPLE 0x40
#define OPT_TO 0x80
#define OPT_ZOOM 0x100
static
char
*optionNames[] = {
"-background"
,
"-compositingrule"
,
"-format"
,
"-from"
,
"-grayscale"
,
"-shrink"
,
"-subsample"
,
"-to"
,
"-zoom"
,
(
char
*) NULL
};
#define TK_PHOTO_ALLOC_FAILURE_MESSAGE \
"not enough free memory for image buffer"
static
int
ImgPhotoCreate _ANSI_ARGS_((Tcl_Interp *interp,
char
*name,
int
objc, Tcl_Obj *CONST objv[],
Tk_ImageType *typePtr, Tk_ImageMaster master,
ClientData *clientDataPtr));
static
ClientData ImgPhotoGet _ANSI_ARGS_((Tk_Window tkwin,
ClientData clientData));
static
void
ImgPhotoDisplay _ANSI_ARGS_((ClientData clientData,
Display *display, Drawable drawable,
int
imageX,
int
imageY,
int
width,
int
height,
int
drawableX,
int
drawableY));
static
void
ImgPhotoFree _ANSI_ARGS_((ClientData clientData,
Display *display));
static
void
ImgPhotoDelete _ANSI_ARGS_((ClientData clientData));
static
int
ImgPhotoPostscript _ANSI_ARGS_((ClientData clientData,
Tcl_Interp *interp, Tk_Window tkwin,
Tk_PostscriptInfo psInfo,
int
x,
int
y,
int
width,
int
height,
int
prepass));
Tk_ImageType tkPhotoImageType = {
"photo"
,
ImgPhotoCreate,
ImgPhotoGet,
ImgPhotoDisplay,
ImgPhotoFree,
ImgPhotoDelete,
ImgPhotoPostscript,
(Tk_ImageType *) NULL
};
typedef
struct
ThreadSpecificData {
Tk_PhotoImageFormat *formatList;
Tk_PhotoImageFormat *oldFormatList;
int
initialized;
} ThreadSpecificData;
static
Tcl_ThreadDataKey dataKey;
#define DEF_PHOTO_GAMMA "1"
#define DEF_PHOTO_HEIGHT "0"
#define DEF_PHOTO_PALETTE ""
#define DEF_PHOTO_WIDTH "0"
static
Tk_ConfigSpec configSpecs[] = {
{TK_CONFIG_LANGARG,
"-data"
, (
char
*) NULL, (
char
*) NULL,
(
char
*) NULL, Tk_Offset(PhotoMaster, dataString), TK_CONFIG_NULL_OK},
{TK_CONFIG_LANGARG,
"-format"
, (
char
*) NULL, (
char
*) NULL,
(
char
*) NULL, Tk_Offset(PhotoMaster, format), TK_CONFIG_NULL_OK},
{TK_CONFIG_LANGARG,
"-file"
, (
char
*) NULL, (
char
*) NULL,
(
char
*) NULL, Tk_Offset(PhotoMaster, fileString), TK_CONFIG_NULL_OK},
{TK_CONFIG_DOUBLE,
"-gamma"
,
"gamma"
,
"Gamma"
,
DEF_PHOTO_GAMMA, Tk_Offset(PhotoMaster, gamma), 0},
{TK_CONFIG_INT,
"-height"
, (
char
*) NULL, (
char
*) NULL,
DEF_PHOTO_HEIGHT, Tk_Offset(PhotoMaster, userHeight), 0},
{TK_CONFIG_UID,
"-palette"
,
"palette"
,
"Palette"
,
DEF_PHOTO_PALETTE, Tk_Offset(PhotoMaster, palette), 0},
{TK_CONFIG_INT,
"-width"
, (
char
*) NULL, (
char
*) NULL,
DEF_PHOTO_WIDTH, Tk_Offset(PhotoMaster, userWidth), 0},
{TK_CONFIG_END, (
char
*) NULL, (
char
*) NULL, (
char
*) NULL,
(
char
*) NULL, 0, 0}
};
static
Tcl_HashTable imgPhotoColorHash;
static
int
imgPhotoColorHashInitialized;
#define N_COLOR_HASH (sizeof(ColorTableId) / sizeof(int))
static
void
PhotoFormatThreadExitProc _ANSI_ARGS_((
ClientData clientData));
static
int
ImgPhotoCmd _ANSI_ARGS_((ClientData clientData,
Tcl_Interp *interp,
int
objc, Tcl_Obj *CONST objv[]));
static
int
ParseSubcommandOptions _ANSI_ARGS_((
struct
SubcommandOptions *optPtr,
Tcl_Interp *interp,
int
allowedOptions,
int
*indexPtr,
int
objc, Tcl_Obj *CONST objv[]));
static
void
ImgPhotoCmdDeletedProc _ANSI_ARGS_((
ClientData clientData));
static
int
ImgPhotoConfigureMaster _ANSI_ARGS_((
Tcl_Interp *interp, PhotoMaster *masterPtr,
int
objc, Tcl_Obj *CONST objv[],
int
flags));
static
void
ImgPhotoConfigureInstance _ANSI_ARGS_((
PhotoInstance *instancePtr));
static
int
ToggleComplexAlphaIfNeeded _ANSI_ARGS_((
PhotoMaster *mPtr));
static
void
ImgPhotoBlendComplexAlpha _ANSI_ARGS_((
XImage *bgImg, PhotoInstance *iPtr,
int
xOffset,
int
yOffset,
int
width,
int
height));
static
int
ImgPhotoSetSize _ANSI_ARGS_((PhotoMaster *masterPtr,
int
width,
int
height));
static
void
ImgPhotoInstanceSetSize _ANSI_ARGS_((
PhotoInstance *instancePtr));
static
int
ImgStringWrite _ANSI_ARGS_((Tcl_Interp *interp,
Tcl_Obj *formatString,
Tk_PhotoImageBlock *blockPtr));
static
char
* ImgGetPhoto _ANSI_ARGS_((PhotoMaster *masterPtr,
Tk_PhotoImageBlock *blockPtr,
struct
SubcommandOptions *optPtr));
static
int
IsValidPalette _ANSI_ARGS_((PhotoInstance *instancePtr,
CONST
char
*palette));
static
int
CountBits _ANSI_ARGS_((pixel mask));
static
void
GetColorTable _ANSI_ARGS_((PhotoInstance *instancePtr));
static
void
FreeColorTable _ANSI_ARGS_((ColorTable *colorPtr,
int
force));
static
void
AllocateColors _ANSI_ARGS_((ColorTable *colorPtr));
static
void
DisposeColorTable _ANSI_ARGS_((ClientData clientData));
static
void
DisposeInstance _ANSI_ARGS_((ClientData clientData));
static
int
ReclaimColors _ANSI_ARGS_((ColorTableId *id,
int
numColors));
static
int
MatchFileFormat _ANSI_ARGS_((Tcl_Interp *interp,
Tcl_Channel chan, Tcl_Obj *fileName, Tcl_Obj *formatString,
Tk_PhotoImageFormat **imageFormatPtr,
int
*widthPtr,
int
*heightPtr,
int
*oldformat));
static
int
MatchStringFormat _ANSI_ARGS_((Tcl_Interp *interp,
Tcl_Obj *data, Tcl_Obj *formatString,
Tk_PhotoImageFormat **imageFormatPtr,
int
*widthPtr,
int
*heightPtr,
int
*oldformat));
static
Tcl_ObjCmdProc * PhotoOptionFind _ANSI_ARGS_((Tcl_Interp * interp,
Tcl_Obj *obj));
static
void
DitherInstance _ANSI_ARGS_((PhotoInstance *instancePtr,
int
x,
int
y,
int
width,
int
height));
static
void
PhotoOptionCleanupProc _ANSI_ARGS_((
ClientData clientData, Tcl_Interp *interp));
#undef MIN
#define MIN(a, b) ((a) < (b)? (a): (b))
#undef MAX
#define MAX(a, b) ((a) > (b)? (a): (b))
static
void
PhotoFormatThreadExitProc(clientData)
ClientData clientData;
{
Tk_PhotoImageFormat *freePtr;
ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
Tcl_GetThreadData(&dataKey,
sizeof
(ThreadSpecificData));
while
(tsdPtr->oldFormatList != NULL) {
freePtr = tsdPtr->oldFormatList;
tsdPtr->oldFormatList = tsdPtr->oldFormatList->nextPtr;
ckfree((
char
*) freePtr->name);
ckfree((
char
*) freePtr);
}
while
(tsdPtr->formatList != NULL) {
freePtr = tsdPtr->formatList;
tsdPtr->formatList = tsdPtr->formatList->nextPtr;
ckfree((
char
*) freePtr->name);
ckfree((
char
*) freePtr);
}
}
void
Tk_CreateOldPhotoImageFormat(formatPtr)
Tk_PhotoImageFormat *formatPtr;
{
Tk_PhotoImageFormat *copyPtr;
ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
Tcl_GetThreadData(&dataKey,
sizeof
(ThreadSpecificData));
if
(!tsdPtr->initialized) {
tsdPtr->initialized = 1;
Tcl_CreateThreadExitHandler(PhotoFormatThreadExitProc, NULL);
}
copyPtr = (Tk_PhotoImageFormat *) ckalloc(
sizeof
(Tk_PhotoImageFormat));
*copyPtr = *formatPtr;
copyPtr->name = (
char
*) ckalloc((unsigned) (
strlen
(formatPtr->name) + 1));
strcpy
(copyPtr->name, formatPtr->name);
copyPtr->nextPtr = tsdPtr->oldFormatList;
tsdPtr->oldFormatList = copyPtr;
}
void
Tk_CreatePhotoImageFormat(formatPtr)
Tk_PhotoImageFormat *formatPtr;
{
Tk_PhotoImageFormat *copyPtr;
ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
Tcl_GetThreadData(&dataKey,
sizeof
(ThreadSpecificData));
if
(!tsdPtr->initialized) {
tsdPtr->initialized = 1;
Tcl_CreateThreadExitHandler(PhotoFormatThreadExitProc, NULL);
}
copyPtr = (Tk_PhotoImageFormat *) ckalloc(
sizeof
(Tk_PhotoImageFormat));
*copyPtr = *formatPtr;
copyPtr->name = (
char
*) ckalloc((unsigned) (
strlen
(formatPtr->name) + 1));
strcpy
(copyPtr->name, formatPtr->name);
if
(0 &&
isupper
((unsigned
char
) *formatPtr->name)) {
copyPtr->nextPtr = tsdPtr->oldFormatList;
tsdPtr->oldFormatList = copyPtr;
}
else
{
copyPtr->nextPtr = tsdPtr->formatList;
tsdPtr->formatList = copyPtr;
}
}
static
int
ImgPhotoCreate(interp, name, objc, objv, typePtr, master, clientDataPtr)
Tcl_Interp *interp;
char
*name;
int
objc;
Tcl_Obj *CONST objv[];
Tk_ImageType *typePtr;
Tk_ImageMaster master;
ClientData *clientDataPtr;
{
PhotoMaster *masterPtr;
masterPtr = (PhotoMaster *) ckalloc(
sizeof
(PhotoMaster));
memset
((
void
*) masterPtr, 0,
sizeof
(PhotoMaster));
masterPtr->tkMaster = master;
masterPtr->interp = interp;
masterPtr->imageCmd = Lang_CreateImage(interp, name, ImgPhotoCmd,
(ClientData) masterPtr, ImgPhotoCmdDeletedProc, typePtr);
masterPtr->palette = NULL;
masterPtr->pix32 = NULL;
masterPtr->instancePtr = NULL;
masterPtr->validRegion = TkCreateRegion();
if
(ImgPhotoConfigureMaster(interp, masterPtr, objc, objv, 0) != TCL_OK) {
ImgPhotoDelete((ClientData) masterPtr);
return
TCL_ERROR;
}
*clientDataPtr = (ClientData) masterPtr;
return
TCL_OK;
}
static
int
ImgPhotoCmd(clientData, interp, objc, objv)
ClientData clientData;
Tcl_Interp *interp;
int
objc;
Tcl_Obj *CONST objv[];
{
int
oldformat = 0;
static
CONST
char
*photoOptions[] = {
"blank"
,
"cget"
,
"configure"
,
"copy"
,
"data"
,
"get"
,
"put"
,
"read"
,
"redither"
,
"transparency"
,
"write"
,
"formats"
, (
char
*) NULL
};
enum
options {
PHOTO_BLANK, PHOTO_CGET, PHOTO_CONFIGURE, PHOTO_COPY, PHOTO_DATA,
PHOTO_GET, PHOTO_PUT, PHOTO_READ, PHOTO_REDITHER, PHOTO_TRANS,
PHOTO_WRITE, PHOTO_FORMATS
};
PhotoMaster *masterPtr = (PhotoMaster *) clientData;
int
result, index;
int
x, y, width, height;
int
dataWidth = 0, dataHeight;
struct
SubcommandOptions options;
int
listArgc;
Tcl_Obj * *listArgv;
Tcl_Obj * *srcArgv;
unsigned
char
*pixelPtr = NULL;
Tk_PhotoImageBlock block;
Tk_Window tkwin;
XColor color;
Tk_PhotoImageFormat *imageFormat;
int
imageWidth, imageHeight;
int
matched;
Tcl_Channel chan;
Tk_PhotoHandle srcHandle;
size_t
length;
Tcl_Obj *obj;
int
c;
ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
Tcl_GetThreadData(&dataKey,
sizeof
(ThreadSpecificData));
if
(objc < 2) {
Tcl_WrongNumArgs(interp, 1, objv,
"option ?arg arg ...?"
);
return
TCL_ERROR;
}
if
(Tcl_GetIndexFromObj(interp, objv[1], photoOptions,
"option"
, 0,
&index) != TCL_OK) {
Tcl_ObjCmdProc *proc;
proc = PhotoOptionFind(interp, objv[1]);
if
(proc == (Tcl_ObjCmdProc *) NULL) {
return
TCL_ERROR;
}
return
proc(clientData, interp, objc, objv);
}
switch
((
enum
options) index) {
case
PHOTO_BLANK:
if
(objc == 2) {
Tk_PhotoBlank(masterPtr);
return
TCL_OK;
}
else
{
Tcl_WrongNumArgs(interp, 2, objv, (
char
*) NULL);
return
TCL_ERROR;
}
case
PHOTO_CGET: {
char
*arg;
if
(objc != 3) {
Tcl_WrongNumArgs(interp, 2, objv,
"option"
);
return
TCL_ERROR;
}
arg = Tcl_GetStringFromObj(objv[2], (
int
*) &length);
if
(
strncmp
(arg,
"-data"
, length) == 0) {
if
(masterPtr->dataString) {
Tcl_SetObjResult(interp, masterPtr->dataString);
}
}
else
if
(
strncmp
(arg,
"-format"
, length) == 0) {
if
(masterPtr->format) {
Tcl_SetObjResult(interp, masterPtr->format);
}
}
else
{
Tk_ConfigureValue(interp, Tk_MainWindow(interp), configSpecs,
(
char
*) masterPtr, Tcl_GetString(objv[2]), 0);
}
return
TCL_OK;
}
case
PHOTO_CONFIGURE:
if
(objc == 2) {
Tcl_Obj *obj, *subobj;
result = Tk_ConfigureInfo(interp, Tk_MainWindow(interp),
configSpecs, (
char
*) masterPtr, (
char
*) NULL, 0);
if
(result != TCL_OK) {
return
result;
}
subobj = Tcl_NewStringObj(
"-data {} {} {}"
, 14);
if
(masterPtr->dataString) {
Tcl_ListObjAppendElement(interp, subobj, masterPtr->dataString);
}
else
{
Tcl_ListObjAppendElement(interp, subobj, Tcl_NewStringObj(
""
,0));
}
Tcl_ListObjAppendElement(interp, Tcl_GetObjResult(interp), subobj);
subobj = Tcl_NewStringObj(
"-format {} {} {}"
, 16);
if
(masterPtr->format) {
Tcl_ListObjAppendElement(interp, subobj, masterPtr->format);
}
else
{
Tcl_ListObjAppendElement(interp, subobj, Tcl_NewStringObj(
""
,0));
}
Tcl_ListObjAppendElement(interp, Tcl_GetObjResult(interp), subobj);
return
TCL_OK;
}
if
(objc == 3) {
char
*arg = Tcl_GetStringFromObj(objv[2], (
int
*) &length);
if
(!
strncmp
(arg,
"-data"
, length)) {
Tcl_Obj *subobj = Tcl_NewStringObj(
"-data {} {} {}"
, 14);
if
(masterPtr->dataString) {
Tcl_ListObjAppendElement(interp, subobj, masterPtr->dataString);
}
else
{
Tcl_ListObjAppendElement(interp, subobj, Tcl_NewStringObj(
""
,0));
}
Tcl_SetObjResult(interp,subobj);
return
TCL_OK;
}
else
if
(!
strncmp
(arg,
"-format"
, length)) {
Tcl_Obj *subobj = Tcl_NewStringObj(
"-format {} {} {}"
, 16);
if
(masterPtr->format) {
Tcl_ListObjAppendElement(interp, subobj, masterPtr->format);
}
else
{
Tcl_ListObjAppendElement(interp, subobj, Tcl_NewStringObj(
""
,0));
}
Tcl_SetObjResult(interp,subobj);
return
TCL_OK;
}
else
{
return
Tk_ConfigureInfo(interp, Tk_MainWindow(interp),
configSpecs, (
char
*) masterPtr, arg, 0);
}
}
return
ImgPhotoConfigureMaster(interp, masterPtr, objc-2, objv+2,
TK_CONFIG_ARGV_ONLY);
case
PHOTO_COPY:
index = 2;
memset
((
VOID
*) &options, 0,
sizeof
(options));
options.zoomX = options.zoomY = 1;
options.subsampleX = options.subsampleY = 1;
options.name = NULL;
options.compositingRule = TK_PHOTO_COMPOSITE_OVERLAY;
if
(ParseSubcommandOptions(&options, interp,
OPT_FROM | OPT_TO | OPT_ZOOM | OPT_SUBSAMPLE | OPT_SHRINK |
OPT_COMPOSITE, &index, objc, objv) != TCL_OK) {
return
TCL_ERROR;
}
if
(options.name == NULL || index < objc) {
Tcl_WrongNumArgs(interp, 2, objv,
"source-image ?-compositingrule rule? ?-from x1 y1 x2 y2? ?-to x1 y1 x2 y2? ?-zoom x y? ?-subsample x y?"
);
return
TCL_ERROR;
}
srcHandle = Tk_FindPhoto(interp, Tcl_GetString(options.name));
if
(srcHandle == NULL) {
Tcl_AppendResult(interp,
"image \""
,
Tcl_GetString(options.name),
"\" doesn't"
,
" exist or is not a photo image"
, (
char
*) NULL);
return
TCL_ERROR;
}
Tk_PhotoGetImage(srcHandle, &block);
if
((options.fromX2 > block.width) || (options.fromY2 > block.height)
|| (options.fromX2 > block.width)
|| (options.fromY2 > block.height)) {
Tcl_AppendResult(interp,
"coordinates for -from option extend "
,
"outside source image"
, (
char
*) NULL);
return
TCL_ERROR;
}
if
(!(options.options & OPT_FROM) || (options.fromX2 < 0)) {
options.fromX2 = block.width;
options.fromY2 = block.height;
}
if
(!(options.options & OPT_TO) || (options.toX2 < 0)) {
width = options.fromX2 - options.fromX;
if
(options.subsampleX > 0) {
width = (width + options.subsampleX - 1) / options.subsampleX;
}
else
if
(options.subsampleX == 0) {
width = 0;
}
else
{
width = (width - options.subsampleX - 1) / -options.subsampleX;
}
options.toX2 = options.toX + width * options.zoomX;
height = options.fromY2 - options.fromY;
if
(options.subsampleY > 0) {
height = (height + options.subsampleY - 1)
/ options.subsampleY;
}
else
if
(options.subsampleY == 0) {
height = 0;
}
else
{
height = (height - options.subsampleY - 1)
/ -options.subsampleY;
}
options.toY2 = options.toY + height * options.zoomY;
}
if
(options.options & OPT_SHRINK) {
if
(ImgPhotoSetSize(masterPtr, options.toX2,
options.toY2) != TCL_OK) {
Tcl_ResetResult(interp);
Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
TK_PHOTO_ALLOC_FAILURE_MESSAGE, (
char
*) NULL);
return
TCL_ERROR;
}
}
block.pixelPtr += options.fromX * block.pixelSize
+ options.fromY * block.pitch;
block.width = options.fromX2 - options.fromX;
block.height = options.fromY2 - options.fromY;
Tk_PhotoPutZoomedBlock((Tk_PhotoHandle) masterPtr, &block,
options.toX, options.toY, options.toX2 - options.toX,
options.toY2 - options.toY, options.zoomX, options.zoomY,
options.subsampleX, options.subsampleY,
options.compositingRule);
return
TCL_OK;
case
PHOTO_DATA: {
char
*data;
Tk_ImageStringWriteProc *stringWriteProc = NULL;
index = 2;
memset
((
VOID
*) &options, 0,
sizeof
(options));
options.name = NULL;
options.format = NULL;
options.fromX = 0;
options.fromY = 0;
if
(ParseSubcommandOptions(&options, interp,
OPT_FORMAT | OPT_FROM | OPT_GRAYSCALE | OPT_BACKGROUND,
&index, objc, objv) != TCL_OK) {
return
TCL_ERROR;
}
if
((options.name != NULL) || (index < objc)) {
Tcl_WrongNumArgs(interp, 2, objv,
"?options?"
);
return
TCL_ERROR;
}
if
((options.fromX > masterPtr->width)
|| (options.fromY > masterPtr->height)
|| (options.fromX2 > masterPtr->width)
|| (options.fromY2 > masterPtr->height)) {
Tcl_AppendResult(interp,
"coordinates for -from option extend "
,
"outside image"
, (
char
*) NULL);
return
TCL_ERROR;
}
if
(((options.options & OPT_FROM) == 0) || (options.fromX2 < 0)) {
options.fromX2 = masterPtr->width;
options.fromY2 = masterPtr->height;
}
if
(options.options & OPT_FORMAT) {
for
(imageFormat = tsdPtr->formatList; imageFormat != NULL;
imageFormat = imageFormat->nextPtr) {
if
((strncasecmp(Tcl_GetString(options.format),
imageFormat->name,
strlen
(imageFormat->name)) == 0)) {
if
(imageFormat->stringWriteProc != NULL) {
stringWriteProc = imageFormat->stringWriteProc;
break
;
}
}
}
if
(stringWriteProc == NULL) {
Tcl_AppendResult(interp,
"image string format \""
,
Tcl_GetString(options.format),
"\" is not supported"
, (
char
*) NULL);
return
TCL_ERROR;
}
}
else
{
stringWriteProc = ImgStringWrite;
}
data = ImgGetPhoto(masterPtr, &block, &options);
result = stringWriteProc(interp, options.format, &block);
if
(options.background) {
Tk_FreeColor(options.background);
}
if
(data) {
ckfree(data);
}
return
result;
}
case
PHOTO_FORMATS: {
Tk_PhotoImageFormat *format = tsdPtr->formatList;
for
(format = tsdPtr->formatList; format; format = format->nextPtr) {
Tcl_ListObjAppendElement(interp, Tcl_GetObjResult(interp), Tcl_NewStringObj(format->name,-1));
}
for
(format = tsdPtr->oldFormatList; format; format = format->nextPtr) {
Tcl_ListObjAppendElement(interp, Tcl_GetObjResult(interp), Tcl_NewStringObj(format->name,-1));
}
return
TCL_OK;
break
;
}
case
PHOTO_GET: {
char
string[TCL_INTEGER_SPACE * 3];
if
(objc != 4) {
Tcl_WrongNumArgs(interp, 2, objv,
"x y"
);
return
TCL_ERROR;
}
if
((Tcl_GetIntFromObj(interp, objv[2], &x) != TCL_OK)
|| (Tcl_GetIntFromObj(interp, objv[3], &y) != TCL_OK)) {
return
TCL_ERROR;
}
if
((x < 0) || (x >= masterPtr->width)
|| (y < 0) || (y >= masterPtr->height)) {
Tcl_AppendResult(interp, Tcl_GetString(objv[0]),
" get: "
,
"coordinates out of range"
, (
char
*) NULL);
return
TCL_ERROR;
}
pixelPtr = masterPtr->pix32 + (y * masterPtr->width + x) * 4;
obj = Tcl_NewListObj(0,NULL);
for
(c=0; c < 3; c++) {
Tcl_ListObjAppendElement(interp, obj, Tcl_NewIntObj(pixelPtr[c]));
}
Tcl_SetObjResult(interp, obj);
return
TCL_OK;
}
case
PHOTO_PUT:
index = 2;
memset
((
VOID
*) &options, 0,
sizeof
(options));
options.name = NULL;
if
(ParseSubcommandOptions(&options, interp, OPT_TO|OPT_FORMAT,
&index, objc, objv) != TCL_OK) {
return
TCL_ERROR;
}
if
((options.name == NULL) || (index < objc)) {
Tcl_WrongNumArgs(interp, 2, objv,
"data ?options?"
);
return
TCL_ERROR;
}
if
(MatchStringFormat(interp, options.name ? objv[2]:NULL,
options.format, &imageFormat, &imageWidth,
&imageHeight, &oldformat) == TCL_OK) {
Tcl_Obj *format, *data;
if
(((options.options & OPT_TO) == 0) || (options.toX2 < 0)) {
options.toX2 = options.toX + imageWidth;
options.toY2 = options.toY + imageHeight;
}
if
(imageWidth > options.toX2 - options.toX) {
imageWidth = options.toX2 - options.toX;
}
if
(imageHeight > options.toY2 - options.toY) {
imageHeight = options.toY2 - options.toY;
}
format = options.format;
data = objv[2];
if
(oldformat) {
if
(format) {
format = (Tcl_Obj *) Tcl_GetString(format);
}
data = (Tcl_Obj *) Tcl_GetString(data);
}
if
((*imageFormat->stringReadProc)(interp, data,
format, (Tk_PhotoHandle) masterPtr,
options.toX, options.toY, imageWidth, imageHeight,
0, 0) != TCL_OK) {
return
TCL_ERROR;
}
masterPtr->flags |= IMAGE_CHANGED;
return
TCL_OK;
}
if
(options.options & OPT_FORMAT) {
return
TCL_ERROR;
}
Tcl_ResetResult(interp);
if
(Tcl_ListObjGetElements(interp, options.name, &dataHeight, &srcArgv) != TCL_OK) {
return
TCL_ERROR;
}
tkwin = Tk_MainWindow(interp);
block.pixelPtr = NULL;
dataWidth = 0;
pixelPtr = NULL;
for
(y = 0; y < dataHeight; ++y) {
if
(Tcl_ListObjGetElements(interp, srcArgv[y], &listArgc, &listArgv) != TCL_OK) {
break
;
}
if
(y == 0) {
if
(listArgc == 0) {
break
;
}
dataWidth = listArgc;
pixelPtr = (unsigned
char
*)
ckalloc((unsigned) dataWidth * dataHeight * 3);
block.pixelPtr = pixelPtr;
}
else
if
(listArgc != dataWidth) {
Tcl_AppendResult(interp,
"all elements of color list must"
,
" have the same number of elements"
, (
char
*) NULL);
break
;
}
for
(x = 0; x < dataWidth; ++x) {
if
(!XParseColor(Tk_Display(tkwin), Tk_Colormap(tkwin),
Tcl_GetString(listArgv[x]), &color)) {
Tcl_AppendResult(interp,
"can't parse color \""
,
Tcl_GetString(listArgv[x]),
"\""
, (
char
*) NULL);
break
;
}
*pixelPtr++ = color.red >> 8;
*pixelPtr++ = color.green >> 8;
*pixelPtr++ = color.blue >> 8;
}
if
(x < dataWidth) {
break
;
}
}
if
(y < dataHeight || dataHeight == 0 || dataWidth == 0) {
if
(block.pixelPtr != NULL) {
ckfree((
char
*) block.pixelPtr);
}
if
(y < dataHeight) {
return
TCL_ERROR;
}
return
TCL_OK;
}
if
(!(options.options & OPT_TO) || (options.toX2 < 0)) {
options.toX2 = options.toX + dataWidth;
options.toY2 = options.toY + dataHeight;
}
block.width = dataWidth;
block.height = dataHeight;
block.pitch = dataWidth * 3;
block.pixelSize = 3;
block.offset[0] = 0;
block.offset[1] = 1;
block.offset[2] = 2;
block.offset[3] = 0;
Tk_PhotoPutBlock((ClientData)masterPtr, &block,
options.toX, options.toY, options.toX2 - options.toX,
options.toY2 - options.toY, TK_PHOTO_COMPOSITE_SET);
ckfree((
char
*) block.pixelPtr);
return
TCL_OK;
case
PHOTO_READ: {
Tcl_Obj *format;
index = 2;
memset
((
VOID
*) &options, 0,
sizeof
(options));
options.name = NULL;
options.format = NULL;
if
(ParseSubcommandOptions(&options, interp,
OPT_FORMAT | OPT_FROM | OPT_TO | OPT_SHRINK,
&index, objc, objv) != TCL_OK) {
return
TCL_ERROR;
}
if
((options.name == NULL) || (index < objc)) {
Tcl_WrongNumArgs(interp, 2, objv,
"fileName ?options?"
);
return
TCL_ERROR;
}
if
(Tcl_IsSafe(interp)) {
Tcl_AppendResult(interp,
"can't get image from a file in a"
,
" safe interpreter"
, (
char
*) NULL);
return
TCL_ERROR;
}
chan = Tcl_OpenFileChannel(interp,
Tcl_GetString(options.name),
"r"
, 0);
if
(chan == NULL) {
return
TCL_ERROR;
}
if
(Tcl_SetChannelOption(interp, chan,
"-translation"
,
"binary"
)
!= TCL_OK) {
Tcl_Close(NULL, chan);
return
TCL_ERROR;
}
if
(Tcl_SetChannelOption(interp, chan,
"-encoding"
,
"binary"
)
!= TCL_OK) {
Tcl_Close(NULL, chan);
return
TCL_ERROR;
}
if
(MatchFileFormat(interp, chan,
options.name, options.format,
&imageFormat, &imageWidth, &imageHeight, &oldformat) != TCL_OK) {
Tcl_Close(NULL, chan);
return
TCL_ERROR;
}
if
((options.fromX > imageWidth) || (options.fromY > imageHeight)
|| (options.fromX2 > imageWidth)
|| (options.fromY2 > imageHeight)) {
Tcl_AppendResult(interp,
"coordinates for -from option extend "
,
"outside source image"
, (
char
*) NULL);
Tcl_Close(NULL, chan);
return
TCL_ERROR;
}
if
(((options.options & OPT_FROM) == 0) || (options.fromX2 < 0)) {
width = imageWidth - options.fromX;
height = imageHeight - options.fromY;
}
else
{
width = options.fromX2 - options.fromX;
height = options.fromY2 - options.fromY;
}
if
(options.options & OPT_SHRINK) {
if
(ImgPhotoSetSize(masterPtr, options.toX + width,
options.toY + height) != TCL_OK) {
Tcl_ResetResult(interp);
Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
TK_PHOTO_ALLOC_FAILURE_MESSAGE, (
char
*) NULL);
return
TCL_ERROR;
}
}
format = options.format;
if
(oldformat && format) {
format = (Tcl_Obj *) Tcl_GetString(format);
}
result = (*imageFormat->fileReadProc)(interp, chan,
options.name,
format, (Tk_PhotoHandle) masterPtr, options.toX,
options.toY, width, height, options.fromX, options.fromY);
if
(chan != NULL) {
Tcl_Close(NULL, chan);
}
return
result;
}
case
PHOTO_REDITHER:
if
(objc != 2) {
Tcl_WrongNumArgs(interp, 2, objv, (
char
*) NULL);
return
TCL_ERROR;
}
x = masterPtr->ditherX;
y = masterPtr->ditherY;
if
(masterPtr->ditherX != 0) {
Tk_DitherPhoto((Tk_PhotoHandle) masterPtr, x, y,
masterPtr->width - x, 1);
}
if
(masterPtr->ditherY < masterPtr->height) {
x = 0;
Tk_DitherPhoto((Tk_PhotoHandle)masterPtr, 0,
masterPtr->ditherY, masterPtr->width,
masterPtr->height - masterPtr->ditherY);
}
if
(y < masterPtr->height) {
Tk_ImageChanged(masterPtr->tkMaster, x, y,
(masterPtr->width - x), (masterPtr->height - y),
masterPtr->width, masterPtr->height);
}
return
TCL_OK;
case
PHOTO_TRANS: {
static
CONST
char
*photoTransOptions[] = {
"get"
,
"set"
, (
char
*) NULL
};
enum
transOptions {
PHOTO_TRANS_GET, PHOTO_TRANS_SET
};
if
(objc < 3) {
Tcl_WrongNumArgs(interp, 2, objv,
"option ?arg arg ...?"
);
return
TCL_ERROR;
}
if
(Tcl_GetIndexFromObj(interp, objv[2], photoTransOptions,
"option"
,
0, &index) != TCL_OK) {
return
TCL_ERROR;
}
switch
((
enum
transOptions) index) {
case
PHOTO_TRANS_GET: {
XRectangle testBox;
TkRegion testRegion;
if
(objc != 5) {
Tcl_WrongNumArgs(interp, 3, objv,
"x y"
);
return
TCL_ERROR;
}
if
((Tcl_GetIntFromObj(interp, objv[3], &x) != TCL_OK)
|| (Tcl_GetIntFromObj(interp, objv[4], &y) != TCL_OK)) {
return
TCL_ERROR;
}
if
((x < 0) || (x >= masterPtr->width)
|| (y < 0) || (y >= masterPtr->height)) {
Tcl_AppendResult(interp, Tcl_GetString(objv[0]),
" transparency get: coordinates out of range"
,
(
char
*) NULL);
return
TCL_ERROR;
}
testBox.x = x;
testBox.y = y;
testBox.width = 1;
testBox.height = 1;
testRegion = TkCreateRegion();
TkUnionRectWithRegion(&testBox, testRegion, testRegion);
TkIntersectRegion(testRegion, masterPtr->validRegion, testRegion);
TkClipBox(testRegion, &testBox);
TkDestroyRegion(testRegion);
Tcl_SetBooleanObj(Tcl_GetObjResult(interp),
(testBox.width==0 && testBox.height==0));
return
TCL_OK;
}
case
PHOTO_TRANS_SET: {
int
transFlag;
XRectangle setBox;
if
(objc != 6) {
Tcl_WrongNumArgs(interp, 3, objv,
"x y boolean"
);
return
TCL_ERROR;
}
if
((Tcl_GetIntFromObj(interp, objv[3], &x) != TCL_OK)
|| (Tcl_GetIntFromObj(interp, objv[4], &y) != TCL_OK)
|| (Tcl_GetBooleanFromObj(interp, objv[5],
&transFlag) != TCL_OK)) {
return
TCL_ERROR;
}
if
((x < 0) || (x >= masterPtr->width)
|| (y < 0) || (y >= masterPtr->height)) {
Tcl_AppendResult(interp, Tcl_GetString(objv[0]),
" transparency set: coordinates out of range"
,
(
char
*) NULL);
return
TCL_ERROR;
}
setBox.x = x;
setBox.y = y;
setBox.width = 1;
setBox.height = 1;
pixelPtr = masterPtr->pix32 + (y * masterPtr->width + x) * 4;
if
(transFlag) {
TkRegion clearRegion = TkCreateRegion();
TkUnionRectWithRegion(&setBox, clearRegion, clearRegion);
TkSubtractRegion(masterPtr->validRegion, clearRegion,
masterPtr->validRegion);
TkDestroyRegion(clearRegion);
pixelPtr[3] = 0;
}
else
{
TkUnionRectWithRegion(&setBox, masterPtr->validRegion,
masterPtr->validRegion);
pixelPtr[3] = 255;
}
Tk_ImageChanged(masterPtr->tkMaster, x, y, 1, 1,
masterPtr->width, masterPtr->height);
masterPtr->flags &= ~IMAGE_CHANGED;
return
TCL_OK;
}
}
panic(
"unexpected fallthrough"
);
}
case
PHOTO_WRITE: {
char
*data;
Tcl_Obj *format;
if
(Tcl_IsSafe(interp)) {
Tcl_AppendResult(interp,
"can't write image to a file in a"
,
" safe interpreter"
, (
char
*) NULL);
return
TCL_ERROR;
}
index = 2;
memset
((
VOID
*) &options, 0,
sizeof
(options));
options.name = NULL;
options.format = NULL;
if
(ParseSubcommandOptions(&options, interp,
OPT_FORMAT | OPT_FROM | OPT_GRAYSCALE | OPT_BACKGROUND,
&index, objc, objv) != TCL_OK) {
return
TCL_ERROR;
}
if
((options.name == NULL) || (index < objc)) {
Tcl_WrongNumArgs(interp, 2, objv,
"fileName ?options?"
);
return
TCL_ERROR;
}
if
((options.fromX > masterPtr->width)
|| (options.fromY > masterPtr->height)
|| (options.fromX2 > masterPtr->width)
|| (options.fromY2 > masterPtr->height)) {
Tcl_AppendResult(interp,
"coordinates for -from option extend "
,
"outside image"
, (
char
*) NULL);
return
TCL_ERROR;
}
if
(!(options.options & OPT_FROM) || (options.fromX2 < 0)) {
options.fromX2 = masterPtr->width;
options.fromY2 = masterPtr->height;
}
matched = 0;
for
(imageFormat = tsdPtr->formatList; imageFormat != NULL;
imageFormat = imageFormat->nextPtr) {
if
((options.format == NULL)
|| (strncasecmp(Tcl_GetString(options.format),
imageFormat->name,
strlen
(imageFormat->name)) == 0)) {
matched = 1;
if
(imageFormat->fileWriteProc != NULL) {
break
;
}
}
}
if
(imageFormat == NULL) {
oldformat = 1;
for
(imageFormat = tsdPtr->oldFormatList; imageFormat != NULL;
imageFormat = imageFormat->nextPtr) {
if
((options.format == NULL)
|| (strncasecmp(Tcl_GetString(options.format),
imageFormat->name,
strlen
(imageFormat->name)) == 0)) {
matched = 1;
if
(imageFormat->fileWriteProc != NULL) {
break
;
}
}
}
}
if
(imageFormat == NULL) {
if
(options.format == NULL) {
Tcl_AppendResult(interp,
"no available image file format "
,
"has file writing capability"
, (
char
*) NULL);
}
else
if
(!matched) {
Tcl_AppendResult(interp,
"image file format \""
,
Tcl_GetString(options.format),
"\" is unknown"
, (
char
*) NULL);
}
else
{
Tcl_AppendResult(interp,
"image file format \""
,
Tcl_GetString(options.format),
"\" has no file writing capability"
,
(
char
*) NULL);
}
return
TCL_ERROR;
}
data = ImgGetPhoto(masterPtr, &block, &options);
format = options.format;
if
(oldformat && format) {
format = (Tcl_Obj *) Tcl_GetString(options.format);
}
result = (*imageFormat->fileWriteProc)(interp,
Tcl_GetString(options.name), format, &block);
if
(options.background) {
Tk_FreeColor(options.background);
}
if
(data) {
ckfree(data);
}
return
result;
}
}
panic(
"unexpected fallthrough"
);
return
TCL_ERROR;
}
static
int
ParseSubcommandOptions(optPtr, interp, allowedOptions, optIndexPtr, objc, objv)
struct
SubcommandOptions *optPtr;
Tcl_Interp *interp;
int
allowedOptions;
int
*optIndexPtr;
int
objc;
Tcl_Obj *CONST objv[];
{
int
index, c, bit, currentBit;
int
length;
char
*option, **listPtr;
int
values[4];
int
numValues, maxValues, argIndex;
for
(index = *optIndexPtr; index < objc; *optIndexPtr = ++index) {
option = Tcl_GetStringFromObj(objv[index], &length);
if
(option[0] !=
'-'
) {
if
(optPtr->name == NULL) {
optPtr->name = objv[index];
continue
;
}
break
;
}
c = option[0];
bit = 0;
currentBit = 1;
for
(listPtr = optionNames; *listPtr != NULL; ++listPtr) {
if
((c == *listPtr[0])
&& (
strncmp
(option, *listPtr, (
size_t
) length) == 0)) {
if
(bit != 0) {
bit = 0;
break
;
}
bit = currentBit;
}
currentBit <<= 1;
}
if
((allowedOptions & bit) == 0) {
Tcl_AppendResult(interp,
"unrecognized option \""
,
Tcl_GetString(objv[index]),
"\": must be "
, (
char
*)NULL);
bit = 1;
for
(listPtr = optionNames; *listPtr != NULL; ++listPtr) {
if
((allowedOptions & bit) != 0) {
if
((allowedOptions & (bit - 1)) != 0) {
Tcl_AppendResult(interp,
", "
, (
char
*) NULL);
if
((allowedOptions & ~((bit << 1) - 1)) == 0) {
Tcl_AppendResult(interp,
"or "
, (
char
*) NULL);
}
}
Tcl_AppendResult(interp, *listPtr, (
char
*) NULL);
}
bit <<= 1;
}
return
TCL_ERROR;
}
if
(bit == OPT_BACKGROUND) {
if
(index + 1 < objc) {
*optIndexPtr = ++index;
optPtr->background = Tk_GetColor(interp, Tk_MainWindow(interp),
Tk_GetUid(Tcl_GetString(objv[index])));
if
(!optPtr->background) {
return
TCL_ERROR;
}
}
else
{
Tcl_AppendResult(interp,
"the \"-background\" option "
,
"requires a value"
, (
char
*) NULL);
return
TCL_ERROR;
}
}
else
if
(bit == OPT_FORMAT) {
if
(index + 1 < objc) {
*optIndexPtr = ++index;
optPtr->format = objv[index];
}
else
{
Tcl_AppendResult(interp,
"the \"-format\" option "
,
"requires a value"
, (
char
*) NULL);
return
TCL_ERROR;
}
}
else
if
(bit == OPT_COMPOSITE) {
if
(index + 1 < objc) {
static
CONST
char
*compositingRules[] = {
"overlay"
,
"set"
,
NULL
};
index++;
if
(Tcl_GetIndexFromObj(interp, objv[index], compositingRules,
"compositing rule"
, 0, &optPtr->compositingRule)
!= TCL_OK) {
return
TCL_ERROR;
}
*optIndexPtr = index;
}
else
{
Tcl_AppendResult(interp,
"the \"-compositingrule\" option "
,
"requires a value"
, (
char
*) NULL);
return
TCL_ERROR;
}
}
else
if
((bit != OPT_SHRINK) && (bit != OPT_GRAYSCALE)) {
char
*val;
maxValues = ((bit == OPT_FROM) || (bit == OPT_TO))? 4: 2;
argIndex = index + 1;
for
(numValues = 0; numValues < maxValues; ++numValues) {
if
(argIndex >= objc) {
break
;
}
val = Tcl_GetString(objv[argIndex]);
if
((argIndex < objc) && (
isdigit
(
UCHAR
(val[0]))
|| ((val[0] ==
'-'
) &&
isdigit
(
UCHAR
(val[1]))))) {
if
(Tcl_GetIntFromObj(interp, objv[argIndex], &values[numValues])
!= TCL_OK) {
return
TCL_ERROR;
}
}
else
{
break
;
}
++argIndex;
}
if
(numValues == 0) {
Tcl_AppendResult(interp,
"the \""
, option,
"\" option "
,
"requires one "
, maxValues == 2?
"or two"
:
"to four"
,
" integer values"
, (
char
*) NULL);
return
TCL_ERROR;
}
*optIndexPtr = (index += numValues);
if
(numValues == 1) {
values[1] = values[0];
}
if
(numValues == 3) {
values[3] = values[2];
}
switch
(bit) {
case
OPT_FROM:
if
((values[0] < 0) || (values[1] < 0) || ((numValues > 2)
&& ((values[2] < 0) || (values[3] < 0)))) {
Tcl_AppendResult(interp,
"value(s) for the -from"
,
" option must be non-negative"
, (
char
*) NULL);
return
TCL_ERROR;
}
if
(numValues <= 2) {
optPtr->fromX = values[0];
optPtr->fromY = values[1];
optPtr->fromX2 = -1;
optPtr->fromY2 = -1;
}
else
{
optPtr->fromX = MIN(values[0], values[2]);
optPtr->fromY = MIN(values[1], values[3]);
optPtr->fromX2 = MAX(values[0], values[2]);
optPtr->fromY2 = MAX(values[1], values[3]);
}
break
;
case
OPT_SUBSAMPLE:
optPtr->subsampleX = values[0];
optPtr->subsampleY = values[1];
break
;
case
OPT_TO:
if
((values[0] < 0) || (values[1] < 0) || ((numValues > 2)
&& ((values[2] < 0) || (values[3] < 0)))) {
Tcl_AppendResult(interp,
"value(s) for the -to"
,
" option must be non-negative"
, (
char
*) NULL);
return
TCL_ERROR;
}
if
(numValues <= 2) {
optPtr->toX = values[0];
optPtr->toY = values[1];
optPtr->toX2 = -1;
optPtr->toY2 = -1;
}
else
{
optPtr->toX = MIN(values[0], values[2]);
optPtr->toY = MIN(values[1], values[3]);
optPtr->toX2 = MAX(values[0], values[2]);
optPtr->toY2 = MAX(values[1], values[3]);
}
break
;
case
OPT_ZOOM:
if
((values[0] <= 0) || (values[1] <= 0)) {
Tcl_AppendResult(interp,
"value(s) for the -zoom"
,
" option must be positive"
, (
char
*) NULL);
return
TCL_ERROR;
}
optPtr->zoomX = values[0];
optPtr->zoomY = values[1];
break
;
}
}
optPtr->options |= bit;
}
return
TCL_OK;
}
static
int
ImgPhotoConfigureMaster(interp, masterPtr, objc, objv, flags)
Tcl_Interp *interp;
PhotoMaster *masterPtr;
int
objc;
Tcl_Obj *CONST objv[];
int
flags;
{
PhotoInstance *instancePtr;
Tcl_Obj *oldFileString;
CONST
char
*oldPaletteString;
Tcl_Obj *oldData, *data = NULL, *oldFormat, *format = NULL;
int
length, i, j;
double
oldGamma;
int
result;
Tcl_Channel chan;
Tk_PhotoImageFormat *imageFormat;
int
imageWidth, imageHeight;
CONST
char
**args;
int
oldformat;
Tcl_Obj *tempdata, *tempformat;
args = (CONST
char
**) ckalloc((objc + 1) *
sizeof
(
char
*));
for
(i = 0, j = 0; i < objc; i++,j++) {
args[j] = Tcl_GetStringFromObj(objv[i], &length);
if
((length > 1) && (args[j][0] ==
'-'
)) {
if
((args[j][1] ==
'd'
) &&
!
strncmp
(args[j],
"-data"
, (
size_t
) length)) {
if
(++i < objc) {
data = objv[i];
j--;
}
else
{
Tcl_AppendResult(interp,
"value for \"-data\" missing"
, (
char
*) NULL);
return
TCL_ERROR;
}
}
else
if
((args[j][1] ==
'f'
) &&
!
strncmp
(args[j],
"-format"
, (
size_t
) length)) {
if
(++i < objc) {
format = objv[i];
j--;
}
else
{
Tcl_AppendResult(interp,
"value for \"-format\" missing"
, (
char
*) NULL);
return
TCL_ERROR;
}
}
}
}
oldFileString = masterPtr->fileString;
if
(oldFileString == NULL) {
oldData = masterPtr->dataString;
if
(oldData != NULL) {
Tcl_IncrRefCount(oldData);
}
}
else
{
oldData = NULL;
}
oldFormat = masterPtr->format;
if
(oldFormat != NULL) {
Tcl_IncrRefCount(oldFormat);
}
oldPaletteString = masterPtr->palette;
oldGamma = masterPtr->gamma;
if
(Tk_ConfigureWidget(interp, Tk_MainWindow(interp), configSpecs,
objc, objv, (
char
*) masterPtr, flags) != TCL_OK) {
ckfree((
char
*) args);
goto
errorExit;
}
ckfree((
char
*) args);
if
((masterPtr->fileString != NULL) && (Tcl_GetString(masterPtr->fileString)[0] == 0)) {
Tcl_DecrRefCount(masterPtr->fileString);
masterPtr->fileString = NULL;
}
if
(data) {
if
(TclObjLength(data)
|| (TclObjGetType(data) == Tcl_GetObjType(
"bytearray"
)
&& TclObjInternal(data)->otherValuePtr != NULL)) {
Tcl_IncrRefCount(data);
}
else
{
data = NULL;
}
}
if
(masterPtr->dataString != NULL) {
Tcl_GetStringFromObj(masterPtr->dataString, &length);
if
(!length) {
Tcl_DecrRefCount(masterPtr->dataString);
masterPtr->dataString = NULL;
}
}
if
(masterPtr->format != NULL) {
Tcl_GetStringFromObj(masterPtr->format, &length);
if
(!length) {
Tcl_DecrRefCount(masterPtr->format);
masterPtr->format = NULL;
}
}
if
(ImgPhotoSetSize(masterPtr, masterPtr->width,
masterPtr->height) != TCL_OK) {
Tcl_ResetResult(interp);
Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
TK_PHOTO_ALLOC_FAILURE_MESSAGE, (
char
*) NULL);
goto
errorExit;
}
if
((masterPtr->fileString != NULL)
&& ((masterPtr->fileString != oldFileString)
|| (masterPtr->format != oldFormat))) {
if
(Tcl_IsSafe(interp)) {
Tcl_ResetResult(interp);
Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
"can't get image from a file in a safe interpreter"
,
(
char
*) NULL);
goto
errorExit;
}
chan = Tcl_OpenFileChannel(interp, Tcl_GetString(masterPtr->fileString),
"r"
, 0);
if
(chan == NULL) {
goto
errorExit;
}
if
((Tcl_SetChannelOption(interp, chan,
"-translation"
,
"binary"
) != TCL_OK) ||
(MatchFileFormat(interp, chan, masterPtr->fileString,
masterPtr->format, &imageFormat, &imageWidth,
&imageHeight, &oldformat) != TCL_OK)) {
Tcl_Close(NULL, chan);
goto
errorExit;
}
result = ImgPhotoSetSize(masterPtr, imageWidth, imageHeight);
if
(result != TCL_OK) {
Tcl_Close(NULL, chan);
Tcl_ResetResult(interp);
Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
TK_PHOTO_ALLOC_FAILURE_MESSAGE, (
char
*) NULL);
goto
errorExit;
}
tempformat = masterPtr->format;
if
(oldformat && tempformat) {
tempformat = (Tcl_Obj *) Tcl_GetString(tempformat);
}
result = (*imageFormat->fileReadProc)(interp, chan,
masterPtr->fileString, tempformat,
(Tk_PhotoHandle) masterPtr, 0, 0,
imageWidth, imageHeight, 0, 0);
Tcl_Close(NULL, chan);
if
(result != TCL_OK) {
goto
errorExit;
}
Tcl_ResetResult(interp);
masterPtr->flags |= IMAGE_CHANGED;
}
if
((masterPtr->fileString == NULL) && (masterPtr->dataString != NULL)
&& ((masterPtr->dataString != oldData)
|| (masterPtr->format != oldFormat))) {
if
(MatchStringFormat(interp, masterPtr->dataString,
masterPtr->format, &imageFormat, &imageWidth,
&imageHeight, &oldformat) != TCL_OK) {
goto
errorExit;
}
if
(ImgPhotoSetSize(masterPtr, imageWidth, imageHeight) != TCL_OK) {
Tcl_ResetResult(interp);
Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
TK_PHOTO_ALLOC_FAILURE_MESSAGE, (
char
*) NULL);
goto
errorExit;
}
tempformat = masterPtr->format;
tempdata = masterPtr->dataString;
if
(oldformat) {
if
(tempformat) {
tempformat = (Tcl_Obj *) Tcl_GetString(tempformat);
}
tempdata = (Tcl_Obj *) Tcl_GetString(tempdata);
}
if
((*imageFormat->stringReadProc)(interp, tempdata,
tempformat, (Tk_PhotoHandle) masterPtr,
0, 0, imageWidth, imageHeight, 0, 0) != TCL_OK) {
goto
errorExit;
}
Tcl_ResetResult(interp);
masterPtr->flags |= IMAGE_CHANGED;
}
if
(masterPtr->gamma <= 0) {
masterPtr->gamma = 1.0;
}
if
((masterPtr->gamma != oldGamma)
|| (masterPtr->palette != oldPaletteString)) {
masterPtr->flags |= IMAGE_CHANGED;
}
for
(instancePtr = masterPtr->instancePtr; instancePtr != NULL;
instancePtr = instancePtr->nextPtr) {
ImgPhotoConfigureInstance(instancePtr);
}
Tk_ImageChanged(masterPtr->tkMaster, 0, 0, masterPtr->width,
masterPtr->height, masterPtr->width, masterPtr->height);
masterPtr->flags &= ~IMAGE_CHANGED;
if
(oldData != NULL) {
Tcl_DecrRefCount(oldData);
}
if
(oldFormat != NULL) {
Tcl_DecrRefCount(oldFormat);
}
ToggleComplexAlphaIfNeeded(masterPtr);
return
TCL_OK;
errorExit:
if
(oldData != NULL) {
Tcl_DecrRefCount(oldData);
}
if
(oldFormat != NULL) {
Tcl_DecrRefCount(oldFormat);
}
return
TCL_ERROR;
}
static
void
ImgPhotoConfigureInstance(instancePtr)
PhotoInstance *instancePtr;
{
PhotoMaster *masterPtr = instancePtr->masterPtr;
XImage *imagePtr;
int
bitsPerPixel;
ColorTable *colorTablePtr;
XRectangle validBox;
if
((masterPtr->palette && masterPtr->palette[0])
&& IsValidPalette(instancePtr, masterPtr->palette)) {
instancePtr->palette = masterPtr->palette;
}
else
{
instancePtr->palette = instancePtr->defaultPalette;
}
instancePtr->gamma = masterPtr->gamma;
colorTablePtr = instancePtr->colorTablePtr;
if
((colorTablePtr == NULL)
|| (instancePtr->colormap != colorTablePtr->id.colormap)
|| (instancePtr->palette != colorTablePtr->id.palette)
|| (instancePtr->gamma != colorTablePtr->id.gamma)) {
if
(colorTablePtr != NULL) {
colorTablePtr->liveRefCount -= 1;
FreeColorTable(colorTablePtr, 0);
}
GetColorTable(instancePtr);
if
(instancePtr->colorTablePtr->flags & BLACK_AND_WHITE) {
bitsPerPixel = 1;
}
else
{
bitsPerPixel = instancePtr->visualInfo.depth;
}
if
((instancePtr->imagePtr == NULL)
|| (instancePtr->imagePtr->bits_per_pixel != bitsPerPixel)) {
if
(instancePtr->imagePtr != NULL) {
XFree((
char
*) instancePtr->imagePtr);
}
imagePtr = XCreateImage(instancePtr->display,
instancePtr->visualInfo.visual, (unsigned) bitsPerPixel,
(bitsPerPixel > 1? ZPixmap: XYBitmap), 0, (
char
*) NULL,
1, 1, 32, 0);
instancePtr->imagePtr = imagePtr;
if
(imagePtr != NULL) {
union
{
int
i;
char
c[
sizeof
(
int
)];
} kludge;
imagePtr->bitmap_unit =
sizeof
(pixel) * NBBY;
kludge.i = 0;
kludge.c[0] = 1;
imagePtr->byte_order = (kludge.i == 1) ? LSBFirst : MSBFirst;
_XInitImageFuncPtrs(imagePtr);
}
}
}
if
((instancePtr->pixels == None) || (instancePtr->error == NULL)
|| (instancePtr->width != masterPtr->width)
|| (instancePtr->height != masterPtr->height)) {
ImgPhotoInstanceSetSize(instancePtr);
}
if
((masterPtr->flags & IMAGE_CHANGED)
|| (instancePtr->colorTablePtr != colorTablePtr)) {
TkClipBox(masterPtr->validRegion, &validBox);
if
((validBox.width > 0) && (validBox.height > 0)) {
DitherInstance(instancePtr, validBox.x, validBox.y,
validBox.width, validBox.height);
}
}
}
static
ClientData
ImgPhotoGet(tkwin, masterData)
Tk_Window tkwin;
ClientData masterData;
{
PhotoMaster *masterPtr = (PhotoMaster *) masterData;
PhotoInstance *instancePtr;
Colormap colormap;
int
mono, nRed, nGreen, nBlue;
XVisualInfo visualInfo, *visInfoPtr;
char
buf[TCL_INTEGER_SPACE * 3];
int
numVisuals;
XColor *white, *black;
XGCValues gcValues;
static
int
paletteChoice[13][3] = {
{2, 2, 2,
},
{2, 3, 2,
},
{3, 4, 2,
},
{4, 5, 3,
},
{5, 6, 4,
},
{7, 7, 4,
},
{8, 10, 6,
},
{10, 12, 8,
},
{14, 15, 9,
},
{16, 20, 12,
},
{20, 24, 16,
},
{26, 30, 20,
},
{32, 32, 30,
}
};
colormap = Tk_Colormap(tkwin);
for
(instancePtr = masterPtr->instancePtr; instancePtr != NULL;
instancePtr = instancePtr->nextPtr) {
if
((colormap == instancePtr->colormap)
&& (Tk_Display(tkwin) == instancePtr->display)) {
if
(instancePtr->refCount == 0) {
Tcl_CancelIdleCall(DisposeInstance, (ClientData) instancePtr);
if
(instancePtr->colorTablePtr != NULL) {
FreeColorTable(instancePtr->colorTablePtr, 0);
}
GetColorTable(instancePtr);
}
instancePtr->refCount++;
return
(ClientData) instancePtr;
}
}
instancePtr = (PhotoInstance *) ckalloc(
sizeof
(PhotoInstance));
instancePtr->masterPtr = masterPtr;
instancePtr->display = Tk_Display(tkwin);
instancePtr->colormap = Tk_Colormap(tkwin);
Tk_PreserveColormap(instancePtr->display, instancePtr->colormap);
instancePtr->refCount = 1;
instancePtr->colorTablePtr = NULL;
instancePtr->pixels = None;
instancePtr->error = NULL;
instancePtr->width = 0;
instancePtr->height = 0;
instancePtr->imagePtr = 0;
instancePtr->nextPtr = masterPtr->instancePtr;
masterPtr->instancePtr = instancePtr;
visualInfo.screen = Tk_ScreenNumber(tkwin);
visualInfo.visualid = XVisualIDFromVisual(Tk_Visual(tkwin));
visInfoPtr = XGetVisualInfo(Tk_Display(tkwin),
VisualScreenMask | VisualIDMask, &visualInfo, &numVisuals);
nRed = 2;
nGreen = nBlue = 0;
mono = 1;
if
(visInfoPtr != NULL) {
instancePtr->visualInfo = *visInfoPtr;
switch
(visInfoPtr->
class
) {
case
DirectColor:
case
TrueColor:
nRed = 1 << CountBits(visInfoPtr->red_mask);
nGreen = 1 << CountBits(visInfoPtr->green_mask);
nBlue = 1 << CountBits(visInfoPtr->blue_mask);
mono = 0;
break
;
case
PseudoColor:
case
StaticColor:
if
(visInfoPtr->depth > 15) {
nRed = 32;
nGreen = 32;
nBlue = 32;
mono = 0;
}
else
if
(visInfoPtr->depth >= 3) {
int
*ip = paletteChoice[visInfoPtr->depth - 3];
nRed = ip[0];
nGreen = ip[1];
nBlue = ip[2];
mono = 0;
}
break
;
case
GrayScale:
case
StaticGray:
nRed = 1 << visInfoPtr->depth;
break
;
}
XFree((
char
*) visInfoPtr);
}
else
{
panic(
"ImgPhotoGet couldn't find visual for window"
);
}
sprintf
(buf, ((mono) ?
"%d"
:
"%d/%d/%d"
), nRed, nGreen, nBlue);
instancePtr->defaultPalette = Tk_GetUid(buf);
white = Tk_GetColor(masterPtr->interp, tkwin,
"white"
);
black = Tk_GetColor(masterPtr->interp, tkwin,
"black"
);
gcValues.foreground = (white != NULL)? white->pixel:
WhitePixelOfScreen(Tk_Screen(tkwin));
gcValues.background = (black != NULL)? black->pixel:
BlackPixelOfScreen(Tk_Screen(tkwin));
gcValues.graphics_exposures = False;
instancePtr->gc = Tk_GetGC(tkwin,
GCForeground|GCBackground|GCGraphicsExposures, &gcValues);
ImgPhotoConfigureInstance(instancePtr);
if
(instancePtr->nextPtr == NULL) {
Tk_ImageChanged(masterPtr->tkMaster, 0, 0, 0, 0,
masterPtr->width, masterPtr->height);
}
return
(ClientData) instancePtr;
}
static
int
ToggleComplexAlphaIfNeeded(PhotoMaster *mPtr)
{
size_t
len = MAX(mPtr->userWidth, mPtr->width) *
MAX(mPtr->userHeight, mPtr->height) * 4;
unsigned
char
*c = mPtr->pix32;
unsigned
char
*end = c + len;
mPtr->flags &= ~COMPLEX_ALPHA;
c += 3;
for
(; c < end; c += 4) {
if
(*c && *c != 255) {
mPtr->flags |= COMPLEX_ALPHA;
break
;
}
}
return
(mPtr->flags & COMPLEX_ALPHA);
}
#ifndef __WIN32__
#define GetRValue(rgb) (UCHAR((rgb & red_mask) >> red_shift))
#define GetGValue(rgb) (UCHAR((rgb & green_mask) >> green_shift))
#define GetBValue(rgb) (UCHAR((rgb & blue_mask) >> blue_shift))
#define RGB(r,g,b) ((unsigned)((UCHAR(r)<<red_shift)|(UCHAR(g)<<green_shift)|(UCHAR(b)<<blue_shift)))
#define RGB15(r,g,b) ((unsigned)(((r*red_mask/255)&red_mask)|((g*green_mask/255)&green_mask)|((b*blue_mask/255)&blue_mask)))
#endif
static
void
ImgPhotoBlendComplexAlpha (
XImage *bgImg,
PhotoInstance *iPtr,
int
xOffset,
int
yOffset,
int
width,
int
height
)
{
int
x, y, line;
unsigned
long
pixel;
unsigned
char
r, g, b, alpha, unalpha;
unsigned
char
*alphaAr = iPtr->masterPtr->pix32;
unsigned
char
*masterPtr;
#ifndef __WIN32__
unsigned
long
red_mask, green_mask, blue_mask;
unsigned
long
red_shift, green_shift, blue_shift;
Visual *visual = iPtr->visualInfo.visual;
red_mask = visual->red_mask;
green_mask = visual->green_mask;
blue_mask = visual->blue_mask;
red_shift = 0;
green_shift = 0;
blue_shift = 0;
while
((0x0001 & (red_mask >> red_shift)) == 0) red_shift++;
while
((0x0001 & (green_mask >> green_shift)) == 0) green_shift++;
while
((0x0001 & (blue_mask >> blue_shift)) == 0) blue_shift++;
#endif
#define ALPHA_BLEND(bgPix, imgPix, alpha, unalpha) \
((bgPix * unalpha + imgPix * alpha) / 255)
#if !(defined(__WIN32__) || defined(MAC_OSX_TK))
if
(bgImg->depth < 24) {
unsigned
char
red_mlen, green_mlen, blue_mlen;
red_mlen = 8 - CountBits(red_mask >> red_shift);
green_mlen = 8 - CountBits(green_mask >> green_shift);
blue_mlen = 8 - CountBits(blue_mask >> blue_shift);
for
(y = 0; y < height; y++) {
line = (y + yOffset) * iPtr->masterPtr->width;
for
(x = 0; x < width; x++) {
masterPtr = alphaAr + ((line + x + xOffset) * 4);
alpha = masterPtr[3];
if
(alpha) {
r = masterPtr[0];
g = masterPtr[1];
b = masterPtr[2];
if
(alpha != 255) {
unsigned
char
ra, ga, ba;
pixel = XGetPixel(bgImg, x, y);
ra = GetRValue(pixel) << red_mlen;
ga = GetGValue(pixel) << green_mlen;
ba = GetBValue(pixel) << blue_mlen;
unalpha = 255 - alpha;
r = ALPHA_BLEND(ra, r, alpha, unalpha);
g = ALPHA_BLEND(ga, g, alpha, unalpha);
b = ALPHA_BLEND(ba, b, alpha, unalpha);
}
XPutPixel(bgImg, x, y, RGB15(r, g, b));
}
}
}
}
else
#endif
for
(y = 0; y < height; y++) {
line = (y + yOffset) * iPtr->masterPtr->width;
for
(x = 0; x < width; x++) {
masterPtr = alphaAr + ((line + x + xOffset) * 4);
alpha = masterPtr[3];
if
(alpha) {
r = masterPtr[0];
g = masterPtr[1];
b = masterPtr[2];
if
(alpha != 255) {
unsigned
char
ra, ga, ba;
pixel = XGetPixel(bgImg, x, y);
ra = GetRValue(pixel);
ga = GetGValue(pixel);
ba = GetBValue(pixel);
unalpha = 255 - alpha;
r = ALPHA_BLEND(ra, r, alpha, unalpha);
g = ALPHA_BLEND(ga, g, alpha, unalpha);
b = ALPHA_BLEND(ba, b, alpha, unalpha);
}
XPutPixel(bgImg, x, y, RGB(r, g, b));
}
}
}
#undef ALPHA_BLEND
}
static
void
ImgPhotoDisplay(clientData, display, drawable, imageX, imageY, width,
height, drawableX, drawableY)
ClientData clientData;
Display *display;
Drawable drawable;
int
imageX, imageY;
int
width, height;
int
drawableX, drawableY;
{
PhotoInstance *instancePtr = (PhotoInstance *) clientData;
XVisualInfo visInfo = instancePtr->visualInfo;
if
(instancePtr->pixels == None) {
return
;
}
if
(
#if defined(MAC_TCL) || defined(MAC_OSX_TK)
0 &&
#endif
(instancePtr->masterPtr->flags & COMPLEX_ALPHA)
&& visInfo.depth >= 15
&& (visInfo.
class
== DirectColor || visInfo.
class
== TrueColor)) {
Tk_ErrorHandler handler;
XImage *bgImg = NULL;
handler = Tk_CreateErrorHandler(display, -1, -1, -1,
(Tk_ErrorProc *) NULL, (ClientData) NULL);
bgImg = XGetImage(display, drawable, drawableX, drawableY,
(unsigned
int
)width, (unsigned
int
)height, AllPlanes, ZPixmap);
if
(bgImg == NULL) {
Tk_DeleteErrorHandler(handler);
return
;
}
ImgPhotoBlendComplexAlpha(bgImg, instancePtr,
imageX, imageY, width, height);
TkPutImage(NULL, 0, display, drawable, instancePtr->gc,
bgImg, 0, 0, drawableX, drawableY,
(unsigned
int
) width, (unsigned
int
) height);
XDestroyImage(bgImg);
Tk_DeleteErrorHandler(handler);
}
else
{
TkSetRegion(display, instancePtr->gc, instancePtr->masterPtr->validRegion);
XSetClipOrigin(display, instancePtr->gc, drawableX - imageX,
drawableY - imageY);
XCopyArea(display, instancePtr->pixels, drawable, instancePtr->gc,
imageX, imageY, (unsigned) width, (unsigned) height,
drawableX, drawableY);
XSetClipMask(display, instancePtr->gc, None);
XSetClipOrigin(display, instancePtr->gc, 0, 0);
}
XFlush (display);
}
static
void
ImgPhotoFree(clientData, display)
ClientData clientData;
Display *display;
{
PhotoInstance *instancePtr = (PhotoInstance *) clientData;
ColorTable *colorPtr;
instancePtr->refCount -= 1;
if
(instancePtr->refCount > 0) {
return
;
}
colorPtr = instancePtr->colorTablePtr;
if
(colorPtr != NULL) {
colorPtr->liveRefCount -= 1;
}
Tcl_DoWhenIdle(DisposeInstance, (ClientData) instancePtr);
}
static
void
ImgPhotoDelete(masterData)
ClientData masterData;
{
PhotoMaster *masterPtr = (PhotoMaster *) masterData;
PhotoInstance *instancePtr;
while
((instancePtr = masterPtr->instancePtr) != NULL) {
if
(instancePtr->refCount > 0) {
panic(
"tried to delete photo image when instances still exist"
);
}
Tcl_CancelIdleCall(DisposeInstance, (ClientData) instancePtr);
DisposeInstance((ClientData) instancePtr);
}
masterPtr->tkMaster = NULL;
if
(masterPtr->imageCmd != NULL) {
Tcl_DeleteCommandFromToken(masterPtr->interp, masterPtr->imageCmd);
}
if
(masterPtr->pix32 != NULL) {
ckfree((
char
*) masterPtr->pix32);
}
if
(masterPtr->validRegion != NULL) {
TkDestroyRegion(masterPtr->validRegion);
}
#ifndef _LANG
if
(masterPtr->dataString != NULL) {
Tcl_DecrRefCount(masterPtr->dataString);
}
if
(masterPtr->format != NULL) {
Tcl_DecrRefCount(masterPtr->format);
}
#endif
Tk_FreeOptions(configSpecs, (
char
*) masterPtr, (Display *) NULL, 0);
ckfree((
char
*) masterPtr);
}
static
void
ImgPhotoCmdDeletedProc(clientData)
ClientData clientData;
{
PhotoMaster *masterPtr = (PhotoMaster *) clientData;
masterPtr->imageCmd = NULL;
if
(masterPtr->tkMaster != NULL) {
Tk_DeleteImage(masterPtr->interp, Tk_NameOfImage(masterPtr->tkMaster));
}
}
static
int
ImgPhotoSetSize(masterPtr, width, height)
PhotoMaster *masterPtr;
int
width, height;
{
unsigned
char
*newPix32 = NULL;
int
h, offset, pitch;
unsigned
char
*srcPtr, *destPtr;
XRectangle validBox, clipBox;
TkRegion clipRegion;
PhotoInstance *instancePtr;
if
(masterPtr->userWidth > 0) {
width = masterPtr->userWidth;
}
if
(masterPtr->userHeight > 0) {
height = masterPtr->userHeight;
}
pitch = width * 4;
if
((width != masterPtr->width) || (height != masterPtr->height)
|| (masterPtr->pix32 == NULL)) {
unsigned
newPixSize = (unsigned
) (height * pitch);
if
(newPixSize == 0) {
newPix32 = NULL;
}
else
{
newPix32 = (unsigned
char
*) attemptckalloc(newPixSize);
if
(newPix32 == NULL) {
return
TCL_ERROR;
}
}
}
TkClipBox(masterPtr->validRegion, &validBox);
if
((validBox.x + validBox.width > width)
|| (validBox.y + validBox.height > height)) {
clipBox.x = 0;
clipBox.y = 0;
clipBox.width = width;
clipBox.height = height;
clipRegion = TkCreateRegion();
TkUnionRectWithRegion(&clipBox, clipRegion, clipRegion);
TkIntersectRegion(masterPtr->validRegion, clipRegion,
masterPtr->validRegion);
TkDestroyRegion(clipRegion);
TkClipBox(masterPtr->validRegion, &validBox);
}
if
(newPix32 != NULL) {
if
((masterPtr->pix32 != NULL)
&& ((width == masterPtr->width) || (width == validBox.width))) {
if
(validBox.y > 0) {
memset
((
VOID
*) newPix32, 0, (
size_t
) (validBox.y * pitch));
}
h = validBox.y + validBox.height;
if
(h < height) {
memset
((
VOID
*) (newPix32 + h * pitch), 0,
(
size_t
) ((height - h) * pitch));
}
}
else
{
memset
((
VOID
*) newPix32, 0, (
size_t
) (height * pitch));
}
if
(masterPtr->pix32 != NULL) {
if
(newPix32 == NULL) {
}
else
if
(width == masterPtr->width) {
offset = validBox.y * pitch;
memcpy
((
VOID
*) (newPix32 + offset),
(
VOID
*) (masterPtr->pix32 + offset),
(
size_t
) (validBox.height * pitch));
}
else
if
((validBox.width > 0) && (validBox.height > 0)) {
destPtr = newPix32 + (validBox.y * width + validBox.x) * 4;
srcPtr = masterPtr->pix32 + (validBox.y * masterPtr->width
+ validBox.x) * 4;
for
(h = validBox.height; h > 0; h--) {
memcpy
((
VOID
*) destPtr, (
VOID
*) srcPtr,
(
size_t
) (validBox.width * 4));
destPtr += width * 4;
srcPtr += masterPtr->width * 4;
}
}
ckfree((
char
*) masterPtr->pix32);
}
masterPtr->pix32 = newPix32;
masterPtr->width = width;
masterPtr->height = height;
if
((validBox.x > 0) || (validBox.y > 0)) {
masterPtr->ditherX = 0;
masterPtr->ditherY = 0;
}
else
if
(validBox.width == width) {
if
((
int
) validBox.height < masterPtr->ditherY) {
masterPtr->ditherX = 0;
masterPtr->ditherY = validBox.height;
}
}
else
if
((masterPtr->ditherY > 0)
|| ((
int
) validBox.width < masterPtr->ditherX)) {
masterPtr->ditherX = validBox.width;
masterPtr->ditherY = 0;
}
}
ToggleComplexAlphaIfNeeded(masterPtr);
for
(instancePtr = masterPtr->instancePtr; instancePtr != NULL;
instancePtr = instancePtr->nextPtr) {
ImgPhotoInstanceSetSize(instancePtr);
}
return
TCL_OK;
}
static
void
ImgPhotoInstanceSetSize(instancePtr)
PhotoInstance *instancePtr;
{
PhotoMaster *masterPtr;
schar *newError;
schar *errSrcPtr, *errDestPtr;
int
h, offset;
XRectangle validBox;
Pixmap newPixmap;
masterPtr = instancePtr->masterPtr;
TkClipBox(masterPtr->validRegion, &validBox);
if
((instancePtr->width != masterPtr->width)
|| (instancePtr->height != masterPtr->height)
|| (instancePtr->pixels == None)) {
newPixmap = Tk_GetPixmap(instancePtr->display,
RootWindow(instancePtr->display,
instancePtr->visualInfo.screen),
(masterPtr->width > 0) ? masterPtr->width: 1,
(masterPtr->height > 0) ? masterPtr->height: 1,
instancePtr->visualInfo.depth);
if
(!newPixmap) {
panic(
"Fail to create pixmap with Tk_GetPixmap in ImgPhotoInstanceSetSize.\n"
);
return
;
}
TkSetPixmapColormap(newPixmap, instancePtr->colormap);
if
(instancePtr->pixels != None) {
XCopyArea(instancePtr->display, instancePtr->pixels, newPixmap,
instancePtr->gc, validBox.x, validBox.y,
validBox.width, validBox.height, validBox.x, validBox.y);
Tk_FreePixmap(instancePtr->display, instancePtr->pixels);
}
instancePtr->pixels = newPixmap;
}
if
((instancePtr->width != masterPtr->width)
|| (instancePtr->height != masterPtr->height)
|| (instancePtr->error == NULL)) {
if
(masterPtr->height > 0 && masterPtr->width > 0) {
newError = (schar *) ckalloc((unsigned)
masterPtr->height * masterPtr->width * 3 *
sizeof
(schar));
if
((instancePtr->error != NULL)
&& ((instancePtr->width == masterPtr->width)
|| (validBox.width == masterPtr->width))) {
if
(validBox.y > 0) {
memset
((
VOID
*) newError, 0, (
size_t
)
validBox.y * masterPtr->width * 3 *
sizeof
(schar));
}
h = validBox.y + validBox.height;
if
(h < masterPtr->height) {
memset
((
VOID
*) (newError + h * masterPtr->width * 3), 0,
(
size_t
) (masterPtr->height - h)
* masterPtr->width * 3 *
sizeof
(schar));
}
}
else
{
memset
((
VOID
*) newError, 0, (
size_t
)
masterPtr->height * masterPtr->width * 3 *
sizeof
(schar));
}
}
else
{
newError = NULL;
}
if
(instancePtr->error != NULL) {
if
(masterPtr->width == instancePtr->width) {
offset = validBox.y * masterPtr->width * 3;
memcpy
((
VOID
*) (newError + offset),
(
VOID
*) (instancePtr->error + offset),
(
size_t
) (validBox.height
* masterPtr->width * 3 *
sizeof
(schar)));
}
else
if
(validBox.width > 0 && validBox.height > 0) {
errDestPtr = newError
+ (validBox.y * masterPtr->width + validBox.x) * 3;
errSrcPtr = instancePtr->error
+ (validBox.y * instancePtr->width + validBox.x) * 3;
for
(h = validBox.height; h > 0; --h) {
memcpy
((
VOID
*) errDestPtr, (
VOID
*) errSrcPtr,
validBox.width * 3 *
sizeof
(schar));
errDestPtr += masterPtr->width * 3;
errSrcPtr += instancePtr->width * 3;
}
}
ckfree((
char
*) instancePtr->error);
}
instancePtr->error = newError;
}
instancePtr->width = masterPtr->width;
instancePtr->height = masterPtr->height;
}
static
int
IsValidPalette(instancePtr, palette)
PhotoInstance *instancePtr;
CONST
char
*palette;
{
int
nRed, nGreen, nBlue, mono, numColors;
char
*endp;
nRed =
strtol
(palette, &endp, 10);
if
((endp == palette) || ((*endp != 0) && (*endp !=
'/'
))
|| (nRed < 2) || (nRed > 256)) {
return
0;
}
if
(*endp == 0) {
mono = 1;
nGreen = nBlue = nRed;
}
else
{
palette = endp + 1;
nGreen =
strtol
(palette, &endp, 10);
if
((endp == palette) || (*endp !=
'/'
) || (nGreen < 2)
|| (nGreen > 256)) {
return
0;
}
palette = endp + 1;
nBlue =
strtol
(palette, &endp, 10);
if
((endp == palette) || (*endp != 0) || (nBlue < 2)
|| (nBlue > 256)) {
return
0;
}
mono = 0;
}
switch
(instancePtr->visualInfo.
class
) {
case
DirectColor:
case
TrueColor:
if
((nRed > (1 << CountBits(instancePtr->visualInfo.red_mask)))
|| (nGreen > (1
<< CountBits(instancePtr->visualInfo.green_mask)))
|| (nBlue > (1
<< CountBits(instancePtr->visualInfo.blue_mask)))) {
return
0;
}
break
;
case
PseudoColor:
case
StaticColor:
numColors = nRed;
if
(!mono) {
numColors *= nGreen*nBlue;
}
if
(numColors > (1 << instancePtr->visualInfo.depth)) {
return
0;
}
break
;
case
GrayScale:
case
StaticGray:
if
(!mono || (nRed > (1 << instancePtr->visualInfo.depth))) {
return
0;
}
break
;
}
return
1;
}
static
int
CountBits(mask)
pixel mask;
{
int
n;
for
( n = 0; mask != 0; mask &= mask - 1 )
n++;
return
n;
}
static
void
GetColorTable(instancePtr)
PhotoInstance *instancePtr;
{
ColorTable *colorPtr;
Tcl_HashEntry *entry;
ColorTableId id;
int
isNew;
memset
((
VOID
*) &id, 0,
sizeof
(id));
id.display = instancePtr->display;
id.colormap = instancePtr->colormap;
id.palette = instancePtr->palette;
id.gamma = instancePtr->gamma;
if
(!imgPhotoColorHashInitialized) {
Tcl_InitHashTable(&imgPhotoColorHash, N_COLOR_HASH);
imgPhotoColorHashInitialized = 1;
}
entry = Tcl_CreateHashEntry(&imgPhotoColorHash, (
char
*) &id, &isNew);
if
(!isNew) {
colorPtr = (ColorTable *) Tcl_GetHashValue(entry);
}
else
{
colorPtr = (ColorTable *) ckalloc(
sizeof
(ColorTable));
memset
((
VOID
*) &colorPtr->id, 0,
sizeof
(ColorTableId));
colorPtr->id = id;
Tk_PreserveColormap(colorPtr->id.display, colorPtr->id.colormap);
colorPtr->flags = 0;
colorPtr->refCount = 0;
colorPtr->liveRefCount = 0;
colorPtr->numColors = 0;
colorPtr->visualInfo = instancePtr->visualInfo;
colorPtr->pixelMap = NULL;
Tcl_SetHashValue(entry, colorPtr);
}
colorPtr->refCount++;
colorPtr->liveRefCount++;
instancePtr->colorTablePtr = colorPtr;
if
(colorPtr->flags & DISPOSE_PENDING) {
Tcl_CancelIdleCall(DisposeColorTable, (ClientData) colorPtr);
colorPtr->flags &= ~DISPOSE_PENDING;
}
if
((colorPtr->numColors == 0)
&& ((colorPtr->flags & BLACK_AND_WHITE) == 0)) {
AllocateColors(colorPtr);
}
}
static
void
FreeColorTable(colorPtr, force)
ColorTable *colorPtr;
int
force;
{
colorPtr->refCount--;
if
(colorPtr->refCount > 0) {
return
;
}
if
(force) {
if
((colorPtr->flags & DISPOSE_PENDING) != 0) {
Tcl_CancelIdleCall(DisposeColorTable, (ClientData) colorPtr);
colorPtr->flags &= ~DISPOSE_PENDING;
}
DisposeColorTable((ClientData) colorPtr);
}
else
if
((colorPtr->flags & DISPOSE_PENDING) == 0) {
Tcl_DoWhenIdle(DisposeColorTable, (ClientData) colorPtr);
colorPtr->flags |= DISPOSE_PENDING;
}
}
static
void
AllocateColors(colorPtr)
ColorTable *colorPtr;
{
int
i, r, g, b, rMult, mono;
int
numColors, nRed, nGreen, nBlue;
double
fr, fg, fb, igam;
XColor *colors;
unsigned
long
*pixels;
# define CFRAC(i, n) ((i) * 65535 / (n))
# define CGFRAC(i, n, g) ((int)(65535 * pow((double)(i) / (n), (g))))
mono =
sscanf
(colorPtr->id.palette,
"%d/%d/%d"
, &nRed, &nGreen, &nBlue)
<= 1;
igam = 1.0 / colorPtr->id.gamma;
for
(;;) {
if
(mono && (nRed <= 2)) {
colorPtr->flags |= BLACK_AND_WHITE;
return
;
}
if
((colorPtr->visualInfo.
class
== DirectColor)
|| (colorPtr->visualInfo.
class
== TrueColor)) {
if
(mono) {
numColors = nGreen = nBlue = nRed;
}
else
{
numColors = MAX(MAX(nRed, nGreen), nBlue);
}
colors = (XColor *) ckalloc(numColors *
sizeof
(XColor));
for
(i = 0; i < numColors; ++i) {
if
(igam == 1.0) {
colors[i].red = CFRAC(i, nRed - 1);
colors[i].green = CFRAC(i, nGreen - 1);
colors[i].blue = CFRAC(i, nBlue - 1);
}
else
{
colors[i].red = CGFRAC(i, nRed - 1, igam);
colors[i].green = CGFRAC(i, nGreen - 1, igam);
colors[i].blue = CGFRAC(i, nBlue - 1, igam);
}
}
}
else
{
numColors = (mono) ? nRed: (nRed * nGreen * nBlue);
colors = (XColor *) ckalloc(numColors *
sizeof
(XColor));
if
(!mono) {
i = 0;
for
(r = 0; r < nRed; ++r) {
for
(g = 0; g < nGreen; ++g) {
for
(b = 0; b < nBlue; ++b) {
if
(igam == 1.0) {
colors[i].red = CFRAC(r, nRed - 1);
colors[i].green = CFRAC(g, nGreen - 1);
colors[i].blue = CFRAC(b, nBlue - 1);
}
else
{
colors[i].red = CGFRAC(r, nRed - 1, igam);
colors[i].green = CGFRAC(g, nGreen - 1, igam);
colors[i].blue = CGFRAC(b, nBlue - 1, igam);
}
i++;
}
}
}
}
else
{
for
(i = 0; i < numColors; ++i) {
if
(igam == 1.0) {
r = CFRAC(i, numColors - 1);
}
else
{
r = CGFRAC(i, numColors - 1, igam);
}
colors[i].red = colors[i].green = colors[i].blue = r;
}
}
}
pixels = (unsigned
long
*) ckalloc(numColors *
sizeof
(unsigned
long
));
for
(i = 0; i < numColors; ++i) {
if
(!XAllocColor(colorPtr->id.display, colorPtr->id.colormap,
&colors[i])) {
if
(!ReclaimColors(&colorPtr->id, numColors - i)
|| !XAllocColor(colorPtr->id.display,
colorPtr->id.colormap, &colors[i])) {
break
;
}
}
pixels[i] = colors[i].pixel;
}
if
(i >= numColors) {
break
;
}
XFreeColors(colorPtr->id.display, colorPtr->id.colormap, pixels, i, 0);
ckfree((
char
*) colors);
ckfree((
char
*) pixels);
if
(!mono) {
if
((nRed == 2) && (nGreen == 2) && (nBlue == 2)) {
mono = 1;
}
else
{
nRed = (nRed * 3 + 2) / 4;
nGreen = (nGreen * 3 + 2) / 4;
nBlue = (nBlue * 3 + 2) / 4;
}
}
else
{
nRed = nRed / 2;
}
}
if
(!mono) {
colorPtr->flags |= COLOR_WINDOW;
#ifndef __WIN32__
if
((colorPtr->visualInfo.
class
!= DirectColor)
&& (colorPtr->visualInfo.
class
!= TrueColor)) {
colorPtr->flags |= MAP_COLORS;
}
#endif /* __WIN32__ */
}
colorPtr->numColors = numColors;
colorPtr->pixelMap = pixels;
rMult = nGreen * nBlue;
for
(i = 0; i < 256; ++i) {
r = (i * (nRed - 1) + 127) / 255;
if
(mono) {
fr = (
double
) colors[r].red / 65535.0;
if
(colorPtr->id.gamma != 1.0 ) {
fr =
pow
(fr, colorPtr->id.gamma);
}
colorPtr->colorQuant[0][i] = (
int
)(fr * 255.99);
colorPtr->redValues[i] = colors[r].pixel;
}
else
{
g = (i * (nGreen - 1) + 127) / 255;
b = (i * (nBlue - 1) + 127) / 255;
if
((colorPtr->visualInfo.
class
== DirectColor)
|| (colorPtr->visualInfo.
class
== TrueColor)) {
colorPtr->redValues[i] = colors[r].pixel
& colorPtr->visualInfo.red_mask;
colorPtr->greenValues[i] = colors[g].pixel
& colorPtr->visualInfo.green_mask;
colorPtr->blueValues[i] = colors[b].pixel
& colorPtr->visualInfo.blue_mask;
}
else
{
r *= rMult;
g *= nBlue;
colorPtr->redValues[i] = r;
colorPtr->greenValues[i] = g;
colorPtr->blueValues[i] = b;
}
fr = (
double
) colors[r].red / 65535.0;
fg = (
double
) colors[g].green / 65535.0;
fb = (
double
) colors[b].blue / 65535.0;
if
(colorPtr->id.gamma != 1.0) {
fr =
pow
(fr, colorPtr->id.gamma);
fg =
pow
(fg, colorPtr->id.gamma);
fb =
pow
(fb, colorPtr->id.gamma);
}
colorPtr->colorQuant[0][i] = (
int
)(fr * 255.99);
colorPtr->colorQuant[1][i] = (
int
)(fg * 255.99);
colorPtr->colorQuant[2][i] = (
int
)(fb * 255.99);
}
}
ckfree((
char
*) colors);
}
static
void
DisposeColorTable(clientData)
ClientData clientData;
{
ColorTable *colorPtr;
Tcl_HashEntry *entry;
colorPtr = (ColorTable *) clientData;
if
(colorPtr->pixelMap != NULL) {
if
(colorPtr->numColors > 0) {
XFreeColors(colorPtr->id.display, colorPtr->id.colormap,
colorPtr->pixelMap, colorPtr->numColors, 0);
Tk_FreeColormap(colorPtr->id.display, colorPtr->id.colormap);
}
ckfree((
char
*) colorPtr->pixelMap);
}
entry = Tcl_FindHashEntry(&imgPhotoColorHash, (
char
*) &colorPtr->id);
if
(entry == NULL) {
panic(
"DisposeColorTable couldn't find hash entry"
);
}
Tcl_DeleteHashEntry(entry);
ckfree((
char
*) colorPtr);
}
static
int
ReclaimColors(id, numColors)
ColorTableId *id;
int
numColors;
{
Tcl_HashSearch srch;
Tcl_HashEntry *entry;
ColorTable *colorPtr;
int
nAvail;
nAvail = 0;
entry = Tcl_FirstHashEntry(&imgPhotoColorHash, &srch);
while
(entry != NULL) {
colorPtr = (ColorTable *) Tcl_GetHashValue(entry);
if
((colorPtr->id.display == id->display)
&& (colorPtr->id.colormap == id->colormap)
&& (colorPtr->liveRefCount == 0 )&& (colorPtr->numColors != 0)
&& ((colorPtr->id.palette != id->palette)
|| (colorPtr->id.gamma != id->gamma))) {
nAvail += colorPtr->numColors;
}
entry = Tcl_NextHashEntry(&srch);
}
if
(nAvail < numColors) {
return
0;
}
entry = Tcl_FirstHashEntry(&imgPhotoColorHash, &srch);
while
((entry != NULL) && (numColors > 0)) {
colorPtr = (ColorTable *) Tcl_GetHashValue(entry);
if
((colorPtr->id.display == id->display)
&& (colorPtr->id.colormap == id->colormap)
&& (colorPtr->liveRefCount == 0) && (colorPtr->numColors != 0)
&& ((colorPtr->id.palette != id->palette)
|| (colorPtr->id.gamma != id->gamma))) {
XFreeColors(colorPtr->id.display, colorPtr->id.colormap,
colorPtr->pixelMap, colorPtr->numColors, 0);
numColors -= colorPtr->numColors;
colorPtr->numColors = 0;
ckfree((
char
*) colorPtr->pixelMap);
colorPtr->pixelMap = NULL;
}
entry = Tcl_NextHashEntry(&srch);
}
return
1;
}
static
void
DisposeInstance(clientData)
ClientData clientData;
{
PhotoInstance *instancePtr = (PhotoInstance *) clientData;
PhotoInstance *prevPtr;
if
(instancePtr->pixels != None) {
Tk_FreePixmap(instancePtr->display, instancePtr->pixels);
}
if
(instancePtr->gc != None) {
Tk_FreeGC(instancePtr->display, instancePtr->gc);
}
if
(instancePtr->imagePtr != NULL) {
XFree((
char
*) instancePtr->imagePtr);
}
if
(instancePtr->error != NULL) {
ckfree((
char
*) instancePtr->error);
}
if
(instancePtr->colorTablePtr != NULL) {
FreeColorTable(instancePtr->colorTablePtr, 1);
}
if
(instancePtr->masterPtr->instancePtr == instancePtr) {
instancePtr->masterPtr->instancePtr = instancePtr->nextPtr;
}
else
{
for
(prevPtr = instancePtr->masterPtr->instancePtr;
prevPtr->nextPtr != instancePtr; prevPtr = prevPtr->nextPtr) {
}
prevPtr->nextPtr = instancePtr->nextPtr;
}
Tk_FreeColormap(instancePtr->display, instancePtr->colormap);
ckfree((
char
*) instancePtr);
}
char
*
Tk_PhotoFormatName(interp, formatString)
Tcl_Interp *interp;
Tcl_Obj * formatString;
{
int
objc = 0;
Tcl_Obj **objv;
if
(formatString != NULL &&
Tcl_ListObjGetElements(interp, formatString, &objc, &objv) == TCL_OK &&
objc != 0) {
return
Tcl_GetString(objv[0]);
}
return
NULL;
}
static
int
MatchFileFormat(interp, chan, fileName, formatObj, imageFormatPtr,
widthPtr, heightPtr, oldformat)
Tcl_Interp *interp;
Tcl_Channel chan;
Tcl_Obj *fileName;
Tcl_Obj *formatObj;
Tk_PhotoImageFormat **imageFormatPtr;
int
*widthPtr, *heightPtr;
int
*oldformat;
{
int
matched;
int
useoldformat = 0;
Tk_PhotoImageFormat *formatPtr;
ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
Tcl_GetThreadData(&dataKey,
sizeof
(ThreadSpecificData));
char
*formatString = NULL;
if
(formatObj) {
formatString = Tk_PhotoFormatName(interp, formatObj);
}
matched = 0;
for
(formatPtr = tsdPtr->formatList; formatPtr != NULL;
formatPtr = formatPtr->nextPtr) {
if
(formatObj != NULL) {
if
(strncasecmp(formatString,
formatPtr->name,
strlen
(formatPtr->name)) != 0) {
continue
;
}
matched = 1;
if
(formatPtr->fileMatchProc == NULL) {
Tcl_AppendResult(interp,
"-file option isn't supported for "
,
formatPtr->name,
" images"
, (
char
*) NULL);
return
TCL_ERROR;
}
}
if
(formatPtr->fileMatchProc != NULL) {
(
void
) Tcl_Seek(chan, Tcl_LongAsWide(0L), SEEK_SET);
if
((*formatPtr->fileMatchProc)(chan, fileName, formatObj,
widthPtr, heightPtr, interp)) {
if
(*widthPtr < 1) {
*widthPtr = 1;
}
if
(*heightPtr < 1) {
*heightPtr = 1;
}
break
;
}
}
}
if
(formatPtr == NULL) {
useoldformat = 1;
for
(formatPtr = tsdPtr->oldFormatList; formatPtr != NULL;
formatPtr = formatPtr->nextPtr) {
if
(formatString != NULL) {
if
(strncasecmp(formatString,
formatPtr->name,
strlen
(formatPtr->name)) != 0) {
continue
;
}
matched = 1;
if
(formatPtr->fileMatchProc == NULL) {
Tcl_AppendResult(interp,
"-file option isn't supported"
,
" for "
, formatString,
" images"
, (
char
*) NULL);
return
TCL_ERROR;
}
}
if
(formatPtr->fileMatchProc != NULL) {
(
void
) Tcl_Seek(chan, Tcl_LongAsWide(0L), SEEK_SET);
if
((*formatPtr->fileMatchProc)(chan, fileName, (Tcl_Obj *)
formatString, widthPtr, heightPtr, interp)) {
if
(*widthPtr < 1) {
*widthPtr = 1;
}
if
(*heightPtr < 1) {
*heightPtr = 1;
}
break
;
}
}
}
}
if
(formatPtr == NULL) {
if
((formatObj != NULL) && !matched) {
Tcl_AppendResult(interp,
"image file format \""
,
formatString,
"\" is not supported"
, (
char
*) NULL);
}
else
{
Tcl_AppendResult(interp,
"couldn't recognize data in image file \""
,
fileName,
"\""
, (
char
*) NULL);
}
return
TCL_ERROR;
}
*imageFormatPtr = formatPtr;
*oldformat = useoldformat;
(
void
) Tcl_Seek(chan, Tcl_LongAsWide(0L), SEEK_SET);
return
TCL_OK;
}
static
int
MatchStringFormat(interp, data, formatObj, imageFormatPtr,
widthPtr, heightPtr, oldformat)
Tcl_Interp *interp;
Tcl_Obj *data;
Tcl_Obj *formatObj;
Tk_PhotoImageFormat **imageFormatPtr;
int
*widthPtr, *heightPtr;
int
*oldformat;
{
int
matched;
int
useoldformat = 0;
Tk_PhotoImageFormat *formatPtr;
ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
Tcl_GetThreadData(&dataKey,
sizeof
(ThreadSpecificData));
char
*formatString = NULL;
if
(formatObj) {
formatString = Tcl_GetString(formatObj);
}
matched = 0;
for
(formatPtr = tsdPtr->formatList; formatPtr != NULL;
formatPtr = formatPtr->nextPtr) {
if
(formatObj != NULL) {
if
(strncasecmp(formatString,
formatPtr->name,
strlen
(formatPtr->name)) != 0) {
continue
;
}
matched = 1;
if
(formatPtr->stringMatchProc == NULL) {
Tcl_AppendResult(interp,
"-data option isn't supported for "
,
formatPtr->name,
" images"
, (
char
*) NULL);
return
TCL_ERROR;
}
}
if
((formatPtr->stringMatchProc != NULL)
&& (formatPtr->stringReadProc != NULL)
&& (*formatPtr->stringMatchProc)(data, formatObj,
widthPtr, heightPtr, interp)) {
break
;
}
}
if
(formatPtr == NULL) {
useoldformat = 1;
for
(formatPtr = tsdPtr->oldFormatList; formatPtr != NULL;
formatPtr = formatPtr->nextPtr) {
if
(formatObj != NULL) {
if
(strncasecmp(formatString,
formatPtr->name,
strlen
(formatPtr->name)) != 0) {
continue
;
}
matched = 1;
if
(formatPtr->stringMatchProc == NULL) {
Tcl_AppendResult(interp,
"-data option isn't supported"
,
" for "
, formatString,
" images"
, (
char
*) NULL);
return
TCL_ERROR;
}
}
if
((formatPtr->stringMatchProc != NULL)
&& (formatPtr->stringReadProc != NULL)
&& (*formatPtr->stringMatchProc)(
(Tcl_Obj *) Tcl_GetString(data),
(Tcl_Obj *) formatString,
widthPtr, heightPtr, interp)) {
break
;
}
}
}
if
(formatPtr == NULL) {
if
((formatObj != NULL) && !matched) {
Tcl_AppendResult(interp,
"image format \""
, formatString,
"\" is not supported"
, (
char
*) NULL);
}
else
{
Tcl_AppendResult(interp,
"couldn't recognize image data"
,
(
char
*) NULL);
}
return
TCL_ERROR;
}
*imageFormatPtr = formatPtr;
*oldformat = useoldformat;
return
TCL_OK;
}
Tk_PhotoHandle
Tk_FindPhoto(interp, imageName)
Tcl_Interp *interp;
CONST
char
*imageName;
{
ClientData clientData;
Tk_ImageType *typePtr;
clientData = Tk_GetImageMasterData(interp, imageName, &typePtr);
if
(typePtr != &tkPhotoImageType) {
return
NULL;
}
return
(Tk_PhotoHandle) clientData;
}
void
Tk_PhotoPutBlock(handle, blockPtr, x, y, width, height, compRule)
Tk_PhotoHandle handle;
register
Tk_PhotoImageBlock *blockPtr;
int
x, y;
int
width, height;
int
compRule;
{
register
PhotoMaster *masterPtr;
int
xEnd, yEnd;
int
greenOffset, blueOffset, alphaOffset;
int
wLeft, hLeft;
int
wCopy, hCopy;
unsigned
char
*srcPtr, *srcLinePtr;
unsigned
char
*destPtr, *destLinePtr;
int
pitch;
XRectangle rect;
masterPtr = (PhotoMaster *) handle;
if
((masterPtr->userWidth != 0) && ((x + width) > masterPtr->userWidth)) {
width = masterPtr->userWidth - x;
}
if
((masterPtr->userHeight != 0)
&& ((y + height) > masterPtr->userHeight)) {
height = masterPtr->userHeight - y;
}
if
((width <= 0) || (height <= 0)) {
return
;
}
xEnd = x + width;
yEnd = y + height;
if
((xEnd > masterPtr->width) || (yEnd > masterPtr->height)) {
if
(ImgPhotoSetSize(masterPtr, MAX(xEnd, masterPtr->width),
MAX(yEnd, masterPtr->height)) == TCL_ERROR) {
panic(TK_PHOTO_ALLOC_FAILURE_MESSAGE);
}
}
if
((y < masterPtr->ditherY) || ((y == masterPtr->ditherY)
&& (x < masterPtr->ditherX))) {
masterPtr->ditherX = x;
masterPtr->ditherY = y;
}
greenOffset = blockPtr->offset[1] - blockPtr->offset[0];
blueOffset = blockPtr->offset[2] - blockPtr->offset[0];
alphaOffset = blockPtr->offset[3];
if
((alphaOffset >= blockPtr->pixelSize) || (alphaOffset < 0)) {
alphaOffset = 0;
}
else
{
alphaOffset -= blockPtr->offset[0];
}
if
((greenOffset != 0) || (blueOffset != 0)) {
masterPtr->flags |= COLOR_IMAGE;
}
destLinePtr = masterPtr->pix32 + (y * masterPtr->width + x) * 4;
pitch = masterPtr->width * 4;
if
((blockPtr->pixelSize == 4)
&& (greenOffset == 1) && (blueOffset == 2) && (alphaOffset == 3)
&& (width <= blockPtr->width) && (height <= blockPtr->height)
&& ((height == 1) || ((x == 0) && (width == masterPtr->width)
&& (blockPtr->pitch == pitch)))
&& (compRule == TK_PHOTO_COMPOSITE_SET)) {
memcpy
((
VOID
*) destLinePtr,
(
VOID
*) (blockPtr->pixelPtr + blockPtr->offset[0]),
(
size_t
) (height * width * 4));
}
else
{
int
alpha;
for
(hLeft = height; hLeft > 0;) {
srcLinePtr = blockPtr->pixelPtr + blockPtr->offset[0];
hCopy = MIN(hLeft, blockPtr->height);
hLeft -= hCopy;
for
(; hCopy > 0; --hCopy) {
if
((blockPtr->pixelSize == 4) && (greenOffset == 1)
&& (blueOffset == 2) && (alphaOffset == 3)
&& (width <= blockPtr->width)
&& (compRule == TK_PHOTO_COMPOSITE_SET)) {
memcpy
((
VOID
*) destLinePtr, (
VOID
*) srcLinePtr,
(
size_t
) (width * 4));
}
else
{
destPtr = destLinePtr;
for
(wLeft = width; wLeft > 0;) {
wCopy = MIN(wLeft, blockPtr->width);
wLeft -= wCopy;
srcPtr = srcLinePtr;
for
(; wCopy > 0; --wCopy) {
alpha = srcPtr[alphaOffset];
if
(!alphaOffset || (alpha == 255)) {
*destPtr++ = srcPtr[0];
*destPtr++ = srcPtr[greenOffset];
*destPtr++ = srcPtr[blueOffset];
*destPtr++ = 255;
srcPtr += blockPtr->pixelSize;
continue
;
}
switch
(compRule) {
case
TK_PHOTO_COMPOSITE_SET:
*destPtr++ = srcPtr[0];
*destPtr++ = srcPtr[greenOffset];
*destPtr++ = srcPtr[blueOffset];
*destPtr++ = alpha;
break
;
case
TK_PHOTO_COMPOSITE_OVERLAY:
if
(!destPtr[3]) {
destPtr[0] = destPtr[1] = destPtr[2] = 0xd9;
}
if
(alpha) {
destPtr[0] += (srcPtr[0] - destPtr[0]) * alpha / 255;
destPtr[1] += (srcPtr[greenOffset] - destPtr[1]) * alpha / 255;
destPtr[2] += (srcPtr[blueOffset] - destPtr[2]) * alpha / 255;
destPtr[3] += (255 - destPtr[3]) * alpha / 255;
}
destPtr += 4;
break
;
default
:
panic(
"unknown compositing rule: %d"
, compRule);
}
srcPtr += blockPtr->pixelSize;
}
}
}
srcLinePtr += blockPtr->pitch;
destLinePtr += pitch;
}
}
}
if
(alphaOffset) {
int
x1, y1, end;
if
(compRule != TK_PHOTO_COMPOSITE_OVERLAY) {
TkRegion workRgn = TkCreateRegion();
rect.x = x;
rect.y = y;
rect.width = width;
rect.height = height;
TkUnionRectWithRegion(&rect, workRgn, workRgn);
TkSubtractRegion(masterPtr->validRegion, workRgn,
masterPtr->validRegion);
TkDestroyRegion(workRgn);
}
destLinePtr = masterPtr->pix32 + (y * masterPtr->width + x) * 4 + 3;
for
(y1 = 0; y1 < height; y1++) {
x1 = 0;
destPtr = destLinePtr;
while
(x1 < width) {
while
((x1 < width) && !*destPtr) {
x1++;
destPtr += 4;
}
end = x1;
while
((end < width) && *destPtr) {
end++;
destPtr += 4;
}
if
(end > x1) {
rect.x = x + x1;
rect.y = y + y1;
rect.width = end - x1;
rect.height = 1;
TkUnionRectWithRegion(&rect, masterPtr->validRegion,
masterPtr->validRegion);
}
x1 = end;
}
destLinePtr += masterPtr->width * 4;
}
}
else
{
rect.x = x;
rect.y = y;
rect.width = width;
rect.height = height;
TkUnionRectWithRegion(&rect, masterPtr->validRegion,
masterPtr->validRegion);
}
Tk_DitherPhoto((Tk_PhotoHandle)masterPtr, x, y, width, height);
Tk_ImageChanged(masterPtr->tkMaster, x, y, width, height, masterPtr->width,
masterPtr->height);
}
void
Tk_PhotoPutZoomedBlock(handle, blockPtr, x, y, width, height, zoomX, zoomY,
subsampleX, subsampleY, compRule)
Tk_PhotoHandle handle;
register
Tk_PhotoImageBlock *blockPtr;
int
x, y;
int
width, height;
int
zoomX, zoomY;
int
subsampleX, subsampleY;
int
compRule;
{
register
PhotoMaster *masterPtr;
int
xEnd, yEnd;
int
greenOffset, blueOffset, alphaOffset;
int
wLeft, hLeft;
int
wCopy, hCopy;
int
blockWid, blockHt;
unsigned
char
*srcPtr, *srcLinePtr, *srcOrigPtr;
unsigned
char
*destPtr, *destLinePtr;
int
pitch;
int
xRepeat, yRepeat;
int
blockXSkip, blockYSkip;
XRectangle rect;
if
(zoomX==1 && zoomY==1 && subsampleX==1 && subsampleY==1) {
Tk_PhotoPutBlock(handle, blockPtr, x, y, width, height, compRule);
return
;
}
masterPtr = (PhotoMaster *) handle;
if
(zoomX <= 0 || zoomY <= 0) {
return
;
}
if
((masterPtr->userWidth != 0) && ((x + width) > masterPtr->userWidth)) {
width = masterPtr->userWidth - x;
}
if
((masterPtr->userHeight != 0)
&& ((y + height) > masterPtr->userHeight)) {
height = masterPtr->userHeight - y;
}
if
(width <= 0 || height <= 0) {
return
;
}
xEnd = x + width;
yEnd = y + height;
if
((xEnd > masterPtr->width) || (yEnd > masterPtr->height)) {
int
sameSrc = (blockPtr->pixelPtr == masterPtr->pix32);
if
(ImgPhotoSetSize(masterPtr, MAX(xEnd, masterPtr->width),
MAX(yEnd, masterPtr->height)) == TCL_ERROR) {
panic(TK_PHOTO_ALLOC_FAILURE_MESSAGE);
}
if
(sameSrc) {
blockPtr->pixelPtr = masterPtr->pix32;
}
}
if
((y < masterPtr->ditherY) || ((y == masterPtr->ditherY)
&& (x < masterPtr->ditherX))) {
masterPtr->ditherX = x;
masterPtr->ditherY = y;
}
greenOffset = blockPtr->offset[1] - blockPtr->offset[0];
blueOffset = blockPtr->offset[2] - blockPtr->offset[0];
alphaOffset = blockPtr->offset[3];
if
((alphaOffset >= blockPtr->pixelSize) || (alphaOffset < 0)) {
alphaOffset = 0;
}
else
{
alphaOffset -= blockPtr->offset[0];
}
if
((greenOffset != 0) || (blueOffset != 0)) {
masterPtr->flags |= COLOR_IMAGE;
}
blockXSkip = subsampleX * blockPtr->pixelSize;
blockYSkip = subsampleY * blockPtr->pitch;
if
(subsampleX > 0) {
blockWid = ((blockPtr->width + subsampleX - 1) / subsampleX) * zoomX;
}
else
if
(subsampleX == 0) {
blockWid = width;
}
else
{
blockWid = ((blockPtr->width - subsampleX - 1) / -subsampleX) * zoomX;
}
if
(subsampleY > 0) {
blockHt = ((blockPtr->height + subsampleY - 1) / subsampleY) * zoomY;
}
else
if
(subsampleY == 0) {
blockHt = height;
}
else
{
blockHt = ((blockPtr->height - subsampleY - 1) / -subsampleY) * zoomY;
}
destLinePtr = masterPtr->pix32 + (y * masterPtr->width + x) * 4;
srcOrigPtr = blockPtr->pixelPtr + blockPtr->offset[0];
if
(subsampleX < 0) {
srcOrigPtr += (blockPtr->width - 1) * blockPtr->pixelSize;
}
if
(subsampleY < 0) {
srcOrigPtr += (blockPtr->height - 1) * blockPtr->pitch;
}
pitch = masterPtr->width * 4;
for
(hLeft = height; hLeft > 0; ) {
hCopy = MIN(hLeft, blockHt);
hLeft -= hCopy;
yRepeat = zoomY;
srcLinePtr = srcOrigPtr;
for
(; hCopy > 0; --hCopy) {
destPtr = destLinePtr;
for
(wLeft = width; wLeft > 0;) {
wCopy = MIN(wLeft, blockWid);
wLeft -= wCopy;
srcPtr = srcLinePtr;
for
(; wCopy > 0; wCopy -= zoomX) {
for
(xRepeat = MIN(wCopy, zoomX); xRepeat > 0; xRepeat--) {
if
(!alphaOffset || (srcPtr[alphaOffset] == 255)) {
*destPtr++ = srcPtr[0];
*destPtr++ = srcPtr[greenOffset];
*destPtr++ = srcPtr[blueOffset];
*destPtr++ = 255;
continue
;
}
switch
(compRule) {
case
TK_PHOTO_COMPOSITE_SET:
*destPtr++ = srcPtr[0];
*destPtr++ = srcPtr[greenOffset];
*destPtr++ = srcPtr[blueOffset];
*destPtr++ = srcPtr[alphaOffset];
break
;
case
TK_PHOTO_COMPOSITE_OVERLAY:
if
(!destPtr[3]) {
destPtr[0] = destPtr[1] = destPtr[2] = 0xd9;
}
if
(srcPtr[alphaOffset]) {
destPtr[0] += (srcPtr[0] - destPtr[0]) * srcPtr[alphaOffset] / 255;
destPtr[1] += (srcPtr[greenOffset] - destPtr[1]) * srcPtr[alphaOffset] / 255;
destPtr[2] += (srcPtr[blueOffset] - destPtr[2]) * srcPtr[alphaOffset] / 255;
destPtr[3] += (255 - destPtr[3]) * srcPtr[alphaOffset] / 255;
}
destPtr += 4;
break
;
default
:
panic(
"unknown compositing rule: %d"
, compRule);
}
}
srcPtr += blockXSkip;
}
}
destLinePtr += pitch;
yRepeat--;
if
(yRepeat <= 0) {
srcLinePtr += blockYSkip;
yRepeat = zoomY;
}
}
}
if
(alphaOffset) {
int
x1, y1, end;
if
(compRule != TK_PHOTO_COMPOSITE_OVERLAY) {
TkRegion workRgn = TkCreateRegion();
rect.x = x;
rect.y = y;
rect.width = width;
rect.height = 1;
TkUnionRectWithRegion(&rect, workRgn, workRgn);
TkSubtractRegion(masterPtr->validRegion, workRgn,
masterPtr->validRegion);
TkDestroyRegion(workRgn);
}
destLinePtr = masterPtr->pix32 + (y * masterPtr->width + x) * 4 + 3;
for
(y1 = 0; y1 < height; y1++) {
x1 = 0;
destPtr = destLinePtr;
while
(x1 < width) {
while
((x1 < width) && !*destPtr) {
x1++;
destPtr += 4;
}
end = x1;
while
((end < width) && *destPtr) {
end++;
destPtr += 4;
}
if
(end > x1) {
rect.x = x + x1;
rect.y = y + y1;
rect.width = end - x1;
rect.height = 1;
TkUnionRectWithRegion(&rect, masterPtr->validRegion,
masterPtr->validRegion);
}
x1 = end;
}
destLinePtr += masterPtr->width * 4;
}
}
else
{
rect.x = x;
rect.y = y;
rect.width = width;
rect.height = height;
TkUnionRectWithRegion(&rect, masterPtr->validRegion,
masterPtr->validRegion);
}
Tk_DitherPhoto((Tk_PhotoHandle)masterPtr, x, y, width, height);
Tk_ImageChanged(masterPtr->tkMaster, x, y, width, height, masterPtr->width,
masterPtr->height);
}
void
Tk_DitherPhoto(photo, x, y, width, height)
Tk_PhotoHandle photo;
int
x, y;
int
width, height;
{
PhotoMaster *masterPtr = (PhotoMaster *) photo;
PhotoInstance *instancePtr;
if
((width <= 0) || (height <= 0)) {
return
;
}
for
(instancePtr = masterPtr->instancePtr; instancePtr != NULL;
instancePtr = instancePtr->nextPtr) {
DitherInstance(instancePtr, x, y, width, height);
}
if
(((y < masterPtr->ditherY)
|| ((y == masterPtr->ditherY) && (x <= masterPtr->ditherX)))
&& ((y + height) > (masterPtr->ditherY))) {
if
((x == 0) && (width == masterPtr->width)) {
masterPtr->ditherX = 0;
masterPtr->ditherY = y + height;
}
else
{
if
(x <= masterPtr->ditherX) {
masterPtr->ditherX = x + width;
if
(masterPtr->ditherX >= masterPtr->width) {
masterPtr->ditherX = 0;
masterPtr->ditherY++;
}
}
}
}
}
static
void
DitherInstance(instancePtr, xStart, yStart, width, height)
PhotoInstance *instancePtr;
int
xStart, yStart;
int
width, height;
{
PhotoMaster *masterPtr;
ColorTable *colorPtr;
XImage *imagePtr;
int
nLines, bigEndian;
int
i, c, x, y;
int
xEnd, yEnd;
int
bitsPerPixel, bytesPerLine, lineLength;
unsigned
char
*srcLinePtr, *srcPtr;
schar *errLinePtr, *errPtr;
unsigned
char
*destBytePtr, *dstLinePtr;
pixel *destLongPtr;
pixel firstBit, word, mask;
int
col[3];
int
doDithering = 1;
colorPtr = instancePtr->colorTablePtr;
masterPtr = instancePtr->masterPtr;
if
((colorPtr->visualInfo.
class
== DirectColor)
|| (colorPtr->visualInfo.
class
== TrueColor)) {
int
nRed, nGreen, nBlue, result;
result =
sscanf
(colorPtr->id.palette,
"%d/%d/%d"
, &nRed,
&nGreen, &nBlue);
if
((nRed >= 256)
&& ((result == 1) || ((nGreen >= 256) && (nBlue >= 256)))) {
doDithering = 0;
}
}
nLines = (MAX_PIXELS + width - 1) / width;
if
(nLines < 1) {
nLines = 1;
}
if
(nLines > height ) {
nLines = height;
}
imagePtr = instancePtr->imagePtr;
if
(imagePtr == NULL) {
return
;
}
bitsPerPixel = imagePtr->bits_per_pixel;
bytesPerLine = ((bitsPerPixel * width + 31) >> 3) & ~3;
imagePtr->width = width;
imagePtr->height = nLines;
imagePtr->bytes_per_line = bytesPerLine;
imagePtr->data = (
char
*) ckalloc((unsigned) (imagePtr->bytes_per_line * nLines));
bigEndian = imagePtr->bitmap_bit_order == MSBFirst;
firstBit = bigEndian? (1 << (imagePtr->bitmap_unit - 1)): 1;
lineLength = masterPtr->width * 3;
srcLinePtr = masterPtr->pix32 + (yStart * masterPtr->width + xStart) * 4;
errLinePtr = instancePtr->error + yStart * lineLength + xStart * 3;
xEnd = xStart + width;
for
(; height > 0; height -= nLines) {
if
(nLines > height) {
nLines = height;
}
dstLinePtr = (unsigned
char
*) imagePtr->data;
yEnd = yStart + nLines;
for
(y = yStart; y < yEnd; ++y) {
srcPtr = srcLinePtr;
errPtr = errLinePtr;
destBytePtr = dstLinePtr;
destLongPtr = (pixel *) dstLinePtr;
if
(colorPtr->flags & COLOR_WINDOW) {
for
(x = xStart; x < xEnd; ++x) {
if
(doDithering) {
for
(i = 0; i < 3; ++i) {
c = (x > 0) ? errPtr[-3] * 7: 0;
if
(y > 0) {
if
(x > 0) {
c += errPtr[-lineLength-3];
}
c += errPtr[-lineLength] * 5;
if
((x + 1) < masterPtr->width) {
c += errPtr[-lineLength+3] * 3;
}
}
c = ((c + 2056) >> 4) - 128 + *srcPtr++;
if
(c < 0) {
c = 0;
}
else
if
(c > 255) {
c = 255;
}
col[i] = colorPtr->colorQuant[i][c];
*errPtr++ = c - col[i];
}
}
else
{
col[0] = *srcPtr++;
col[1] = *srcPtr++;
col[2] = *srcPtr++;
}
srcPtr++;
i = colorPtr->redValues[col[0]]
+ colorPtr->greenValues[col[1]]
+ colorPtr->blueValues[col[2]];
if
(colorPtr->flags & MAP_COLORS) {
i = colorPtr->pixelMap[i];
}
switch
(bitsPerPixel) {
case
NBBY:
*destBytePtr++ = i;
break
;
#ifndef __WIN32__
case
NBBY *
sizeof
(pixel):
*destLongPtr++ = i;
break
;
#endif
default
:
XPutPixel(imagePtr, x - xStart, y - yStart,
(unsigned) i);
}
}
}
else
if
(bitsPerPixel > 1) {
for
(x = xStart; x < xEnd; ++x) {
c = (x > 0) ? errPtr[-1] * 7: 0;
if
(y > 0) {
if
(x > 0) {
c += errPtr[-lineLength-1];
}
c += errPtr[-lineLength] * 5;
if
(x + 1 < masterPtr->width) {
c += errPtr[-lineLength+1] * 3;
}
}
c = ((c + 2056) >> 4) - 128;
if
((masterPtr->flags & COLOR_IMAGE) == 0) {
c += srcPtr[0];
}
else
{
c += (unsigned)(srcPtr[0] * 11 + srcPtr[1] * 16
+ srcPtr[2] * 5 + 16) >> 5;
}
srcPtr += 4;
if
(c < 0) {
c = 0;
}
else
if
(c > 255) {
c = 255;
}
i = colorPtr->colorQuant[0][c];
*errPtr++ = c - i;
i = colorPtr->redValues[i];
switch
(bitsPerPixel) {
case
NBBY:
*destBytePtr++ = i;
break
;
#ifndef __WIN32__
case
NBBY *
sizeof
(pixel):
*destLongPtr++ = i;
break
;
#endif
default
:
XPutPixel(imagePtr, x - xStart, y - yStart,
(unsigned) i);
}
}
}
else
{
word = 0;
mask = firstBit;
for
(x = xStart; x < xEnd; ++x) {
if
(mask == 0) {
*destLongPtr++ = word;
mask = firstBit;
word = 0;
}
c = (x > 0) ? errPtr[-1] * 7: 0;
if
(y > 0) {
if
(x > 0) {
c += errPtr[-lineLength-1];
}
c += errPtr[-lineLength] * 5;
if
(x + 1 < masterPtr->width) {
c += errPtr[-lineLength+1] * 3;
}
}
c = ((c + 2056) >> 4) - 128;
if
((masterPtr->flags & COLOR_IMAGE) == 0) {
c += srcPtr[0];
}
else
{
c += (unsigned)(srcPtr[0] * 11 + srcPtr[1] * 16
+ srcPtr[2] * 5 + 16) >> 5;
}
srcPtr += 4;
if
(c < 0) {
c = 0;
}
else
if
(c > 255) {
c = 255;
}
if
(c >= 128) {
word |= mask;
*errPtr++ = c - 255;
}
else
{
*errPtr++ = c;
}
mask = bigEndian? (mask >> 1): (mask << 1);
}
*destLongPtr = word;
}
srcLinePtr += masterPtr->width * 4;
errLinePtr += lineLength;
dstLinePtr += bytesPerLine;
}
TkPutImage(colorPtr->pixelMap, colorPtr->numColors,
instancePtr->display, instancePtr->pixels,
instancePtr->gc, imagePtr, 0, 0, xStart, yStart,
(unsigned) width, (unsigned) nLines);
yStart = yEnd;
}
ckfree(imagePtr->data);
imagePtr->data = NULL;
}
void
Tk_PhotoBlank(handle)
Tk_PhotoHandle handle;
{
PhotoMaster *masterPtr;
PhotoInstance *instancePtr;
masterPtr = (PhotoMaster *) handle;
masterPtr->ditherX = masterPtr->ditherY = 0;
masterPtr->flags = 0;
if
(masterPtr->validRegion != NULL) {
TkDestroyRegion(masterPtr->validRegion);
}
masterPtr->validRegion = TkCreateRegion();
memset
((
VOID
*) masterPtr->pix32, 0,
(
size_t
) (masterPtr->width * masterPtr->height * 4));
for
(instancePtr = masterPtr->instancePtr; instancePtr != NULL;
instancePtr = instancePtr->nextPtr) {
if
(instancePtr->error) {
memset
((
VOID
*) instancePtr->error, 0,
(
size_t
) (masterPtr->width * masterPtr->height
* 3 *
sizeof
(schar)));
}
}
Tk_ImageChanged(masterPtr->tkMaster, 0, 0, masterPtr->width,
masterPtr->height, masterPtr->width, masterPtr->height);
}
void
Tk_PhotoExpand(handle, width, height)
Tk_PhotoHandle handle;
int
width, height;
{
PhotoMaster *masterPtr;
masterPtr = (PhotoMaster *) handle;
if
(width <= masterPtr->width) {
width = masterPtr->width;
}
if
(height <= masterPtr->height) {
height = masterPtr->height;
}
if
((width != masterPtr->width) || (height != masterPtr->height)) {
if
(ImgPhotoSetSize(masterPtr, MAX(width, masterPtr->width),
MAX(height, masterPtr->height)) == TCL_ERROR) {
panic(TK_PHOTO_ALLOC_FAILURE_MESSAGE);
}
Tk_ImageChanged(masterPtr->tkMaster, 0, 0, 0, 0, masterPtr->width,
masterPtr->height);
}
}
void
Tk_PhotoGetSize(handle, widthPtr, heightPtr)
Tk_PhotoHandle handle;
int
*widthPtr, *heightPtr;
{
PhotoMaster *masterPtr;
masterPtr = (PhotoMaster *) handle;
*widthPtr = masterPtr->width;
*heightPtr = masterPtr->height;
}
void
Tk_PhotoSetSize(handle, width, height)
Tk_PhotoHandle handle;
int
width, height;
{
PhotoMaster *masterPtr;
masterPtr = (PhotoMaster *) handle;
masterPtr->userWidth = width;
masterPtr->userHeight = height;
if
(ImgPhotoSetSize(masterPtr, ((width > 0) ? width: masterPtr->width),
((height > 0) ? height: masterPtr->height)) == TCL_ERROR) {
panic(TK_PHOTO_ALLOC_FAILURE_MESSAGE);
}
Tk_ImageChanged(masterPtr->tkMaster, 0, 0, 0, 0,
masterPtr->width, masterPtr->height);
}
TkRegion
TkPhotoGetValidRegion(handle)
Tk_PhotoHandle handle;
{
PhotoMaster *masterPtr;
masterPtr = (PhotoMaster *) handle;
return
masterPtr->validRegion;
}
static
char
*
ImgGetPhoto(masterPtr, blockPtr, optPtr)
PhotoMaster *masterPtr;
Tk_PhotoImageBlock *blockPtr;
struct
SubcommandOptions *optPtr;
{
unsigned
char
*pixelPtr;
int
x, y, greenOffset, blueOffset, alphaOffset;
Tk_PhotoGetImage((Tk_PhotoHandle) masterPtr, blockPtr);
blockPtr->pixelPtr += optPtr->fromY * blockPtr->pitch
+ optPtr->fromX * blockPtr->pixelSize;
blockPtr->width = optPtr->fromX2 - optPtr->fromX;
blockPtr->height = optPtr->fromY2 - optPtr->fromY;
if
(!(masterPtr->flags & COLOR_IMAGE) &&
(!(optPtr->options & OPT_BACKGROUND)
|| ((optPtr->background->red == optPtr->background->green)
&& (optPtr->background->red == optPtr->background->blue)))) {
blockPtr->offset[0] = blockPtr->offset[1] =
blockPtr->offset[2];
}
alphaOffset = 0;
for
(y = 0; y < blockPtr->height; y++) {
pixelPtr = blockPtr->pixelPtr + (y * blockPtr->pitch)
+ blockPtr->pixelSize - 1;
for
(x = 0; x < blockPtr->width; x++) {
if
(*pixelPtr != 255) {
alphaOffset = 3;
break
;
}
pixelPtr += blockPtr->pixelSize;
}
if
(alphaOffset) {
break
;
}
}
if
(!alphaOffset) {
blockPtr->pixelPtr--;
blockPtr->offset[0]++;
blockPtr->offset[1]++;
blockPtr->offset[2]++;
}
greenOffset = blockPtr->offset[1] - blockPtr->offset[0];
blueOffset = blockPtr->offset[2] - blockPtr->offset[0];
if
(((optPtr->options & OPT_BACKGROUND) && alphaOffset) ||
((optPtr->options & OPT_GRAYSCALE) && (greenOffset || blueOffset))) {
int
newPixelSize,x,y;
unsigned
char
*srcPtr, *destPtr;
char
*data;
newPixelSize = (!(optPtr->options & OPT_BACKGROUND) && alphaOffset) ? 2 : 1;
if
((greenOffset || blueOffset) && !(optPtr->options & OPT_GRAYSCALE)) {
newPixelSize += 2;
}
data = ckalloc((unsigned
int
) (newPixelSize *
blockPtr->width * blockPtr->height));
srcPtr = blockPtr->pixelPtr + blockPtr->offset[0];
destPtr = (unsigned
char
*) data;
if
(!greenOffset && !blueOffset) {
for
(y = blockPtr->height; y > 0; y--) {
for
(x = blockPtr->width; x > 0; x--) {
*destPtr = *srcPtr;
srcPtr += blockPtr->pixelSize;
destPtr += newPixelSize;
}
srcPtr += blockPtr->pitch - (blockPtr->width * blockPtr->pixelSize);
}
}
else
if
(optPtr->options & OPT_GRAYSCALE) {
for
(y = blockPtr->height; y > 0; y--) {
for
(x = blockPtr->width; x > 0; x--) {
*destPtr = (unsigned
char
) ((srcPtr[0] * 11 + srcPtr[1] * 16
+ srcPtr[2] * 5 + 16) >> 5);
srcPtr += blockPtr->pixelSize;
destPtr += newPixelSize;
}
srcPtr += blockPtr->pitch - (blockPtr->width * blockPtr->pixelSize);
}
}
else
{
for
(y = blockPtr->height; y > 0; y--) {
for
(x = blockPtr->width; x > 0; x--) {
destPtr[0] = srcPtr[0];
destPtr[1] = srcPtr[1];
destPtr[2] = srcPtr[2];
srcPtr += blockPtr->pixelSize;
destPtr += newPixelSize;
}
srcPtr += blockPtr->pitch - (blockPtr->width * blockPtr->pixelSize);
}
}
srcPtr = blockPtr->pixelPtr + alphaOffset;
destPtr = (unsigned
char
*) data;
if
(!alphaOffset) {
}
else
if
(optPtr->options & OPT_BACKGROUND) {
if
(newPixelSize > 2) {
int
red = optPtr->background->red>>8;
int
green = optPtr->background->green>>8;
int
blue = optPtr->background->blue>>8;
for
(y = blockPtr->height; y > 0; y--) {
for
(x = blockPtr->width; x > 0; x--) {
destPtr[0] += (unsigned
char
) (((255 - *srcPtr) *
(red-destPtr[0])) / 255);
destPtr[1] += (unsigned
char
) (((255 - *srcPtr) *
(green-destPtr[1])) / 255);
destPtr[2] += (unsigned
char
) (((255 - *srcPtr) *
(blue-destPtr[2])) / 255);
srcPtr += blockPtr->pixelSize;
destPtr += newPixelSize;
}
srcPtr += blockPtr->pitch - (blockPtr->width * blockPtr->pixelSize);
}
}
else
{
int
gray = (unsigned
char
) (((optPtr->background->red>>8) * 11
+ (optPtr->background->green>>8) * 16
+ (optPtr->background->blue>>8) * 5 + 16) >> 5);
for
(y = blockPtr->height; y > 0; y--) {
for
(x = blockPtr->width; x > 0; x--) {
destPtr[0] += ((255 - *srcPtr) *
(gray-destPtr[0])) / 255;
srcPtr += blockPtr->pixelSize;
destPtr += newPixelSize;
}
srcPtr += blockPtr->pitch - (blockPtr->width * blockPtr->pixelSize);
}
}
}
else
{
destPtr += newPixelSize-1;
for
(y = blockPtr->height; y > 0; y--) {
for
(x = blockPtr->width; x > 0; x--) {
*destPtr = *srcPtr;
srcPtr += blockPtr->pixelSize;
destPtr += newPixelSize;
}
srcPtr += blockPtr->pitch - (blockPtr->width * blockPtr->pixelSize);
}
}
blockPtr->pixelPtr = (unsigned
char
*) data;
blockPtr->pixelSize = newPixelSize;
blockPtr->pitch = newPixelSize * blockPtr->width;
blockPtr->offset[0] = 0;
if
(newPixelSize>2) {
blockPtr->offset[1]= 1;
blockPtr->offset[2]= 2;
}
else
{
blockPtr->offset[1]= 0;
blockPtr->offset[2]= 0;
}
return
data;
}
return
NULL;
}
static
int
ImgStringWrite(interp, formatString, blockPtr)
Tcl_Interp *interp;
Tcl_Obj *formatString;
Tk_PhotoImageBlock *blockPtr;
{
int
row,col;
char
*line, *linePtr;
unsigned
char
*pixelPtr;
int
greenOffset, blueOffset;
Tcl_DString data;
greenOffset = blockPtr->offset[1] - blockPtr->offset[0];
blueOffset = blockPtr->offset[2] - blockPtr->offset[0];
Tcl_DStringInit(&data);
if
((blockPtr->width > 0) && (blockPtr->height > 0)) {
line = (
char
*) ckalloc((unsigned
int
) ((8 * blockPtr->width) + 2));
for
(row=0; row<blockPtr->height; row++) {
pixelPtr = blockPtr->pixelPtr + blockPtr->offset[0] +
row * blockPtr->pitch;
linePtr = line;
for
(col=0; col<blockPtr->width; col++) {
sprintf
(linePtr,
" #%02x%02x%02x"
, *pixelPtr,
pixelPtr[greenOffset], pixelPtr[blueOffset]);
pixelPtr += blockPtr->pixelSize;
linePtr += 8;
}
Tcl_DStringAppendElement(&data, line+1);
}
ckfree (line);
}
Tcl_DStringResult(interp, &data);
return
TCL_OK;
}
int
Tk_PhotoGetImage(handle, blockPtr)
Tk_PhotoHandle handle;
Tk_PhotoImageBlock *blockPtr;
{
PhotoMaster *masterPtr;
masterPtr = (PhotoMaster *) handle;
blockPtr->pixelPtr = masterPtr->pix32;
blockPtr->width = masterPtr->width;
blockPtr->height = masterPtr->height;
blockPtr->pitch = masterPtr->width * 4;
blockPtr->pixelSize = 4;
blockPtr->offset[0] = 0;
blockPtr->offset[1] = 1;
blockPtr->offset[2] = 2;
blockPtr->offset[3] = 3;
return
1;
}
typedef
struct
OptionAssocData {
struct
OptionAssocData *nextPtr;
Tcl_ObjCmdProc *command;
char
name[1];
} OptionAssocData;
static
Tcl_ObjCmdProc *
PhotoOptionFind(interp, obj)
Tcl_Interp *interp;
Tcl_Obj *obj;
{
size_t
length;
char
*name = Tcl_GetStringFromObj(obj, (
int
*) &length);
OptionAssocData *list;
char
*prevname = NULL;
Tcl_ObjCmdProc *proc = (Tcl_ObjCmdProc *) NULL;
list = (OptionAssocData *) Tcl_GetAssocData(interp,
"photoOption"
,
(Tcl_InterpDeleteProc **) NULL);
while
(list != (OptionAssocData *) NULL) {
if
(
strncmp
(name, list->name, length) == 0) {
if
(proc != (Tcl_ObjCmdProc *) NULL) {
Tcl_ResetResult(interp);
Tcl_AppendResult(interp,
"ambiguous option \""
, name,
"\": must be "
, prevname, (
char
*) NULL);
while
(list->nextPtr != (OptionAssocData *) NULL) {
Tcl_AppendResult(interp, prevname,
", "
,(
char
*) NULL);
list = list->nextPtr;
prevname = list->name;
}
Tcl_AppendResult(interp,
", or"
, prevname, (
char
*) NULL);
return
(Tcl_ObjCmdProc *) NULL;
}
proc = list->command;
prevname = list->name;
}
list = list->nextPtr;
}
if
(proc != (Tcl_ObjCmdProc *) NULL) {
Tcl_ResetResult(interp);
}
return
proc;
}
static
void
PhotoOptionCleanupProc(clientData, interp)
ClientData clientData;
Tcl_Interp *interp;
{
OptionAssocData *list = (OptionAssocData *) clientData;
OptionAssocData *ptr;
while
(list != NULL) {
list = (ptr = list)->nextPtr;
ckfree((
char
*) ptr);
}
}
void
Tk_CreatePhotoOption(interp, name, proc)
Tcl_Interp *interp;
CONST
char
*name;
Tcl_ObjCmdProc *proc;
{
OptionAssocData *typePtr2, *prevPtr, *ptr;
OptionAssocData *list;
list = (OptionAssocData *) Tcl_GetAssocData(interp,
"photoOption"
,
(Tcl_InterpDeleteProc **) NULL);
for
(typePtr2 = list, prevPtr = NULL; typePtr2 != NULL;
prevPtr = typePtr2, typePtr2 = typePtr2->nextPtr) {
if
(
strcmp
(typePtr2->name, name) == 0) {
if
(prevPtr == NULL) {
list = typePtr2->nextPtr;
}
else
{
prevPtr->nextPtr = typePtr2->nextPtr;
}
ckfree((
char
*) typePtr2);
break
;
}
}
ptr = (OptionAssocData *) ckalloc(
sizeof
(OptionAssocData) +
strlen
(name));
strcpy
(&(ptr->name[0]), name);
ptr->command = proc;
ptr->nextPtr = list;
Tcl_SetAssocData(interp,
"photoOption"
, PhotoOptionCleanupProc,
(ClientData) ptr);
}
static
int
ImgPhotoPostscript(clientData, interp, tkwin, psInfo,
x, y, width, height, prepass)
ClientData clientData;
Tcl_Interp *interp;
Tk_Window tkwin;
Tk_PostscriptInfo psInfo;
int
x, y;
int
width, height;
int
prepass;
{
Tk_PhotoImageBlock block;
Tk_PhotoGetImage((Tk_PhotoHandle) clientData, &block);
block.pixelPtr += y * block.pitch + x * block.pixelSize;
return
Tk_PostscriptPhoto(interp, &block, psInfo, width, height);
}
void
Tk_PhotoPutBlock_NoComposite(handle, blockPtr, x, y, width, height)
Tk_PhotoHandle handle;
Tk_PhotoImageBlock *blockPtr;
int
x, y, width, height;
{
Tk_PhotoPutBlock(handle, blockPtr, x, y, width, height,
TK_PHOTO_COMPOSITE_OVERLAY);
}
void
Tk_PhotoPutZoomedBlock_NoComposite(handle, blockPtr, x, y, width, height,
zoomX, zoomY, subsampleX, subsampleY)
Tk_PhotoHandle handle;
Tk_PhotoImageBlock *blockPtr;
int
x, y, width, height, zoomX, zoomY, subsampleX, subsampleY;
{
Tk_PhotoPutZoomedBlock(handle, blockPtr, x, y, width, height,
zoomX, zoomY, subsampleX, subsampleY, TK_PHOTO_COMPOSITE_OVERLAY);
}