|
| 1 | +/* |
| 2 | + * Copyright (C) 2016-2025 Grok Image Compression Inc. |
| 3 | + * |
| 4 | + * This source code is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU Affero General Public License, version 3, |
| 6 | + * as published by the Free Software Foundation. |
| 7 | + * |
| 8 | + * This source code is distributed in the hope that it will be useful, |
| 9 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | + * GNU Affero General Public License for more details. |
| 12 | + * |
| 13 | + * You should have received a copy of the GNU Affero General Public License |
| 14 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | + * |
| 16 | + */ |
| 17 | +#include <cstdio> |
| 18 | +#include <cstring> |
| 19 | +#include <memory> |
| 20 | +#include <inttypes.h> |
| 21 | + |
| 22 | +#include "grok.h" |
| 23 | + |
| 24 | +int main([[maybe_unused]] int argc, [[maybe_unused]] const char** argv) |
| 25 | +{ |
| 26 | + const uint32_t dimX = 640; |
| 27 | + const uint32_t dimY = 480; |
| 28 | + const uint32_t numComps = 3; |
| 29 | + const uint32_t precision = 16; |
| 30 | + |
| 31 | + grk_object* codec = nullptr; |
| 32 | + grk_image* inputImage = nullptr; |
| 33 | + grk_image* outputImage = nullptr; |
| 34 | + grk_header_info headerInfo = {}; |
| 35 | + int32_t rc = EXIT_FAILURE; |
| 36 | + |
| 37 | + uint64_t compressedLength = 0; |
| 38 | + |
| 39 | + // 1. initialize library |
| 40 | + grk_initialize(nullptr, 0); |
| 41 | + |
| 42 | + // 2. initialize compress and decompress parameters |
| 43 | + grk_cparameters compressParams; |
| 44 | + grk_compress_set_default_params(&compressParams); |
| 45 | + compressParams.cod_format = GRK_FMT_JP2; |
| 46 | + compressParams.verbose = true; |
| 47 | + |
| 48 | + grk_decompress_parameters decompressParams = {}; |
| 49 | + |
| 50 | + // 3.initialize input and output stream parameters |
| 51 | + grk_stream_params outputStreamParams = {}; |
| 52 | + std::unique_ptr<uint8_t[]> data; |
| 53 | + size_t bufLen = (size_t)numComps * ((precision + 7) / 8) * dimX * dimY; |
| 54 | + data = std::make_unique<uint8_t[]>(bufLen); |
| 55 | + outputStreamParams.buf = data.get(); |
| 56 | + outputStreamParams.buf_len = bufLen; |
| 57 | + |
| 58 | + grk_stream_params inputStreamParams = {}; |
| 59 | + inputStreamParams.buf = data.get(); |
| 60 | + |
| 61 | + // 4. create input image (blank) |
| 62 | + auto components = std::make_unique<grk_image_comp[]>(numComps); |
| 63 | + for(uint32_t i = 0; i < numComps; ++i) |
| 64 | + { |
| 65 | + auto c = &components[i]; |
| 66 | + c->w = dimX; |
| 67 | + c->h = dimY; |
| 68 | + c->dx = 1; |
| 69 | + c->dy = 1; |
| 70 | + c->prec = precision; |
| 71 | + c->sgnd = false; |
| 72 | + } |
| 73 | + inputImage = grk_image_new(numComps, components.get(), GRK_CLRSPC_SRGB, true); |
| 74 | + |
| 75 | + // fill in component data: see grok.h header for full details of image structure |
| 76 | + for(uint16_t compno = 0; compno < inputImage->numcomps; ++compno) |
| 77 | + { |
| 78 | + auto comp = inputImage->comps + compno; |
| 79 | + auto compWidth = comp->w; |
| 80 | + auto compHeight = comp->h; |
| 81 | + auto destData = (int32_t*)comp->data; |
| 82 | + if(!destData) |
| 83 | + { |
| 84 | + fprintf(stderr, "Image has null data for component %d\n", compno); |
| 85 | + goto beach; |
| 86 | + } |
| 87 | + // fill in component data, taking component stride into account |
| 88 | + // in this example, we just zero out each component |
| 89 | + auto srcData = new int32_t[(size_t)compWidth * compHeight]; |
| 90 | + memset(srcData, 0, (size_t)compWidth * compHeight * sizeof(int32_t)); |
| 91 | + auto srcPtr = srcData; |
| 92 | + for(uint32_t j = 0; j < compHeight; ++j) |
| 93 | + { |
| 94 | + memcpy(destData, srcPtr, (size_t)compWidth * sizeof(int32_t)); |
| 95 | + srcPtr += compWidth; |
| 96 | + destData += comp->stride; |
| 97 | + } |
| 98 | + delete[] srcData; |
| 99 | + } |
| 100 | + |
| 101 | + // 5. initialize compressor |
| 102 | + codec = grk_compress_init(&outputStreamParams, &compressParams, inputImage); |
| 103 | + if(!codec) |
| 104 | + { |
| 105 | + fprintf(stderr, "Failed to initialize compressor\n"); |
| 106 | + goto beach; |
| 107 | + } |
| 108 | + |
| 109 | + // 6. compress |
| 110 | + compressedLength = grk_compress(codec, nullptr); |
| 111 | + if(compressedLength == 0) |
| 112 | + { |
| 113 | + fprintf(stderr, "Failed to compress\n"); |
| 114 | + goto beach; |
| 115 | + } |
| 116 | + printf("Compression succeeded: %" PRIu64 " bytes used.\n", compressedLength); |
| 117 | + |
| 118 | + // 7. complete initialization of input stream parameters |
| 119 | + inputStreamParams.buf_len = compressedLength; |
| 120 | + |
| 121 | + grk_object_unref(codec); |
| 122 | + codec = grk_decompress_init(&inputStreamParams, &decompressParams); |
| 123 | + if(!grk_decompress_read_header(codec, &headerInfo)) |
| 124 | + { |
| 125 | + fprintf(stderr, "Failed to read the header\n"); |
| 126 | + goto beach; |
| 127 | + } |
| 128 | + |
| 129 | + // 8. decompress image |
| 130 | + if(!grk_decompress(codec, nullptr)) |
| 131 | + { |
| 132 | + fprintf(stderr, "Decompression failed\n"); |
| 133 | + goto beach; |
| 134 | + } |
| 135 | + |
| 136 | + // 9. retrieve decompressed image |
| 137 | + outputImage = grk_decompress_get_image(codec); |
| 138 | + if(!outputImage) |
| 139 | + { |
| 140 | + fprintf(stderr, "Decompression failed\n"); |
| 141 | + goto beach; |
| 142 | + } |
| 143 | + |
| 144 | + rc = EXIT_SUCCESS; |
| 145 | +beach: |
| 146 | + |
| 147 | + // cleanup |
| 148 | + grk_object_unref(codec); |
| 149 | + if(inputImage) |
| 150 | + grk_object_unref(&inputImage->obj); |
| 151 | + // note: since inputImage was allocated by library, it will be cleaned |
| 152 | + // up by library |
| 153 | + grk_deinitialize(); |
| 154 | + |
| 155 | + return rc; |
| 156 | +} |
0 commit comments