Tk Source Code

View Ticket
Login
Ticket UUID: 03fcf233e173d6c2f572e6dcdfc6dfee4a83566a
Title: Apply zoom before subsample when copying photos
Type: Bug Version: 8.6.11, 9.0.3
Submitter: jal_frezie Created on: 2021-07-06 13:42:32
Subsystem: 41. Photo Images Assigned To: nobody
Priority: 5 Medium Severity: Cosmetic
Status: Open Last Modified: 2026-04-03 11:19:10
Resolution: None Closed By: nobody
    Closed on:
Description:
When you copy a photo image and specify both zoom and subsample, the subsample is applied first, resulting in loss of detail. For instance, if the first row of pixels have values abcdefgh... and the image is copied with -zoom 2 -subsample 3, the first line of the resulting image will be aaddgg... whereas if the zoom were applied first to create a virtual image starting aabbccdd... then that could be subsampled to give an image starting abdegh..., thus preserving more pixels from the original.

Fixing this would also go some way to addressing https://core.tcl-lang.org/tk/tktview/747588ffffffffffffff but could. be coded without floating-point variables. Unless anyone can think of a good reason not to do it, I will try to implement this as a patch to Tk_PhotoPutZoomedBlock
User Comments: jal_frezie added on 2021-07-06 17:43:55:
/Users/jaspert/Build/TclTk/Tk/generic/tkImgPhoto.c.patch

607c607
< 	    width = options.fromX2 - options.fromX;
---
> 	    width = (options.fromX2 - options.fromX) * options.zoomX;
615c615
< 	    options.toX2 = options.toX + width * options.zoomX;
---
> 	    options.toX2 = options.toX + width;
617c617
< 	    height = options.fromY2 - options.fromY;
---
> 	    height = (options.fromY2 - options.fromY) * options.zoomY;
627c627
< 	    options.toY2 = options.toY + height * options.zoomY;
---
> 	    options.toY2 = options.toY + height;
3171c3171
<     int pitch, xRepeat, yRepeat, blockXSkip, blockYSkip, sourceIsSimplePhoto;
---
>     int pitch, blockXSkip, blockYSkip, sourceIsSimplePhoto;
3295,3296c3295,3296
<     blockXSkip = subsampleX * sourceBlock.pixelSize;
<     blockYSkip = subsampleY * sourceBlock.pitch;
---
>     blockXSkip = sourceBlock.pixelSize;
>     blockYSkip = sourceBlock.pitch;
3298c3298
< 	blockWid = ((sourceBlock.width + subsampleX - 1) / subsampleX) * zoomX;
---
> 	blockWid = (sourceBlock.width * zoomX + subsampleX - 1) / subsampleX;
3302c3302
< 	blockWid = ((sourceBlock.width - subsampleX - 1) / -subsampleX) * zoomX;
---
> 	blockWid = (sourceBlock.width * zoomX - subsampleX - 1) / -subsampleX;
3305c3305
< 	blockHt = ((sourceBlock.height + subsampleY - 1) / subsampleY) * zoomY;
---
> 	blockHt = (sourceBlock.height * zoomY + subsampleY - 1) / subsampleY;
3309c3309
< 	blockHt = ((sourceBlock.height - subsampleY - 1) / -subsampleY) * zoomY;
---
> 	blockHt = (sourceBlock.height * zoomY - subsampleY - 1) / -subsampleY;
3329d3328
< 	yRepeat = zoomY;
3330a3330
> 	int yProgress = 0;
3337,3338c3337,3338
< 		for (; wCopy > 0; wCopy -= zoomX) {
< 		    for (xRepeat = MIN(wCopy, zoomX); xRepeat > 0; xRepeat--) {
---
> 		int xProgress = 0;
> 		for (; wCopy > 0; wCopy--) {
3350,3353c3350
< 			    continue;
<  			}
< 
< 			if (compRule==TK_PHOTO_COMPOSITE_SET || !destPtr[3]) {
---
>  			} else if (compRule==TK_PHOTO_COMPOSITE_SET || !destPtr[3]) {
3381,3382c3378
< 		    }
< 		    srcPtr += blockXSkip;
---
> 			srcPtr = srcLinePtr + (((++xProgress)*subsampleX)/zoomX)*blockXSkip;
3386,3390c3382
< 	    yRepeat--;
< 	    if (yRepeat <= 0) {
< 		srcLinePtr += blockYSkip;
< 		yRepeat = zoomY;
< 	    }
---
> 	    srcLinePtr = srcOrigPtr + (((++yProgress)*subsampleY)/zoomY)*blockYSkip;

...does the trick

Attachments: