Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
swift502 committed Aug 31, 2024
1 parent 3ecc247 commit af7b085
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 39 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ python convert.py equi.png --to_mercator

You can use the Converter class directly in Python. Check out the [demo script](demo.py) to see how to run conversions from code.

## Limitations

The conversion process doesn't handle longitude angles of 85+ degress well. Converted images may display issues around the top and bottom borders.

## Python package

If anyone wants to transform this into a functional, publishable package, feel free to fork the project and publish it. I don't have enough experience doing that and can't imagine many people will use this thing to make the extra effort worthwhile.
34 changes: 13 additions & 21 deletions src/cpu_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,27 @@ def clamp(value, minValue, maxValue):

def equi_to_merc(u, v):
# uv to equirectangular
lat = remap(u, 0, 1, 0, 2 * math.pi)
lon = remap(v, 0, 1, -math.pi * 0.5, math.pi * 0.5)
lon = remap(v, 0, 1, -MERC_MAX_LON, MERC_MAX_LON)

# equirectangular to mercator
x = lat
lon = clamp(lon, -MERC_MAX_LON, MERC_MAX_LON)
y = math.log(math.tan(math.pi / 4 + lon / 2))

# mercator to uv
x = remap(x, 0, 2 * math.pi, 0, 1)
y = remap(y, -math.pi, math.pi, 0, 1)

# clamp
x = clamp(x, 0, 1)
y = clamp(y, 0, 1)

return (x, y)
return (u, y)

def merc_to_equi(u, v):
# uv to mercator
lat = remap(u, 0, 1, 0, 2 * math.pi)
lon = remap(v, 0, 1, -math.pi, math.pi)

# mercator to equirectangular
x = lat
y = 2 * math.atan(math.pow(math.e, lon)) - math.pi * 0.5

# equirectangular to uv
x = remap(x, 0, 2 * math.pi, 0, 1)
y = remap(y, -math.pi * 0.5, math.pi * 0.5, 0, 1)

# clamp
x = clamp(x, 0, 1)
y = clamp(y, 0, 1)
y = remap(y, -MERC_MAX_LON, MERC_MAX_LON, 0, 1)

return (x, y)
return (u, y)

def render(image: Image.Image, conversion: CONVERSION):
if conversion == CONVERSION.TO_MERCATOR:
Expand All @@ -62,12 +47,19 @@ def render(image: Image.Image, conversion: CONVERSION):
progress = 0
for x in range(newImage.width):
for y in range(newImage.height):
# Coordinate limits
maxX = newImage.width - 1
maxY = newImage.height - 1

# Conversion
if conversion == CONVERSION.TO_MERCATOR:
# Running through a merc image, we need to sample equi
(u, v) = merc_to_equi(x / newImage.width, y / newImage.height)
(u, v) = merc_to_equi(x / maxX, y / maxY)
else:
# Running through an equi image, we need to sample merc
(u, v) = equi_to_merc(x / newImage.width, y / newImage.height)
(u, v) = equi_to_merc(x / maxX, y / maxY)

# Resample original image using converted UVs
sampleX = round(u * (image.width - 1))
sampleY = round(v * (image.height - 1))
newPixels[x, y] = pixels[sampleX, sampleY]
Expand Down
12 changes: 5 additions & 7 deletions src/shaders/fragment_to_equirectangular.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ in vec2 fragCoord;
out vec4 fragColor;
uniform sampler2D texture;

#define M_PI 3.1415926535897932384626433832795
#define M_E 2.7182818284590452353602874713527
#define M_PI 3.1415926535897932
#define M_E 2.7182818284590452
#define M_MAX 1.4844222297453324

float remap(float value, float oldMin, float oldMax, float newMin, float newMax)
{
Expand All @@ -14,18 +15,15 @@ float remap(float value, float oldMin, float oldMax, float newMin, float newMax)
vec2 equi_to_merc(float u, float v)
{
// uv to equirectangular
float lat = remap(u, 0, 1, 0, 2 * M_PI);
float lon = remap(v, 0, 1, -M_PI * 0.5, M_PI * 0.5);
float lon = remap(v, 0, 1, -M_MAX, M_MAX);

// equirectangular to mercator
float x = lat;
float y = log(tan(M_PI / 4 + lon / 2));

// mercator to uv
x = remap(x, 0, 2 * M_PI, 0, 1);
y = remap(y, -M_PI, M_PI, 0, 1);

return vec2(x, y);
return vec2(u, y);
}

void main()
Expand Down
12 changes: 5 additions & 7 deletions src/shaders/fragment_to_mercator.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ in vec2 fragCoord;
out vec4 fragColor;
uniform sampler2D texture;

#define M_PI 3.1415926535897932384626433832795
#define M_E 2.7182818284590452353602874713527
#define M_PI 3.1415926535897932
#define M_E 2.7182818284590452
#define M_MAX 1.4844222297453324

float remap(float value, float oldMin, float oldMax, float newMin, float newMax)
{
Expand All @@ -14,18 +15,15 @@ float remap(float value, float oldMin, float oldMax, float newMin, float newMax)
vec2 merc_to_equi(float u, float v)
{
// uv to mercator
float lat = remap(u, 0, 1, 0, 2 * M_PI);
float lon = remap(v, 0, 1, -M_PI, M_PI);

// mercator to equirectangular
float x = lat;
float y = 2 * atan(pow(M_E, lon)) - M_PI * 0.5;

// equirectangular to uv
x = remap(x, 0, 2 * M_PI, 0, 1);
y = remap(y, -M_PI * 0.5, M_PI * 0.5, 0, 1);
y = remap(y, -M_MAX, M_MAX, 0, 1);

return vec2(x, y);
return vec2(u, y);
}

void main()
Expand Down

0 comments on commit af7b085

Please sign in to comment.