3

I have to scale down images and turn them into jpegs in a browser. For this i have been using a javascript resize function called Hermite-resize

This works great. After re-sizing a few hundred images I have noticed something odd happens once every while. With some images a thin black line appears at the bottom of the scaled down jpeg image.

Normal image and one with unwanted black line

I thought this might be due to a transparent line being rendered in the scaled down version. After converting to jpeg, the line then becomes black since jpegs turn no background into a black background.

However if i put a white background behind the image before rendering it as a jpeg, then the problem still remains.

If anyone has an idea on what creates this, or how to fix it, i would much appreciate it.

A jsfiddle to clarify what i mean:

canvas.getContext("2d").fillStyle = '#FFF';
canvas.getContext("2d").fillRect(0, 0, W2, H2);
canvas.getContext("2d").putImageData(img2, 0, 0);
document.getElementById("imageid").src = canvas.toDataURL("image/jpeg");

http://jsfiddle.net/gt4r54zr/1/

I'm not sure if it's related but if i set the transparency to full (255) in the Hermite script, then the same black line appears as well:

data2[x2 + 3] = 255;// Original value: gx_a / weights_alpha;

http://jsfiddle.net/3osq1s1w/3/

2
  • 1
    Does this only happen at precise pixel values?
    – invot
    Commented Sep 14, 2015 at 17:09
  • 1
    Excellent question invot. This is actually the case. For the image in the example i put all the pixel values in a pastebin ( pastebin.com/C43FM9k8 ). I don't see much of a pattern in it though. Although i don't really understand how, rphv's answer does seem to solve my problem so i am happy :).
    – Kasper
    Commented Sep 15, 2015 at 8:48

1 Answer 1

3

The function resample_hermite() calculates these ratios:

var ratio_w_half = Math.ceil(ratio_w/2);
var ratio_h_half = Math.ceil(ratio_h/2);

which are eventually used to determine the dimensions of the re-sized image. Note the Math.ceil, which rounds (ratio_h/2) to the next greatest integer. It appears the black line is being rendered in those cases where the mantissa (i.e., everything to the right the decimal point) of (ratio_h/2) is less than 0.5. In these cases, the calculated height of the image is one pixel too large, and there's no data to fill in these pixels.

Changing

var ratio_h_half = Math.ceil(ratio_h/2);

to

var ratio_h_half = Math.round(ratio_h/2);

seems to fix the problem in your example.

3
  • 1
    Thank you rphv!! Although i have trouble understanding how, your answer does seem to solve my problem. I tried it for all the troubling pixel heights of this example image, and none caused a problem. I tried looking at the mantissa but i didn't see the pattern you described. I might have done it wrong though. (If you like you can find the values at pastebin.com/C43FM9k8 )
    – Kasper
    Commented Sep 15, 2015 at 8:53
  • 2
    Glad to help. Your data does show the pattern I identified - let me clarify. Consider (Original height / new height) / 2: in all cases, the mantissa (also significand, or "the part of the number to the right of the decimal point") is less than 0.5. This means that Math.round() and Math.ceil() will return different values - for example, Math.round(4.1666666667) = 4, but Math.ceil(4.1666666667) = 5. In these cases, the calculated size of the resized image is a little bit too large, and the extra space gets filled in with the black line.
    – rphv
    Commented Sep 15, 2015 at 17:03
  • Thank you rphv for further elaborating on this. I understand the rounding difference and how it could cause this effect. This doesn't happen for all the values with a mantissa that's less than 0.5 though. I guess there's another factor involved. However, the whole calculation in the code is a little over my head. I have tried to understand what's going on, but that in itself may have been a mistake ;). For now i'm just really happy my pictures render properly :D. Thank you for that!
    – Kasper
    Commented Sep 16, 2015 at 18:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.