Repository: conan-io/conan-center-index

Issue:
[request]  jpeg-compressor 
### Package Details
  * Package Name/Version: **jpeg-compressor/latest**
  * Website: **https://github.com/richgel999/jpeg-compressor**
  * Source code: **https://github.com/richgel999/jpeg-compressor**


### Description Of The Library / Tool

C++ JPEG compression/fuzzed low-RAM JPEG decompression codec with Public Domain or Apache 2.0 license 


Discussion:
https://github.com/richgel999/jpeg-compressor/pull/14

Resolving patch:
diff --git a/recipes/jpeg-compressor/all/conanfile.py b/recipes/jpeg-compressor/all/conanfile.py
new file mode 100644
--- /dev/null
+++ b/recipes/jpeg-compressor/all/conanfile.py
@@ -0,0 +1,79 @@
+import os
+import glob
+from conans import ConanFile, tools, CMake
+from conans.errors import ConanInvalidConfiguration
+
+
+class JpegCompressorConan(ConanFile):
+    name = "jpeg-compressor"
+    description = "C++ JPEG compression/fuzzed low-RAM JPEG decompression codec with Public Domain or Apache 2.0 license"
+    homepage = "https://github.com/richgel999/jpeg-compressor"
+    topics = ("conan", "jpeg", "image", "compression", "decompression")
+    url = "https://github.com/conan-io/conan-center-index"
+    license = "Public Domain", "Apache-2.0"
+    settings = "os", "arch", "compiler", "build_type"
+    generators = "cmake"
+    exports_sources = ["CMakeLists.txt", "patches/*"]
+    options = {
+        "shared": [True, False],
+        "fPIC": [True, False],
+    }
+    default_options = {
+        "shared": False,
+        "fPIC": True,
+    }
+
+    _cmake = None
+
+    @property
+    def _source_subfolder(self):
+        return "source_subfolder"
+
+    @property
+    def _build_subfolder(self):
+        return "build_subfolder"
+
+    def config_options(self):
+        if self.settings.os == "Windows":
+            del self.options.fPIC
+
+    def configure(self):
+        if self.options.shared:
+            del self.options.fPIC
+            if self.settings.compiler == "Visual Studio" and "MT" in self.settings.compiler.runtime:
+                raise ConanInvalidConfiguration("Visual Studio build for shared library with MT runtime is not supported")
+
+    def source(self):
+        tools.get(**self.conan_data["sources"][self.version])
+        extracted_dir = glob.glob(self.name + "-*/")[0]
+        os.rename(extracted_dir, self._source_subfolder)
+
+    def _configure_cmake(self):
+        if self._cmake:
+            return self._cmake
+        self._cmake = CMake(self)
+        self._cmake.configure()
+        return self._cmake
+
+    def build(self):
+        for patch in self.conan_data.get("patches", {}).get(self.version, []):
+            tools.patch(**patch)
+        cmake = self._configure_cmake()
+        cmake.build()
+
+    def _extract_license(self):
+        with open(os.path.join(self._source_subfolder, "jpge.cpp")) as f:
+            content_lines = f.readlines()
+        license_content = []
+        for i in range(4, 20):
+            license_content.append(content_lines[i][3:-1])
+        return "\n".join(license_content)
+
+    def package(self):
+        cmake = self._configure_cmake()
+        cmake.install()
+        self._extract_license()
+        tools.save(os.path.join(self.package_folder, "licenses", "LICENCE.txt"), self._extract_license())
+
+    def package_info(self):
+        self.cpp_info.libs = tools.collect_libs(self)


Test patch:
diff --git a/recipes/jpeg-compressor/all/test_package/CMakeLists.txt b/recipes/jpeg-compressor/all/test_package/CMakeLists.txt
new file mode 100644
--- /dev/null
+++ b/recipes/jpeg-compressor/all/test_package/CMakeLists.txt
@@ -0,0 +1,9 @@
+cmake_minimum_required(VERSION 3.1)
+project(test_package)
+
+include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
+conan_basic_setup()
+
+add_executable(${PROJECT_NAME} test_package.cpp)
+target_link_libraries(${PROJECT_NAME} ${CONAN_LIBS})
+set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 11)
diff --git a/recipes/jpeg-compressor/all/test_package/conanfile.py b/recipes/jpeg-compressor/all/test_package/conanfile.py
new file mode 100644
--- /dev/null
+++ b/recipes/jpeg-compressor/all/test_package/conanfile.py
@@ -0,0 +1,18 @@
+from conans import ConanFile, CMake, tools
+import os
+
+
+class TestPackageConan(ConanFile):
+    settings = "os", "compiler", "build_type", "arch"
+    generators = "cmake"
+
+    def build(self):
+        cmake = CMake(self)
+        cmake.configure()
+        cmake.build()
+
+    def test(self):
+        if not tools.cross_building(self.settings):
+            img_path = os.path.join(self.source_folder, "testimg.jpg")
+            bin_path = os.path.join("bin", "test_package")
+            self.run("{} {}".format(bin_path, img_path), run_environment=True)
diff --git a/recipes/jpeg-compressor/all/test_package/test_package.cpp b/recipes/jpeg-compressor/all/test_package/test_package.cpp
new file mode 100644
--- /dev/null
+++ b/recipes/jpeg-compressor/all/test_package/test_package.cpp
@@ -0,0 +1,37 @@
+#include <iostream>
+
+#include "jpge.h"
+#include "jpgd.h"
+
+int main(int argc, const char* argv[])
+{
+    if (argc < 2) {
+        std::cerr << "Need at least one argument" << std::endl;
+        return 1;
+    }
+
+    const char* pSrc_filename = argv[1];
+
+    const int req_comps = 4;
+    int width = 0, height = 0, actual_comps = 0;
+    uint8_t* pImage_data = jpgd::decompress_jpeg_image_from_file(pSrc_filename, &width, &height, &actual_comps, req_comps);
+    if (!pImage_data)
+    {
+        std::cerr << "Failed loading file " << pSrc_filename << std::endl;
+        return EXIT_FAILURE;
+    }
+
+    int buf_size = width * height * 3;
+    if (buf_size < 1024) buf_size = 1024;
+    void* pBuf = malloc(buf_size);
+    if (!jpge::compress_image_to_jpeg_file_in_memory(pBuf, buf_size, width, height, req_comps, pImage_data))
+    {
+        std::cerr << "Failed creating JPEG data" << std::endl;
+        return EXIT_FAILURE;
+    }
+
+    free(pBuf);
+    free(pImage_data);
+
+    return EXIT_SUCCESS;
+}
diff --git a/recipes/jpeg-compressor/all/test_package/testimg.jpg b/recipes/jpeg-compressor/all/test_package/testimg.jpg
new file mode 100644
Binary files /dev/null and b/recipes/jpeg-compressor/all/test_package/testimg.jpg differ