gdImageGd2() writes wrong chunk sizes on boundaries

When working on issue #288, I've noticed that gdImageGd2() writes wrong chunk sizes if the image dimensions are a multiple of the chunk size, albeit the chunks themselves are written as expected. The following program demonstrates the erroneous behavior:

#include "gd.h"
#include "gdtest.h"


int main()
{
    gdImagePtr im;
    unsigned char *buf;
    int size, word;

    im = gdImageCreate(64, 64);
    gdImageColorAllocate(im, 0, 0, 0);

    buf = gdImageGd2Ptr(im, 64, 1, &size);

    gdImageDestroy(im);

    word = buf[10] << 8 | buf[11];
    gdTestAssertMsg(word == 64, "chunk size is %d, but expected 64\n", word);
    word = buf[14] << 8 | buf[15];
    gdTestAssertMsg(word == 1, "x chunk count is %d, but expected 1\n", word);
    word = buf[16] << 8 | buf[17];
    gdTestAssertMsg(word == 1, "y chunk count is %d, but expected 1\n", word);
    gdTestAssertMsg(size == 5145, "file size is %d, but expected 5145\n", size);

    gdFree(buf);

    return gdNumFailures();
}

Output:

x chunk count is 2, but expected 1
y chunk count is 2, but expected 1