circle-map/circle_map.py
2021-10-31 13:25:56 +11:00

88 lines
2.8 KiB
Python
Executable File

#!/usr/bin/env python
# MIT License
# Copyright (c) 2021 ryuku (ryuku@ennui.software - https://git.ennui.software/)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import math
import string
from gimpfu import *
from array import array
import sys
def python_circle_map(image, layer):
pdb.gimp_image_undo_group_start(image)
pdb.gimp_context_push()
width = min(layer.width, layer.height)
pdb.gimp_layer_scale(layer,width,width,FALSE)
if not pdb.gimp_drawable_has_alpha(layer): pdb.gimp_layer_add_alpha(layer)
midx = width / 2.0
midy = width / 2.0
new_layer = pdb.gimp_layer_new(image, width, width, RGBA_IMAGE, 'circle map', 100, LAYER_MODE_NORMAL)
pdb.gimp_image_insert_layer(image, new_layer, None, 0)
pixel_rgn_orig = layer.get_pixel_rgn(0, 0, width, width)
pixel_rgn = new_layer.get_pixel_rgn(0, 0, width, width)
orig_buf = pixel_rgn_orig[0 : width, 0 : width]
buf = ['\0' for x in range(0, width * width * 4)]
x = 0
y = 0
while x < int(width):
while y < int(width):
mx = (x - midx) / midx
my = (y - midy) / midy
nx = mx * (1 - (my * my) / 2) ** 0.5
ny = my * (1 - (mx * mx) / 2) ** 0.5
nx = int(nx * midx + midx)
ny = int(ny * midy + midy)
obi = (x + width * y) * 4
bi = (nx + width * ny) * 4
buf[bi:bi + 4] = orig_buf[obi:obi + 4]
y += 1
x += 1
y = 0
pdb.gimp_progress_update(float(x) / width)
pixel_rgn[0 : width, 0 : width] = ''.join(buf)
pdb.gimp_drawable_update(new_layer, 0, 0, width, width)
pdb.gimp_context_pop()
pdb.gimp_image_undo_group_end(image)
pdb.gimp_displays_flush()
register(
"python-plug-in-circle-map",
"square image to circle",
"square image to circle",
"Ryuku (ryuku@ennui.software - https://git.ennui.software/)",
"Ryuku",
"2021",
"<Image>/Filters/Misc/Circle map...",
"RGB*, GRAY*",
[],
[],
python_circle_map)
main()