Some minor improvements to ImageIO::saveTIFF()

This commit is contained in:
Flössie
2023-03-03 09:17:12 +01:00
parent 8587fe068d
commit e2311cc3da

View File

@@ -21,6 +21,8 @@
#include <cstring> #include <cstring>
#include <memory> #include <memory>
#include <string> #include <string>
#include <utility>
#include <vector>
#include <fcntl.h> #include <fcntl.h>
#include <glib/gstdio.h> #include <glib/gstdio.h>
@@ -1352,8 +1354,8 @@ int ImageIO::saveTIFF (
bps = getBPS (); bps = getBPS ();
} }
int lineWidth = width * 3 * bps / 8; int lineWidth = width * 3 * (bps / 8);
unsigned char* linebuffer = new unsigned char[lineWidth]; std::vector<unsigned char> linebuffer(lineWidth);
std::string mode = "w"; std::string mode = "w";
@@ -1381,7 +1383,6 @@ int ImageIO::saveTIFF (
#endif #endif
if (!out) { if (!out) {
delete [] linebuffer;
return IMIO_CANNOTWRITEFILE; return IMIO_CANNOTWRITEFILE;
} }
@@ -1485,15 +1486,9 @@ int ImageIO::saveTIFF (
iptcTag.initLongArray((char*)iptcdata, iptclen); iptcTag.initLongArray((char*)iptcdata, iptclen);
if (needsReverse) { if (needsReverse) {
unsigned char *ptr = iptcTag.getValue(); unsigned char *ptr = iptcTag.getValue();
for (int a = 0; a < iptcTag.getCount(); ++a) { for (int a = 0; a < iptcTag.getCount(); ++a, ptr += 4) {
unsigned char cc; std::swap(ptr[0], ptr[3]);
cc = ptr[3]; std::swap(ptr[1], ptr[2]);
ptr[3] = ptr[0];
ptr[0] = cc;
cc = ptr[2];
ptr[2] = ptr[1];
ptr[1] = cc;
ptr += 4;
} }
} }
TIFFSetField (out, TIFFTAG_RICHTIFFIPTC, iptcTag.getCount(), (long*)iptcTag.getValue()); TIFFSetField (out, TIFFTAG_RICHTIFFIPTC, iptcTag.getCount(), (long*)iptcTag.getValue());
@@ -1533,32 +1528,25 @@ int ImageIO::saveTIFF (
} }
for (int row = 0; row < height; row++) { for (int row = 0; row < height; row++) {
getScanline (row, linebuffer, bps, isFloat); getScanline (row, linebuffer.data(), bps, isFloat);
if (bps == 16) { if (bps == 16) {
if(needsReverse && !uncompressed && isFloat) { if(needsReverse && !uncompressed && isFloat) {
for(int i = 0; i < lineWidth; i += 2) { for(int i = 0; i < lineWidth; i += 2) {
char temp = linebuffer[i]; std::swap(linebuffer[i], linebuffer[i + 1]);
linebuffer[i] = linebuffer[i + 1];
linebuffer[i + 1] = temp;
} }
} }
} else if (bps == 32) { } else if (bps == 32) {
if(needsReverse && !uncompressed) { if(needsReverse && !uncompressed) {
for(int i = 0; i < lineWidth; i += 4) { for(int i = 0; i < lineWidth; i += 4) {
char temp = linebuffer[i]; std::swap(linebuffer[i], linebuffer[i + 3]);
linebuffer[i] = linebuffer[i + 3]; std::swap(linebuffer[i + 1], linebuffer[i + 2]);
linebuffer[i + 3] = temp;
temp = linebuffer[i + 1];
linebuffer[i + 1] = linebuffer[i + 2];
linebuffer[i + 2] = temp;
} }
} }
} }
if (TIFFWriteScanline (out, linebuffer, row, 0) < 0) { if (TIFFWriteScanline (out, linebuffer.data(), row, 0) < 0) {
TIFFClose (out); TIFFClose (out);
delete [] linebuffer;
return IMIO_CANNOTWRITEFILE; return IMIO_CANNOTWRITEFILE;
} }
@@ -1608,8 +1596,6 @@ int ImageIO::saveTIFF (
fclose (file); fclose (file);
#endif #endif
delete [] linebuffer;
if (pl) { if (pl) {
pl->setProgressStr ("PROGRESSBAR_READY"); pl->setProgressStr ("PROGRESSBAR_READY");
pl->setProgress (1.0); pl->setProgress (1.0);