Coverage Report

Created: 2026-11-12 07:19

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/dcmtk/dcmjpeg/libsrc/djutils.cc
Line
Count
Source
1
/*
2
 *
3
 *  Copyright (C) 1997-2021, OFFIS e.V.
4
 *  All rights reserved.  See COPYRIGHT file for details.
5
 *
6
 *  This software and supporting documentation were developed by
7
 *
8
 *    OFFIS e.V.
9
 *    R&D Division Health
10
 *    Escherweg 2
11
 *    D-26121 Oldenburg, Germany
12
 *
13
 *
14
 *  Module:  dcmjpeg
15
 *
16
 *  Author:  Norbert Olges, Marco Eichelberg
17
 *
18
 *  Purpose: Various helper methods for the JPEG encoder
19
 *
20
 */
21
22
#include "dcmtk/config/osconfig.h"
23
#include "dcmtk/dcmjpeg/djutils.h"
24
#include "dcmtk/dcmdata/dcdeftag.h"  /* for tag constants */
25
#include "dcmtk/dcmdata/dcitem.h"    /* for class DcmItem */
26
27
OFLogger DCM_dcmjpegLogger = OFLog::getLogger("dcmtk.dcmjpeg");
28
29
makeOFConditionConst(EJ_Suspension,                           OFM_dcmjpeg,  1, OF_error, "IJG codec suspension return"  );
30
makeOFConditionConst(EJ_IJG8_FrameBufferTooSmall,             OFM_dcmjpeg,  2, OF_error, "Buffer for decompressed image (8 bits/sample) too small"  );
31
makeOFConditionConst(EJ_IJG12_FrameBufferTooSmall,            OFM_dcmjpeg,  3, OF_error, "Buffer for decompressed image (12 bits/sample) too small"  );
32
makeOFConditionConst(EJ_IJG16_FrameBufferTooSmall,            OFM_dcmjpeg,  4, OF_error, "Buffer for decompressed image (16 bits/sample) too small"  );
33
makeOFConditionConst(EJ_UnsupportedPhotometricInterpretation, OFM_dcmjpeg,  5, OF_error, "Codec does not support this PhotometricInterpretation"  );
34
makeOFConditionConst(EJ_UnsupportedColorConversion,           OFM_dcmjpeg,  6, OF_error, "Codec does not support this kind of color conversion"  );
35
makeOFConditionConst(EJ_UnsupportedBitDepth,                  OFM_dcmjpeg,  7, OF_error, "Unsupported bit depth for this codec"  );
36
37
EP_Interpretation DcmJpegHelper::getPhotometricInterpretation(DcmItem *item)
38
0
{
39
0
  if (item)
40
0
  {
41
0
    OFString photometric;
42
0
    if (item->findAndGetOFString(DCM_PhotometricInterpretation, photometric).good() && !photometric.empty())
43
0
    {
44
0
      const char *c = photometric.c_str(); // guaranteed to be zero-terminated
45
0
      char cp[17]; // legal CS cannot be larger than 16 characters plus 0 byte
46
0
      int i=0; // current character index
47
0
      unsigned char cur; // current character
48
0
      while (*c && (i<16))
49
0
      {
50
0
        cur = OFstatic_cast(unsigned char, *c);
51
0
        if (isalpha(cur)) cp[i++] = OFstatic_cast(char, toupper(cur));
52
0
        else if (isdigit(cur)) cp[i++] = cur;
53
0
        c++;
54
0
      }
55
0
      cp[i] = 0; // write terminating zero
56
0
      photometric = cp; // copy back into OFString
57
58
      // now browse PhotometricInterpretationNames
59
0
      i = 0;
60
0
      while (PhotometricInterpretationNames[i].Name)
61
0
      {
62
0
        if (photometric == PhotometricInterpretationNames[i].Name) return PhotometricInterpretationNames[i].Type;
63
0
        i++;
64
0
      }
65
0
      return EPI_Unknown;
66
0
    }
67
0
  }
68
0
  return EPI_Missing;
69
0
}
70
71
void DcmJpegHelper::fixPadding(
72
    Uint8 *buffer,
73
    Uint32 bufSize)
74
0
{
75
0
  if (buffer && (bufSize > 0))
76
0
  {
77
    // first write a zero pad byte after the end of the JPEG bitstream
78
0
    buffer[bufSize - 1] = 0;
79
80
0
#ifndef DISABLE_FF_JPEG_BITSTREAM_PADDING
81
    // look for the EOI marker
82
0
    if ((bufSize > 2) && (buffer[bufSize-3] == 0xFF) && (buffer[bufSize-2] == 0xD9))
83
0
    {
84
      // we now have ff/d9/00 at the end of the JPEG bitstream,
85
      // i.e. an end of image (EOI) marker followed by a pad byte.
86
      // Replace this with ff/ff/d9, which is an "extended" EOI marker
87
      // ending on an even byte boundary.
88
0
      buffer[bufSize-2] = 0xFF;
89
0
      buffer[bufSize-1] = 0xD9;
90
0
    }
91
0
  }
92
0
#endif
93
94
0
  return;
95
0
}