# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

PortSystem          1.0
PortGroup           github 1.0
PortGroup           makefile 1.0

github.setup        ilyakurdyukov jpeg-quantsmooth 1.20240129
# Change github.tarball_from to 'releases' or 'archive' next update
github.tarball_from tarball
revision            0
categories          graphics
license             LGPL-2.1
maintainers         {i0ntempest @i0ntempest} openmaintainer

description         JPEG artifacts removal based on quantization coefficients
long_description    ${name} or jpegqs tries to recover the lost precision of DCT coefficients based on\
                    a quantization table from a JPEG image. The result is saved as a JPEG image with\
                    quantization set to 1 (like a JPEG saved at 100% quality).

checksums           rmd160  13c5a1902982c248816916485e2e870c0991119d \
                    sha256  d000a34609f6ea9d524d2853eebccd4c28874903040b06bc9fca0e2a6ad35549 \
                    size    74249

compiler.openmp_version \
                    4.5
compiler.blacklist-append \
                    {macports-clang-[0-9].*}

depends_lib-append  port:libjpeg-turbo

patchfiles-append   patch-dynamic.patch

# See: https://github.com/ilyakurdyukov/jpeg-quantsmooth/pull/31
patchfiles-append   patch-Makefile.patch

post-patch {
    reinplace "s|LDFLAGS :=|LDFLAGS ?=|g" Makefile
    reinplace "s|CFLAGS :=|CFLAGS ?=|g" Makefile
    if {${os.platform} ne "darwin"} {
        reinplace "s|\.dylib|\.so|g" Makefile
    }
}

if {[string match *clang* ${configure.compiler}]} {
    configure.cflags-append \
                    -I${prefix}/include/libomp
    configure.ldflags-append \
                    -L${prefix}/lib/libomp
}

destroot {
    xinstall -m 755 ${worksrcpath}/jpegqs ${destroot}${prefix}/bin
    xinstall -m 644 ${worksrcpath}/libjpegqs.a ${destroot}${prefix}/lib
    if {${os.platform} eq "darwin"} {
        xinstall -m 755 ${worksrcpath}/libjpegqs.dylib ${destroot}${prefix}/lib
    } else {
        xinstall -m 755 ${worksrcpath}/libjpegqs.so ${destroot}${prefix}/lib
    }
}

variant extensions description {Compile for Intel CPU vector extensions (SSE2, AVX2, AVX512)} {
    if {${configure.build_arch} in [list arm64 ppc ppc64]} {
        known_fail yes
        pre-fetch {
            ui_error "Vector extensions are not available for the current architecture"
            return -code error "incompatible hardware architecture"
        }
    }
    build.args-append \
                    SIMD=select
}
