# -*- 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.20260122
github.tarball_from archive
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  ccfb027de3f14b310174a4ae0b85326de8831af3 \
                    sha256  7dcbaa7d994511a03dba845aa218a25f9c8e70c0bcb0800ff61ae3d92dd50c3d \
                    size    78756

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

depends_lib-append  port:libjpeg-turbo

patchfiles-append   patch-dynamic.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)} {
    # Also supports RISC-V and Loongarch now but irrelevant on macOS
    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
}
