Skip to content

Commit

Permalink
Add pasteImage and saveWebImage to Mapscript imageObj (MapServer#7015)
Browse files Browse the repository at this point in the history
Follow-up and replacement to MapServer#6932 (thanks to @jbfrd)
  • Loading branch information
geographika authored Jan 21, 2024
1 parent d1e2822 commit 38e6ad8
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/mapscript/python/tests/cases/image_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,37 @@ def testImageGetBytes(self):
assert pyimg.size == (200, 200)
assert pyimg.mode == "RGB"

def testSaveWebImage(self):
"""save an image to the imagepath folder and return its URL"""

# set the imagepath and imageurl on the MAP WEB object
self.map.web.imagepath = ""
self.map.web.imageurl = "/output/"

image = self.map.draw()
url = image.saveWebImage()
assert url.startswith("/output/")
assert url.endswith(".png")

def testPasteImage(self):
"""save an image to the imagepath folder and return its URL"""

image1 = self.map.draw()
image2 = self.map.draw()

ret = image1.pasteImage(image2, opacity=0.5, dstx=10, dsty=10)
assert ret == mapscript.MS_SUCCESS

filename = "testImagePaste.png"
fh = open(filename, "wb")
image1.write(fh)
fh.close()
if have_image:
pyimg = Image.open(filename)
assert pyimg.format == "PNG"
assert pyimg.size == (200, 200)
assert pyimg.mode == "RGB"


if __name__ == "__main__":
unittest.main()
62 changes: 62 additions & 0 deletions src/mapscript/swiginc/image.i
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,66 @@
free(buffer.data);
return size;
}

/**
Pastes another imageObj on top of this imageObj.
If optional dstx,dsty are provided then they define the position where the
image should be copied (dstx,dsty = top-left corner position).
*/
int pasteImage(imageObj *imageSrc, double opacity=1.0, int dstx=0, int dsty=0)
{
rendererVTableObj *renderer = NULL;
rasterBufferObj rb;

if (!MS_RENDERER_PLUGIN(self->format)) {
msSetError(MS_IMGERR, "PasteImage function should only be used with renderer plugin drivers.",
"imageObj::pasteImage");
return MS_FAILURE;
}

memset(&rb,0,sizeof(rasterBufferObj));

renderer = self->format->vtable;
if(MS_SUCCESS != renderer->getRasterBufferHandle(imageSrc, &rb)) {
msSetError(MS_IMGERR, "PasteImage failed to extract rasterbuffer handle",
"imageObj::pasteImage");
return MS_FAILURE;
}

if(MS_SUCCESS != renderer->mergeRasterBuffer(self, &rb, opacity, 0, 0, dstx, dsty, rb.width, rb.height)) {
msSetError(MS_IMGERR, "PasteImage failed to merge raster buffer",
"imageObj::pasteImage");
return MS_FAILURE;
}

return MS_SUCCESS;

}

/**
Writes the image to temp directory.
Returns the image URL.
*/
char *saveWebImage()
{
char *imageFile = NULL;
char *imageFilename = NULL;
char path[MS_MAXPATHLEN];
char *imageUrlFull = NULL;

imageFilename = msTmpFilename(self->format->extension);
imageFile = msBuildPath(path, self->imagepath, imageFilename);

if (msSaveImage(NULL, self, imageFile) != MS_SUCCESS) {
msSetError(MS_IMGERR, "Failed writing image to %s",
"imageObj::saveWebImage",
imageFile);
msFree(imageFilename);
return NULL;
}

imageUrlFull = msStrdup(msBuildPath(path, self->imageurl, imageFilename));
msFree(imageFilename);
return imageUrlFull;
}
}

0 comments on commit 38e6ad8

Please sign in to comment.