1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
|
/*************************************************************************
This project implements a complete(!) JPEG (Recommendation ITU-T
T.81 | ISO/IEC 10918-1) codec, plus a library that can be used to
encode and decode JPEG streams.
It also implements ISO/IEC 18477 aka JPEG XT which is an extension
towards intermediate, high-dynamic-range lossy and lossless coding
of JPEG. In specific, it supports ISO/IEC 18477-3/-6/-7/-8 encoding.
Note that only Profiles C and D of ISO/IEC 18477-7 are supported
here. Check the JPEG XT reference software for a full implementation
of ISO/IEC 18477-7.
Copyright (C) 2012-2018 Thomas Richter, University of Stuttgart and
Accusoft. (C) 2019-2020 Thomas Richter, Fraunhofer IIS.
This program is available under two licenses, GPLv3 and the ITU
Software licence Annex A Option 2, RAND conditions.
For the full text of the GPU license option, see README.license.gpl.
For the full text of the ITU license option, see README.license.itu.
You may freely select between these two options.
For the GPL option, please note the following:
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*************************************************************************/
/*
** This file includes the decompressor front-end of the
** command line interface. It doesn't do much except
** calling libjpeg.
**
** $Id: reconstruct.cpp,v 1.8 2017/11/28 13:08:03 thor Exp $
**
*/
/// Includes
#include "std/stdio.hpp"
#include "std/string.hpp"
#include "cmd/reconstruct.hpp"
#include "cmd/bitmaphook.hpp"
#include "cmd/filehook.hpp"
#include "tools/environment.hpp"
#include "tools/traits.hpp"
#include "interface/types.hpp"
#include "interface/hooks.hpp"
#include "interface/tagitem.hpp"
#include "interface/parameters.hpp"
#include "interface/jpeg.hpp"
///
/// Reconstruct
// This reconstructs an image from the given input file
// and writes the output ppm.
void Reconstruct(const char *infile,const char *outfile,
int colortrafo,const char *alpha,bool upsample)
{
FILE *in = fopen(infile,"rb");
if (in) {
struct JPG_Hook filehook(FileHook,in);
class JPEG *jpeg = JPEG::Construct(NULL);
if (jpeg) {
int ok = 1;
struct JPG_TagItem tags[] = {
JPG_PointerTag(JPGTAG_HOOK_IOHOOK,&filehook),
JPG_PointerTag(JPGTAG_HOOK_IOSTREAM,in),
#ifdef TEST_MARKER_INJECTION
// Stop after the image header...
JPG_ValueTag(JPGTAG_DECODER_STOP,JPGFLAG_DECODER_STOP_FRAME),
#endif
JPG_EndTag
};
//
#ifdef TEST_MARKER_INJECTION
LONG marker;
//
// Check whether this is a marker we care about. Note that
// due to the stop-flag the code aborts within each marker in the
// main header.
do {
// First read the header, or the next part of it.
ok = jpeg->Read(tags);
// Get the next marker that could be potentially of some
// interest for this code.
marker = jpeg->PeekMarker(NULL);
if (marker == 0xffe9) {
UBYTE buffer[4]; // For the marker and the size.
ok = (jpeg->ReadMarker(buffer,sizeof(buffer),NULL) == sizeof(buffer));
if (ok) {
int markersize = (buffer[2] << 8) + buffer[3];
// This should better be >= 2!
if (markersize < 2) {
ok = 0;
} else {
ok = (jpeg->SkipMarker(markersize - 2,NULL) != -1);
}
}
}
//
// Abort when we hit an essential marker that ends the tables/misc
// section.
} while(marker && marker != -1L && ok);
//
// Ok, we found the first frame header, do not go for other tables
// at all, and disable now the stop-flag
tags->SetTagData(JPGTAG_DECODER_STOP,0);
#endif
if (ok && jpeg->Read(tags)) {
UBYTE subx[4],suby[4];
struct JPG_TagItem atags[] = {
JPG_ValueTag(JPGTAG_IMAGE_PRECISION,0),
JPG_ValueTag(JPGTAG_IMAGE_IS_FLOAT,false),
JPG_ValueTag(JPGTAG_IMAGE_OUTPUT_CONVERSION,true),
JPG_EndTag
};
struct JPG_TagItem itags[] = {
JPG_ValueTag(JPGTAG_IMAGE_WIDTH,0),
JPG_ValueTag(JPGTAG_IMAGE_HEIGHT,0),
JPG_ValueTag(JPGTAG_IMAGE_DEPTH,0),
JPG_ValueTag(JPGTAG_IMAGE_PRECISION,0),
JPG_ValueTag(JPGTAG_IMAGE_IS_FLOAT,false),
JPG_ValueTag(JPGTAG_IMAGE_OUTPUT_CONVERSION,true),
JPG_ValueTag(JPGTAG_ALPHA_MODE,JPGFLAG_ALPHA_OPAQUE),
JPG_PointerTag(JPGTAG_ALPHA_TAGLIST,atags),
JPG_PointerTag(JPGTAG_IMAGE_SUBX,subx),
JPG_PointerTag(JPGTAG_IMAGE_SUBY,suby),
JPG_ValueTag(JPGTAG_IMAGE_SUBLENGTH,4),
JPG_EndTag
};
if (jpeg->GetInformation(itags)) {
ULONG width = itags->GetTagData(JPGTAG_IMAGE_WIDTH);
ULONG height = itags->GetTagData(JPGTAG_IMAGE_HEIGHT);
UBYTE depth = itags->GetTagData(JPGTAG_IMAGE_DEPTH);
UBYTE prec = itags->GetTagData(JPGTAG_IMAGE_PRECISION);
UBYTE aprec = 0;
bool pfm = itags->GetTagData(JPGTAG_IMAGE_IS_FLOAT)?true:false;
bool convert = itags->GetTagData(JPGTAG_IMAGE_OUTPUT_CONVERSION)?true:false;
bool doalpha = itags->GetTagData(JPGTAG_ALPHA_MODE,JPGFLAG_ALPHA_OPAQUE)?true:false;
bool apfm = false;
bool aconvert= false;
bool writepgx= false;
if (alpha && doalpha) {
aprec = atags->GetTagData(JPGTAG_IMAGE_PRECISION);
apfm = atags->GetTagData(JPGTAG_IMAGE_IS_FLOAT)?true:false;
aconvert = atags->GetTagData(JPGTAG_IMAGE_OUTPUT_CONVERSION)?true:false;
} else {
doalpha = false; // alpha channel is ignored.
}
UBYTE bytesperpixel = sizeof(UBYTE);
UBYTE pixeltype = CTYP_UBYTE;
if (prec > 8) {
bytesperpixel = sizeof(UWORD);
pixeltype = CTYP_UWORD;
}
if (pfm && convert == false) {
bytesperpixel = sizeof(FLOAT);
pixeltype = CTYP_FLOAT;
}
UBYTE alphabytesperpixel = sizeof(UBYTE);
UBYTE alphapixeltype = CTYP_UBYTE;
if (aprec > 8) {
alphabytesperpixel = sizeof(UWORD);
alphapixeltype = CTYP_UWORD;
}
if (apfm && aconvert == false) {
alphabytesperpixel = sizeof(FLOAT);
alphapixeltype = CTYP_FLOAT;
}
UBYTE *amem = NULL;
UBYTE *mem = (UBYTE *)malloc(width * 8 * depth * bytesperpixel);
if (doalpha)
amem = (UBYTE *)malloc(width * 8 * alphabytesperpixel); // only one component!
if (mem) {
struct BitmapMemory bmm;
bmm.bmm_pMemPtr = mem;
bmm.bmm_pAlphaPtr = amem;
bmm.bmm_ulWidth = width;
bmm.bmm_ulHeight = height;
bmm.bmm_usDepth = depth;
bmm.bmm_ucPixelType = pixeltype;
bmm.bmm_ucAlphaType = alphapixeltype;
bmm.bmm_pTarget = fopen(outfile,"wb");
bmm.bmm_pAlphaTarget = (doalpha)?(fopen(alpha,"wb")):NULL;
bmm.bmm_pSource = NULL;
bmm.bmm_pAlphaSource = NULL;
bmm.bmm_pLDRSource = NULL;
bmm.bmm_bFloat = pfm;
bmm.bmm_bAlphaFloat = apfm;
bmm.bmm_bBigEndian = true;
bmm.bmm_bAlphaBigEndian = true;
bmm.bmm_bNoOutputConversion = !convert;
bmm.bmm_bNoAlphaOutputConversion = !aconvert;
bmm.bmm_bUpsampling = upsample;
memset(bmm.bmm_PGXFiles,0,sizeof(bmm.bmm_PGXFiles));
if (depth != 1 && depth != 3)
writepgx = true;
if (!upsample)
writepgx = true;
//
// If upsampling is enabled, the subsampling factors are
// all implicitly 1.
if (upsample) {
memset(subx,1,sizeof(subx));
memset(suby,1,sizeof(suby));
}
bmm.bmm_bWritePGX = writepgx;
if (writepgx) {
for(int i = 0;i < depth;i++) {
char headername[256],rawname[256];
FILE *hdr;
sprintf(headername,"%s_%d.h",outfile,i);
sprintf(rawname ,"%s_%d.raw" ,outfile,i);
fprintf(bmm.bmm_pTarget,"%s\n",rawname);
hdr = fopen(headername,"wb");
// FIXME: component dimensions have to differ without subsampling expansion
if (hdr) {
fprintf(hdr,"P%c ML +%d %d %d\n",pfm?'F':'G',prec,
(width + subx[i] - 1) / subx[i],
(height + suby[i] - 1) / suby[i]);
fclose(hdr);
}
bmm.bmm_PGXFiles[i] = fopen(rawname,"wb");
}
}
if (bmm.bmm_pTarget) {
struct JPG_Hook bmhook(BitmapHook,&bmm);
struct JPG_Hook alphahook(AlphaHook,&bmm);
// pgx reconstruction is component by component since
// we cannot interleave non-upsampled components of differing
// subsampling factors.
if (writepgx) {
int comp;
for(comp = 0;comp < depth;comp++) {
ULONG y = 0;
ULONG lastline;
ULONG thisheight = height;
struct JPG_TagItem tags[] = {
JPG_PointerTag(JPGTAG_BIH_HOOK,&bmhook),
JPG_PointerTag(JPGTAG_BIH_ALPHAHOOK,&alphahook),
JPG_ValueTag(JPGTAG_DECODER_MINY,y),
JPG_ValueTag(JPGTAG_DECODER_MAXY,y + (suby[comp] << 3) - 1),
JPG_ValueTag(JPGTAG_DECODER_UPSAMPLE,upsample),
JPG_ValueTag(JPGTAG_MATRIX_LTRAFO,colortrafo),
JPG_ValueTag(JPGTAG_DECODER_MINCOMPONENT,comp),
JPG_ValueTag(JPGTAG_DECODER_MAXCOMPONENT,comp),
JPG_EndTag
};
// Reconstruct now the buffered image, line by line. Could also
// reconstruct the image as a whole. What we have here is just a demo
// that is not necessarily the most efficient way of handling images.
do {
lastline = height;
if (lastline > y + (suby[comp] << 3))
lastline = y + (suby[comp] << 3);
tags[2].ti_Data.ti_lData = y;
tags[3].ti_Data.ti_lData = lastline - 1;
ok = jpeg->DisplayRectangle(tags);
y = lastline;
} while(y < thisheight && ok);
}
for(int i = 0;i < depth;i++) {
if (bmm.bmm_PGXFiles[i]) {
fclose(bmm.bmm_PGXFiles[i]);
}
}
} else {
ULONG y = 0; // Just as a demo, run a stripe-based reconstruction.
ULONG lastline;
struct JPG_TagItem tags[] = {
JPG_PointerTag(JPGTAG_BIH_HOOK,&bmhook),
JPG_PointerTag(JPGTAG_BIH_ALPHAHOOK,&alphahook),
JPG_ValueTag(JPGTAG_DECODER_MINY,y),
JPG_ValueTag(JPGTAG_DECODER_MAXY,y+7),
JPG_ValueTag(JPGTAG_DECODER_UPSAMPLE,upsample),
JPG_ValueTag(JPGTAG_MATRIX_LTRAFO,colortrafo),
JPG_EndTag
};
fprintf(bmm.bmm_pTarget,"P%c\n%d %d\n%d\n",
(pfm)?((depth > 1)?'F':'f'):((depth > 1)?('6'):('5')),
width,height,(pfm)?(1):((1 << prec) - 1));
if (bmm.bmm_pAlphaTarget)
fprintf(bmm.bmm_pAlphaTarget,"P%c\n%d %d\n%d\n",
(apfm)?('f'):('5'),
width,height,(apfm)?(1):((1 << aprec) - 1));
//
// Reconstruct now the buffered image, line by line. Could also
// reconstruct the image as a whole. What we have here is just a demo
// that is not necessarily the most efficient way of handling images.
do {
lastline = height;
if (lastline > y + 8)
lastline = y + 8;
tags[2].ti_Data.ti_lData = y;
tags[3].ti_Data.ti_lData = lastline - 1;
ok = jpeg->DisplayRectangle(tags);
y = lastline;
} while(y < height && ok);
}
} else {
perror("failed to open the output file");
}
fclose(bmm.bmm_pTarget);
if (bmm.bmm_pAlphaTarget)
fclose(bmm.bmm_pAlphaTarget);
if (amem)
free(amem);
free(mem);
} else {
fprintf(stderr,"unable to allocate memory to buffer the image");
}
} else ok = 0;
} else ok = 0;
if (!ok) {
const char *error;
int code = jpeg->LastError(error);
fprintf(stderr,"reading a JPEG file failed - error %d - %s\n",code,error);
}
JPEG::Destruct(jpeg);
} else {
fprintf(stderr,"failed to construct the JPEG object");
}
fclose(in);
} else {
perror("failed to open the input file");
}
}
///
|