| Description: |
OriginalBugID: 4268 RFE
Version: 8.3
SubmitDate: '2000-02-29'
LastModified: '2000-04-14'
Severity: CRIT
Status: Closed
Submitter: techsupp
ChangedBy: hobbs
RelatedBugIDs: 5030
OS: Windows 98
Machine: Pentium // 300Mhz 32 MB RAM 24 bit color screen
ClosedDate: '2000-04-14'
Name:
Alberto Biancardi
Comments:
The Postscript created handles transparency (due to a bug in some
hp printers a pixel is transparent when alpha is equal to zero or one)
The Postscript created is Level 2 only, but I think that handling
transparencies is a big bonus.
On the C code side.
There is a kind of deadlock owing to TkPostscriptInfo and Image
structures being opaque outside their respective files; I did not
change anything, but I had to make the decision of passsing
the masterData pointer to the postscriptProc and then bounce
back to the tkImgPhoto.c after getting the colorLevel info ...
I tried my best to test the code, but, even if I need to run wish
on Windows, I can only compile on linux.
Anyway I think my patch works on other platforms as well ...
ReproducibleScript:
cd $tk_library/images
pack [canvas .c -width 600 -height 800]
.c create image 300 400 -image \
[image create photo -file pwrdLogo75.gif]
.c postscript -file test.ps
ObservedBehavior:
Wish crashes because XGetImage is not implemented.
DesiredBehavior:
In my opinion using XGetImage is not necessary, it is not
portable and links the color level of the output to the visual
of the canvas (which is something unusual).
By looking at the sources I think that most of the neccessary changes
are already in place: any image type should provide its own
image-to-postscript function.
Moreover it would benefit from working directly on the master data
avoiding any dithering or, at any rate, letting the postscript
device do whatever it thinks optimal.
Now, what I do not understand is why the function Tk_PostscriptImage
does not check whether a Tk_ImagePostscriptProc is defined or not
for the current image.
Secondly, I would define a TkPostscriptPhoto which specialises
postscript generation for photo images.
Patch:
*** tkCanvPs.c.origTue Dec 14 07:52:26 1999
--- tkCanvPs.cMon Feb 28 11:26:33 2000
***************
*** 1681,1683 ****
--- 1681,1856 ----
ckfree((char *) cdata.colors);
return TCL_OK;
}
+
+
+
+
+ /*
+ *--------------------------------------------------------------
+ *
+ * TkPostscriptPhoto --
+ *
+ *This procedure is called to output the contents of a
+ *photo image in Postscript by calling the AppendPhotoPsData
+ * function.
+ *
+ * Results:
+ *Returns a standard Tcl return value.
+ *
+ * Side effects:
+ *None.
+ *
+ *--------------------------------------------------------------
+ */
+
+ int
+ TkPostscriptPhoto(clientData, interp, tkwin, psInfo,
+ x, y, width, height, prepass)
+ ClientData clientData; /* photo image pointer*/
+ Tcl_Interp *interp;
+ Tk_Window tkwin;
+ Tk_PostscriptInfo psInfo;/* postscript info */
+ int x, y;/* First pixel to output */
+ int width, height;/* Width and height of area */
+ int prepass;
+ {
+
+ TkPostscriptInfo *psInfoPtr = (TkPostscriptInfo *) psInfo;
+ static int codeIncluded = 0;
+ int level = psInfoPtr->colorLevel;
+
+
+ if (psInfoPtr->prepass) {
+ codeIncluded = 0;
+ return TCL_OK;
+ }
+
+
+ /*
+ * Define the "colTrImage" function, which is a modified version
+ * of the original "transparentimage" function posted
+ * by [email protected] (Ian Kemmish) to comp.lang.postscript.
+ */
+
+ if( !codeIncluded && level!=0) {
+ codeIncluded = !0;
+ Tcl_AppendResult( interp,
+ "/TkPhoto { \n",
+ " gsave \n",
+ " 32 dict begin \n",
+ " /tinteger exch def \n",
+ " /transparent 1 string def \n",
+ " transparent 0 tinteger put \n",
+ " /olddict exch def \n",
+ " olddict /DataSource get dup type /filetype ne { \n",
+ " olddict /DataSource 3 -1 roll \n",
+ " 0 () /SubFileDecode filter put \n",
+ " } { \n",
+ " pop \n",
+ " } ifelse \n",
+ " /newdict olddict maxlength dict def \n",
+ " olddict newdict copy pop \n",
+ " /w newdict /Width get def \n",
+ " /crpp newdict /Decode get length 2 idiv def \n",
+ " /str w string def \n",
+ " /pix w crpp mul string def \n",
+ " /substrlen 2 w log 2 log div floor exp cvi def \n",
+ " /substrs [ \n",
+ " { \n",
+ " substrlen string \n",
+ " 0 1 substrlen 1 sub { \n",
+ " 1 index exch tinteger put \n",
+ " } for \n",
+ " /substrlen substrlen 2 idiv def \n",
+ " substrlen 0 eq {exit} if \n",
+ " } loop \n",
+ " ] def \n",
+ " /h newdict /Height get def \n",
+ " 1 w div 1 h div matrix scale \n",
+ " olddict /ImageMatrix get exch matrix concatmatrix \n",
+ " matrix invertmatrix concat \n",
+ " newdict /Height 1 put \n",
+ " newdict /DataSource pix put \n",
+ " /mat [w 0 0 h 0 0] def \n",
+ " newdict /ImageMatrix mat put \n",
+ " 0 1 h 1 sub { \n",
+ " mat 5 3 -1 roll neg put \n",
+ " olddict /DataSource get str readstring pop pop \n",
+ " /tail str def \n",
+ " /x 0 def \n",
+ " olddict /DataSource get pix readstring pop pop \n",
+ " { \n",
+ " tail transparent search dup /done exch not def \n",
+ " {exch pop exch pop} if \n",
+ " /w1 exch length def \n",
+ " w1 0 ne { \n",
+ " newdict /DataSource ",
+ " pix x crpp mul w1 crpp mul getinterval put \n",
+ " newdict /Width w1 put \n",
+ " mat 4 x neg put \n",
+ " /x x w1 add def \n",
+ " newdict image \n",
+ " /tail tail w1 tail length w1 sub getinterval def \n",
+ " } if \n",
+ " done {exit} if \n",
+ " tail substrs { \n",
+ " anchorsearch {pop} if \n",
+ " } forall \n",
+ " /tail exch def \n",
+ " tail length 0 eq {exit} if \n",
+ " /x w tail length sub def \n",
+ " } loop \n",
+ " } for \n",
+ " end \n",
+ " grestore \n",
+ "} bind def \n\n\n", (char *) NULL);
+ } else if( !codeIncluded ) {
+ /*
+ * Append monochrome-only code
+ */
+ codeIncluded = !0;
+ Tcl_AppendResult( interp,
+ "/TkPhoto { \n",
+ " gsave \n",
+ " 32 dict begin \n",
+ " /dummyInteger exch def \n",
+ " /olddict exch def \n",
+ " olddict /DataSource get dup type /filetype ne { \n",
+ " olddict /DataSource 3 -1 roll \n",
+ " 0 () /SubFileDecode filter put \n",
+ " } { \n",
+ " pop \n",
+ " } ifelse \n",
+ " /newdict olddict maxlength dict def \n",
+ " olddict newdict copy pop \n",
+ " /w newdict /Width get def \n",
+ " /pix w 7 add 8 idiv string def \n",
+ " /h newdict /Height get def \n",
+ " 1 w div 1 h div matrix scale \n",
+ " olddict /ImageMatrix get exch matrix concatmatrix \n",
+ " matrix invertmatrix concat \n",
+ " newdict /Height 1 put \n",
+ " newdict /DataSource pix put \n",
+ " /mat [w 0 0 h 0 0] def \n",
+ " newdict /ImageMatrix mat put \n",
+ " 0 1 h 1 sub { \n",
+ " mat 5 3 -1 roll neg put \n",
+ " 0.000 0.000 0.000 setrgbcolor \n",
+ " olddict /DataSource get pix readstring pop pop \n",
+ " newdict /DataSource pix put \n",
+ " newdict imagemask \n",
+ " 1.000 1.000 1.000 setrgbcolor \n",
+ " olddict /DataSource get pix readstring pop pop \n",
+ " newdict /DataSource pix put \n",
+ " newdict imagemask \n",
+ " } for \n",
+ " end \n",
+ " grestore \n",
+ "} bind def \n\n\n", (char *) NULL);
+ }
+
+
+
+ return TkAppendPhotoPsData( clientData, interp,
+ x, y, width, height, level);
+ }
*** tkImage.c.origTue Dec 14 07:52:28 1999
--- tkImage.cThu Feb 24 14:54:58 2000
***************
*** 634,644 ****
--- 634,669 ----
int width, height;/* Dimensions of region to redraw. */
int prepass;
{
+ Image *imagePtr = (Image *) image;
int result;
XImage *ximage;
Pixmap pmap;
GC newGC;
XGCValues gcValues;
+
+
+ if (imagePtr->masterPtr->typePtr == NULL) {
+ /*
+ * No master for image, so nothing to display on postscript.
+ */
+
+ return TCL_OK;
+ }
+
+
+ /*
+ * Check if an image specific postscript-generation function
+ * exists; otherwise go on with generic code.
+ */
+
+ if (imagePtr->masterPtr->typePtr->postscriptProc != NULL) {
+ return (*imagePtr->masterPtr->typePtr->postscriptProc)(
+ imagePtr->masterPtr->masterData, interp, tkwin, psinfo,
+ x, y, width, height, prepass);
+ }
+
+
+
if (prepass) {
return TCL_OK;
}
*** tkImgPhoto.c.origTue Feb 8 12:31:33 2000
--- tkImgPhoto.cMon Feb 28 11:31:45 2000
***************
*** 31,37 ****
* Declaration for internal Xlib function used here:
*/
! extern _XInitImageFuncPtrs _ANSI_ARGS_((XImage *image));
/*
* A signed 8-bit integral type. If chars are unsigned and the compiler
--- 31,37 ----
* Declaration for internal Xlib function used here:
*/
! extern void _XInitImageFuncPtrs _ANSI_ARGS_((XImage *image));
/*
* A signed 8-bit integral type. If chars are unsigned and the compiler
***************
*** 301,307 ****
ImgPhotoDisplay,/* displayProc */
ImgPhotoFree,/* freeProc */
ImgPhotoDelete,/* deleteProc */
! (Tk_ImagePostscriptProc *) NULL, /* postscriptProc */
(Tk_ImageType *) NULL/* nextPtr */
};
--- 301,307 ----
ImgPhotoDisplay,/* displayProc */
ImgPhotoFree,/* freeProc */
ImgPhotoDelete,/* deleteProc */
! TkPostscriptPhoto, /* postscriptProc */
(Tk_ImageType *) NULL/* nextPtr */
};
***************
*** 5111,5114 ****
--- 5111,5411 ----
ptr->nextPtr = list;
Tcl_SetAssocData(interp, "photoOption", PhotoOptionCleanupProc,
(ClientData) ptr);
+ }
+
+
+
+
+ /*
+ *--------------------------------------------------------------
+ *
+ * TkAppendPostscriptData --
+ *
+ *This procedure is called to output the contents of a
+ *photo image in Postscript, using a format appropriate for
+ * the requested postscript color mode (i.e. one byte per pixel
+ * in gray, and three bytes per pixel in color).
+ *
+ * Results:
+ *Returns a standard Tcl return value. If an error occurs
+ *then an error message will be left in interp->result.
+ *If no error occurs, then additional Postscript will be
+ *appended to interp->result.
+ *
+ * Side effects:
+ *None.
+ *
+ *--------------------------------------------------------------
+ */
+
+
+ int
+ TkAppendPhotoPsData( clientData, interp, x, y, width, height, colorLevel)
+ ClientData clientData; /* photo image pointer*/
+ Tcl_Interp *interp;
+ int x, y;/* First pixel to output */
+ int width, height;/* Width and height of area */
+ int colorLevel;
+ {
+ PhotoMaster *masterPtr = (PhotoMaster *) clientData;
+ unsigned char *pixelPtr;
+ char buffer[256], cspace[40], decode[40];
+ int bpc;
+ int xx, yy, lineLen;
+ float red, green, blue;
+ int alpha;
+ int bytesPerLine=0, maxWidth=0;
+
+
+ /*
+ * Check that at least one row of the image can be represented
+ * with a string less than 64 KB long (this is a limit in the
+ * Postscript interpreter).
+ */
+
+ switch (colorLevel)
+ {
+ case 0: bytesPerLine = (width + 7) / 8; maxWidth = 240000; break;
+ case 1: bytesPerLine = 2 * width; maxWidth = 30000; break;
+ default: bytesPerLine = 4 * width; maxWidth = 15000; break;
+ }
+
+ if (bytesPerLine > 60000) {
+ Tcl_ResetResult(interp);
+ sprintf(buffer,
+ "Can't generate Postscript for images more than %d pixels wide",
+ maxWidth);
+ Tcl_AppendResult(interp, buffer, (char *) NULL);
+ return TCL_ERROR;
+ }
+
+
+ #if 0
+ if (colorLevel!=0) {
+ sprintf(buffer, "0 %d translate\n", height);
+ Tcl_AppendResult(interp, buffer, (char *) NULL);
+ }
+ #endif
+
+
+ switch (colorLevel) {
+ case 0:
+ strcpy( cspace, "/DeviceGray");
+ strcpy( decode, "[1 0]");
+ bpc = 1;
+ break;
+ case 1:
+ strcpy( cspace, "/DeviceGray");
+ strcpy( decode, "[0 1]");
+ bpc = 8;
+ break;
+ default:
+ strcpy( cspace, "/DeviceRGB");
+ strcpy( decode, "[0 1 0 1 0 1]");
+ bpc = 8;
+ break;
+ }
+
+
+ sprintf(buffer,
+ "\n%%%%0 -%d translate\ngsave\n%d %d scale\n",
+ height, width, height);
+
+ Tcl_AppendResult(interp, buffer,
+ cspace, " setcolorspace\n\n",
+ (char *) NULL);
+
+ sprintf(buffer,
+ " /Width %d\n /Height %d\n /BitsPerComponent %d\n",
+ width, height, bpc);
+
+ Tcl_AppendResult(interp,
+ "<<\n /ImageType 1\n", buffer,
+ " /DataSource currentfile /ASCIIHexDecode filter\n",
+ (char *) NULL);
+
+
+ sprintf(buffer,
+ " /ImageMatrix [%d 0 0 -%d 0 %d]\n", width, height, height);
+
+
+
+ /* per le immagini mono devo generare 2 set di pixel
+ * uno per i pixel bianchi e 1 x quelli neri ed usare imagemask!
+ */
+
+ Tcl_AppendResult(interp, buffer,
+ " /Decode ", decode,
+ "\n>>\n1 TkPhoto\n",
+ (char *) NULL);
+
+ for (yy = y, lineLen=0; yy < y+height; yy++) {
+ switch (colorLevel) {
+ case 0: {
+ /*
+ * Generate data for image in monochrome mode.
+ * No attempt at dithering is made--instead, just
+ * set a threshold.
+ * To handle transparecies we need to output two lines:
+ * one for the black pixels, one for the white ones.
+ */
+
+ unsigned char mask=0x80;
+ unsigned char data=0x00;
+ for (xx = x; xx< x+width; xx++) {
+ pixelPtr = masterPtr->pix24 +
+ (yy * masterPtr->width + xx) * 4;
+ red = *(pixelPtr++);
+ green = *(pixelPtr++);
+ blue = *(pixelPtr++);
+ alpha = *(pixelPtr++);
+
+ /*
+ * If pixel is than threshold, then it is black.
+ */
+
+ if ((alpha != 0) &&
+ ( 0.3086 * red + 0.6094 * green + 0.082 * blue < 128))
+ data |= mask;
+ mask >>= 1;
+ if (mask == 0) {
+ sprintf(buffer, "%02X", data);
+ Tcl_AppendResult(interp, buffer, (char *) NULL);
+ lineLen += 2;
+ if (lineLen >= 60) {
+ lineLen = 0;
+ Tcl_AppendResult(interp, "\n", (char *) NULL);
+ }
+ mask=0x80;
+ data=0x00;
+ }
+ }
+ if ((width % 8) != 0) {
+ sprintf(buffer, "%02X", data);
+ Tcl_AppendResult(interp, buffer, (char *) NULL);
+ mask=0x80;
+ data=0x00;
+ }
+
+ mask=0x80;
+ data=0x00;
+ for (xx = x; xx< x+width; xx++) {
+ pixelPtr = masterPtr->pix24 +
+ (yy * masterPtr->width + xx) * 4;
+ red = *(pixelPtr++);
+ green = *(pixelPtr++);
+ blue = *(pixelPtr++);
+ alpha = *(pixelPtr++);
+
+ /*
+ * If pixel is than threshold, then it is black.
+ */
+
+ if ((alpha != 0) &&
+ ( 0.3086 * red + 0.6094 * green + 0.082 * blue >= 128))
+ data |= mask;
+ mask >>= 1;
+ if (mask == 0) {
+ sprintf(buffer, "%02X", data);
+ Tcl_AppendResult(interp, buffer, (char *) NULL);
+ lineLen += 2;
+ if (lineLen >= 60) {
+ lineLen = 0;
+ Tcl_AppendResult(interp, "\n", (char *) NULL);
+ }
+ mask=0x80;
+ data=0x00;
+ }
+ }
+ if ((width % 8) != 0) {
+ sprintf(buffer, "%02X", data);
+ Tcl_AppendResult(interp, buffer, (char *) NULL);
+ mask=0x80;
+ data=0x00;
+ }
+ break;
+ }
+ case 1: {
+ /*
+ * Generate transparency data.
+ */
+ for (xx = x; xx < x+width; xx ++) {
+ pixelPtr = masterPtr->pix24 +
+ (yy * masterPtr->width + xx) * 4;
+ sprintf(buffer, "%02X", pixelPtr[3] | 0x01);
+ Tcl_AppendResult(interp, buffer, (char *) NULL);
+ lineLen += 2;
+ if (lineLen >= 60) {
+ lineLen = 0;
+ Tcl_AppendResult(interp, "\n", (char *) NULL);
+ }
+ }
+
+
+ /*
+ * Generate data in gray mode--in this case, take a
+ * weighted sum of the red, green, and blue values.
+ */
+
+ for (xx = x; xx < x+width; xx ++) {
+ pixelPtr = masterPtr->pix24 +
+ (yy * masterPtr->width + xx) * 4;
+ red = *(pixelPtr++);
+ green = *(pixelPtr++);
+ blue = *(pixelPtr++);
+ sprintf(buffer, "%02X", (int) floor(0.5 +
+ ( 0.3086 * red
+ + 0.6094 * green
+ + 0.0820 * blue)));
+ Tcl_AppendResult(interp, buffer, (char *) NULL);
+ lineLen += 2;
+ if (lineLen >= 60) {
+ lineLen = 0;
+ Tcl_AppendResult(interp, "\n", (char *) NULL);
+ }
+ }
+ break;
+ }
+ default: {
+ /*
+ * Generate transparency data.
+ */
+ for (xx = x; xx < x+width; xx ++) {
+ pixelPtr = masterPtr->pix24 +
+ (yy * masterPtr->width + xx) * 4;
+ sprintf(buffer, "%02X", pixelPtr[3] | 0x01);
+ Tcl_AppendResult(interp, buffer, (char *) NULL);
+ lineLen += 2;
+ if (lineLen >= 60) {
+ lineLen = 0;
+ Tcl_AppendResult(interp, "\n", (char *) NULL);
+ }
+ }
+
+
+ /*
+ * Finally, color mode. Here, just output the red, green,
+ * and blue values directly.
+ */
+
+ for (xx = x; xx < x+width; xx++) {
+ pixelPtr = masterPtr->pix24 +
+ (yy * masterPtr->width + xx) * 4;
+ sprintf(buffer, "%02X%02X%02X",
+ pixelPtr[0], pixelPtr[1], pixelPtr[2]);
+ Tcl_AppendResult(interp, buffer, (char *) NULL);
+ lineLen += 6;
+ if (lineLen >= 60) {
+ lineLen = 0;
+ Tcl_AppendResult(interp, "\n", (char *) NULL);
+ }
+ }
+ break;
+ }
+ }
+ }
+
+ Tcl_AppendResult(interp, ">\n grestore\n", (char *) NULL);
+ return TCL_OK;
+
}
*** tkInt.h.origFri Jan 21 04:54:41 2000
--- tkInt.hThu Feb 24 14:21:54 2000
***************
*** 999,1004 ****
--- 999,1009 ----
EXTERN intTk_WmCmd _ANSI_ARGS_((ClientData clientData,
Tcl_Interp *interp, int argc, char **argv));
+ EXTERN int TkAppendPhotoPsData _ANSI_ARGS_((
+ ClientData clientData, Tcl_Interp *interp,
+ int x, int y, int width, int height,
+ int colorLevel));
+
voidTkConsolePrint _ANSI_ARGS_((Tcl_Interp *interp,
int devId, char *buffer, long size));
***************
*** 1052,1057 ****
--- 1057,1067 ----
Tk_Window tkwin, Tk_PostscriptInfo psInfo,
XImage *ximage, int x, int y, int width,
int height));
+ EXTERN intTkPostscriptPhoto _ANSI_ARGS_((
+ ClientData clientData, Tcl_Interp *interp,
+ Tk_Window tkwin, Tk_PostscriptInfo psInfo,
+ int x, int y, int width, int height,
+ int prepass));
EXTERN intTkSmoothParseProc _ANSI_ARGS_((ClientData clientData,
Tcl_Interp *interp, Tk_Window tkwin,
CONST char *value, char *recordPtr, int offset));
PatchFiles:
tkInt.h tkCanvPs.c tkImage.c tkImgPhoto.c
|