| 1 |
/* |
| 2 |
libheif example application. |
| 3 |
|
| 4 |
MIT License |
| 5 |
|
| 6 |
Copyright (c) 2024 Brad Hards <[email protected]> |
| 7 |
|
| 8 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 |
of this software and associated documentation files (the "Software"), to deal |
| 10 |
in the Software without restriction, including without limitation the rights |
| 11 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 |
copies of the Software, and to permit persons to whom the Software is |
| 13 |
furnished to do so, subject to the following conditions: |
| 14 |
|
| 15 |
The above copyright notice and this permission notice shall be included in all |
| 16 |
copies or substantial portions of the Software. |
| 17 |
|
| 18 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 |
SOFTWARE. |
| 25 |
*/ |
| 26 |
#include <cerrno> |
| 27 |
#include <tiff.h> |
| 28 |
#include <cstring> |
| 29 |
#include <cstdlib> |
| 30 |
#include <vector> |
| 31 |
|
| 32 |
#include "encoder_tiff.h" |
| 33 |
#include <tiffio.h> |
| 34 |
|
| 35 |
TiffEncoder::TiffEncoder() = default; |
| 36 |
|
| 37 |
bool TiffEncoder::Encode(const struct heif_image_handle *handle, |
| 38 |
const struct heif_image *image, const std::string &filename) |
| 39 |
{ |
| 40 |
TIFF *tif = TIFFOpen(filename.c_str(), "w"); |
| 41 |
|
| 42 |
// For now we write interleaved |
| 43 |
int width = heif_image_get_width(image, heif_channel_interleaved); |
| 44 |
int height = heif_image_get_height(image, heif_channel_interleaved); |
| 45 |
bool hasAlpha = ((heif_image_get_chroma_format(image) == heif_chroma_interleaved_RGBA) || |
| 46 |
(heif_image_get_chroma_format(image) == heif_chroma_interleaved_RRGGBBAA_BE)); |
| 47 |
int input_bpp = heif_image_get_bits_per_pixel_range(image, heif_channel_interleaved); |
| 48 |
|
| 49 |
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width); |
| 50 |
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height); |
| 51 |
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, input_bpp); |
| 52 |
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, hasAlpha ? 4 : 3); |
| 53 |
if (hasAlpha) |
| 54 |
{ |
| 55 |
// TODO: is alpha premultiplied? |
| 56 |
uint16_t extra_samples[1] = {EXTRASAMPLE_UNASSALPHA}; |
| 57 |
TIFFSetField(tif, TIFFTAG_EXTRASAMPLES, 1, &extra_samples); |
| 58 |
} |
| 59 |
TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1); |
| 60 |
TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); |
| 61 |
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); |
| 62 |
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB); |
| 63 |
TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT); |
| 64 |
TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE); |
| 65 |
|
| 66 |
size_t stride_rgb; |
| 67 |
const uint8_t *row_rgb = heif_image_get_plane_readonly2(image, |
| 68 |
heif_channel_interleaved, &stride_rgb); |
| 69 |
|
| 70 |
for (int i = 0; i < height; i++) |
| 71 |
{ |
| 72 |
// memcpy(scan_line, &buffer[i * width], width * sizeof(uint32)); |
| 73 |
TIFFWriteScanline(tif, (void *)(&(row_rgb[i * stride_rgb])), i, 0); |
| 74 |
} |
| 75 |
TIFFClose(tif); |
| 76 |
return true; |
| 77 |
} |