57 return (
ui16)((v<<8) | (v>>8));
77 if (c ==
' ' || c ==
'\r' || c ==
'\n' || c ==
'\t')
81 while (c !=
'\n') c = fgetc(fh);
106 int max_val = (1 << bit_depth) - 1;
107 const si32 *sp = ln0->i32;
109 for ( ; count > 0; --count)
112 val = val >= 0 ? val : 0;
113 val = val <= max_val ? val : max_val;
122 int max_val = (1<<bit_depth) - 1;
123 const si32 *sp0 = ln0->i32;
124 const si32 *sp1 = ln1->i32;
125 const si32 *sp2 = ln2->i32;
127 for (; count > 0; --count)
131 val = val >= 0 ? val : 0;
132 val = val <= max_val ? val : max_val;
135 val = val >= 0 ? val : 0;
136 val = val <= max_val ? val : max_val;
139 val = val >= 0 ? val : 0;
140 val = val <= max_val ? val : max_val;
145 void gen_cvrt_32b1c_to_16ub1c_le(
const line_buf *ln0,
const line_buf *ln1,
146 const line_buf *ln2,
void *dp,
151 int max_val = (1<<bit_depth) - 1;
152 const si32 *sp = ln0->i32;
154 for (; count > 0; --count)
157 val = val >= 0 ? val : 0;
158 val = val <= max_val ? val : max_val;
167 int max_val = (1<<bit_depth) - 1;
168 const si32 *sp0 = ln0->i32;
169 const si32 *sp1 = ln1->i32;
170 const si32 *sp2 = ln2->i32;
172 for (; count > 0; --count)
176 val = val >= 0 ? val : 0;
177 val = val <= max_val ? val : max_val;
180 val = val >= 0 ? val : 0;
181 val = val <= max_val ? val : max_val;
184 val = val >= 0 ? val : 0;
185 val = val <= max_val ? val : max_val;
196 int max_val = (1<<bit_depth) - 1;
197 const si32 *sp = ln0->i32;
199 for (; count > 0; --count)
202 val = val >= 0 ? val : 0;
203 val = val <= max_val ? val : max_val;
204 *p++ = swap_bytes_if_le((
ui16) val);
212 int max_val = (1<<bit_depth) - 1;
213 const si32 *sp0 = ln0->i32;
214 const si32 *sp1 = ln1->i32;
215 const si32 *sp2 = ln2->i32;
217 for (; count > 0; --count)
221 val = val >= 0 ? val : 0;
222 val = val <= max_val ? val : max_val;
223 *p++ = swap_bytes_if_le((
ui16) val);
225 val = val >= 0 ? val : 0;
226 val = val <= max_val ? val : max_val;
227 *p++ = swap_bytes_if_le((
ui16) val);
229 val = val >= 0 ? val : 0;
230 val = val <= max_val ? val : max_val;
231 *p++ = swap_bytes_if_le((
ui16) val);
248 fh = fopen(filename,
"rb");
250 OJPH_ERROR(0x03000001,
"Unable to open file %s", filename);
255 if (fread(t, 1, 2,
fh) != 2)
258 OJPH_ERROR(0x03000002,
"Error reading file %s", filename);
262 if (t[0] !=
'P' || (t[1] !=
'5' && t[1] !=
'6'))
265 OJPH_ERROR(0x03000003,
"unknown file type for file %s", filename);
268 size_t len = strlen(filename);
270 (len < 4 || filename[len - 4] !=
'.' ||
271 (filename[len - 3] !=
'p' && filename[len - 3] !=
'P') ||
272 (filename[len - 2] !=
'g' && filename[len - 2] !=
'G') ||
273 (filename[len - 1] !=
'm' && filename[len - 1] !=
'M')))
276 OJPH_ERROR(0x03000004,
"wrong file extension, a file with "
277 "keyword P5 must have a .pgm extension for file %s", filename);
280 (len < 4 || filename[len - 4] !=
'.' ||
281 (filename[len - 3] !=
'p' && filename[len - 3] !=
'P') ||
282 (filename[len - 2] !=
'p' && filename[len - 2] !=
'P') ||
283 (filename[len - 1] !=
'm' && filename[len - 1] !=
'M')))
286 OJPH_ERROR(0x03000005,
"wrong file extension, a file with keyword P6 "
287 "must have a .ppm extension for file %s", filename);
298 OJPH_ERROR(0x03000006,
"error in file format for file %s", filename);
320 OJPH_ERROR(0x03000007,
"error allocating memory");
351 if (
planar || comp_num == 0)
353 size_t result = fread(
370 si32* dp = line->i32;
377 si32* dp = line->i32;
379 *dp++ = (
si32) swap_bytes_if_le((
ui16) *sp);
396 assert(
fh == NULL &&
buffer == NULL);
399 size_t len = strlen(filename);
402 if (strncmp(
".ppm", filename + len - 4, 4) == 0)
404 filename[len - 2] =
'g';
405 OJPH_WARN(0x03000021,
"file was renamed %s\n", filename);
407 if (strncmp(
".PPM", filename + len - 4, 4) == 0)
409 filename[len - 2] =
'G';
410 OJPH_WARN(0x03000022,
"file was renamed %s\n", filename);
413 fh = fopen(filename,
"wb");
416 "unable to open file %s for writing", filename);
424 size_t len = strlen(filename);
427 if (strncmp(
".pgm", filename + len - 4, 4) == 0)
429 filename[len - 2] =
'p';
430 OJPH_WARN(0x03000024,
"file was renamed %s\n", filename);
432 if (strncmp(
".PGM", filename + len - 4, 4) == 0)
434 filename[len - 2] =
'P';
435 OJPH_WARN(0x03000025,
"file was renamed %s\n", filename);
438 fh = fopen(filename,
"wb");
441 "unable to open file %s for writing", filename);
445 OJPH_ERROR(0x03000027,
"error writing to file %s", filename);
460 "ppm supports 3 colour components, while pgm supports 1");
469#if !defined(OJPH_ENABLE_WASM_SIMD) || !defined(OJPH_EMSCRIPTEN)
484 #ifndef OJPH_DISABLE_SIMD
486 #if (defined(OJPH_ARCH_X86_64) || defined(OJPH_ARCH_I386))
488 #ifndef OJPH_DISABLE_SSE4
505 #ifndef OJPH_DISABLE_AVX2
522 #elif defined(OJPH_ARCH_ARM)
551 lptr[comp_num] = line;
557 size_t result = fwrite(
buffer,
577 fh = fopen(filename,
"rb");
579 OJPH_ERROR(0x03000051,
"Unable to open file %s", filename);
584 if (fread(t, 1, 2,
fh) != 2)
587 OJPH_ERROR(0x03000052,
"Error reading file %s", filename);
591 if (t[0] !=
'P' || (t[1] !=
'F' && t[1] !=
'f'))
594 OJPH_ERROR(0x03000053,
"Unknown file type for file %s", filename);
606 "Error reading width and height in file %s", filename);
611 if (fscanf(
fh,
"%f", &
scale) != 1)
614 OJPH_ERROR(0x03000055,
"Error reading scale in file %s", filename);
635 OJPH_ERROR(0x03000056,
"Error allocating memory");
667 * (
size_t)
width *
sizeof(float);
685 float* dp = line->f32;
694 memcpy(&s, sp,
sizeof(
si32));
696 memcpy(dp, &s,
sizeof(
si32));
708 memcpy(&s, sp,
sizeof(
si32));
711 memcpy(dp, &s,
sizeof(
si32));
717 memcpy(&u, sp,
sizeof(
ui32));
719 memcpy(dp, &u,
sizeof(
ui32));
742 assert(
fh == NULL &&
buffer == NULL);
743 fh = fopen(filename,
"wb");
746 "Unable to open file %s for writing", filename);
748 fprintf(
fh,
"P%c\n%d %d\n%f\n",
751 OJPH_ERROR(0x03000072,
"error writing to file %s", filename);
770 bool is_write_little_endian_on_disk =
773 this->scale = is_write_little_endian_on_disk ? -
scale :
scale;
784 float* sp = line->f32;
785 float* dp =
buffer + comp_num;
796 memcpy(&u, sp,
sizeof(
ui32));
799 memcpy(dp, &u,
sizeof(
ui32));
805 memcpy(&u, sp,
sizeof(
ui32));
807 memcpy(dp, &u,
sizeof(
ui32));
815 memcpy(&u, sp,
sizeof(
ui32));
817 memcpy(dp, &u,
sizeof(
ui32));
830 size_t result = fwrite(
buffer,
sizeof(
float), samples_per_line,
fh);
831 if (result != samples_per_line)
846#ifdef OJPH_ENABLE_TIFF_SUPPORT
848 void tif_in::open(
const char* filename)
851 if ((tiff_handle = TIFFOpen(filename,
"r")) == NULL)
852 OJPH_ERROR(0x03000091,
"Unable to open file %s", filename);
856 ui32 tiff_height = 0;
857 TIFFGetField(tiff_handle, TIFFTAG_IMAGEWIDTH, &tiff_width);
858 TIFFGetField(tiff_handle, TIFFTAG_IMAGELENGTH, &tiff_height);
860 ui16 tiff_bits_per_sample = 0;
861 ui16 tiff_samples_per_pixel = 0;
862 TIFFGetField(tiff_handle, TIFFTAG_BITSPERSAMPLE, &tiff_bits_per_sample);
863 TIFFGetField(tiff_handle, TIFFTAG_SAMPLESPERPIXEL, &tiff_samples_per_pixel);
866 tiff_samples_per_pixel =
867 (tiff_samples_per_pixel < 1) ? 1 : tiff_samples_per_pixel;
869 ui16 tiff_planar_configuration = 0;
870 ui16 tiff_photometric = 0;
871 TIFFGetField(tiff_handle, TIFFTAG_PLANARCONFIG, &tiff_planar_configuration);
872 TIFFGetField(tiff_handle, TIFFTAG_PHOTOMETRIC, &tiff_photometric);
874 planar_configuration = tiff_planar_configuration;
876 ui16 tiff_compression = 0;
877 ui32 tiff_rows_per_strip = 0;
878 TIFFGetField(tiff_handle, TIFFTAG_COMPRESSION, &tiff_compression);
879 TIFFGetField(tiff_handle, TIFFTAG_ROWSPERSTRIP, &tiff_rows_per_strip);
881 if (tiff_planar_configuration == PLANARCONFIG_SEPARATE)
883 bytes_per_line = tiff_samples_per_pixel * TIFFScanlineSize64(tiff_handle);
887 bytes_per_line = TIFFScanlineSize64(tiff_handle);
890 line_buffer = malloc(bytes_per_line);
891 if (NULL == line_buffer)
892 OJPH_ERROR(0x03000092,
"Unable to allocate %d bytes for line_buffer[] "
893 "for file %s", bytes_per_line, filename);
898 if( tiff_bits_per_sample != 8 && tiff_bits_per_sample != 16 )
900 OJPH_ERROR(0x03000093,
"\nTIFF IO is currently limited"
901 " to files with TIFFTAG_BITSPERSAMPLE=8 and TIFFTAG_BITSPERSAMPLE=16 \n"
902 "input file = %s has TIFFTAG_BITSPERSAMPLE=%d",
903 filename, tiff_bits_per_sample);
906 if( TIFFIsTiled( tiff_handle ) )
908 OJPH_ERROR(0x03000094,
"\nTIFF IO is currently limited to TIF files "
909 "without tiles. \nInput file %s has been detected as tiled", filename);
912 if(PHOTOMETRIC_RGB != tiff_photometric &&
913 PHOTOMETRIC_MINISBLACK != tiff_photometric )
915 OJPH_ERROR(0x03000095,
"\nTIFF IO is currently limited to "
916 "TIFFTAG_PHOTOMETRIC=PHOTOMETRIC_MINISBLACK=%d and "
917 "PHOTOMETRIC_RGB=%d. \nInput file %s has been detected "
918 "TIFFTAG_PHOTOMETRIC=%d",
919 PHOTOMETRIC_MINISBLACK, PHOTOMETRIC_RGB, filename, tiff_photometric);
922 if( tiff_samples_per_pixel > 4 )
924 OJPH_ERROR(0x03000096,
"\nTIFF IO is currently limited to "
925 "TIFFTAG_SAMPLESPERPIXEL=4 \nInput file %s has been detected with "
926 "TIFFTAG_SAMPLESPERPIXEL=%d",
927 filename, tiff_samples_per_pixel);
932 height = tiff_height;
933 num_comps = tiff_samples_per_pixel;
934 bytes_per_sample = (tiff_bits_per_sample + 7) / 8;
935 for (
ui32 comp_num = 0; comp_num < num_comps; comp_num++)
936 bit_depth[comp_num] = tiff_bits_per_sample;
940 if (tiff_planar_configuration == PLANARCONFIG_SEPARATE &&
941 bytes_per_sample == 1)
943 line_buffer_for_planar_support_uint8 =
944 (uint8_t*)calloc(width,
sizeof(uint8_t));
945 if (NULL == line_buffer_for_planar_support_uint8)
946 OJPH_ERROR(0x03000097,
"Unable to allocate %d bytes for "
947 "line_buffer_for_planar_support_uint8[] for file %s",
948 width *
sizeof(uint8_t), filename);
950 if (tiff_planar_configuration == PLANARCONFIG_SEPARATE &&
951 bytes_per_sample == 2)
953 line_buffer_for_planar_support_uint16 =
954 (uint16_t*)calloc(width,
sizeof(uint16_t));
955 if (NULL == line_buffer_for_planar_support_uint16)
956 OJPH_ERROR(0x03000098,
"Unable to allocate %d bytes for "
957 "line_buffer_for_planar_support_uint16[] for file %s",
958 width *
sizeof(uint16_t), filename);
965 void tif_in::set_bit_depth(
ui32 num_bit_depths,
ui32* bit_depth)
967 if (num_bit_depths < 1)
968 OJPH_ERROR(0x030000A1,
"one or more bit_depths must be provided");
969 ui32 last_bd_idx = 0;
970 for (
ui32 i = 0; i < 4; ++i)
972 ui32 bd = bit_depth[i < num_bit_depths ? i : last_bd_idx];
973 last_bd_idx += last_bd_idx + 1 < num_bit_depths ? 1 : 0;
975 if (bd > 32 || bd < 1)
978 "bit_depth = %d, this must be an integer from 1-32", bd);
980 this->bit_depth[i] = bd;
987 assert(bytes_per_line != 0 && tiff_handle != 0 && comp_num < num_comps);
988 assert((
ui32)line->size >= width);
992 if (PLANARCONFIG_SEPARATE == planar_configuration && 0 == comp_num )
994 for (
ui32 color = 0; color < num_comps; color++)
996 if (bytes_per_sample == 1)
998 TIFFReadScanline(tiff_handle, line_buffer_for_planar_support_uint8,
999 cur_line, (
ui16)color);
1001 uint8_t* line_buffer_of_interleaved_components =
1002 (uint8_t*)line_buffer;
1003 for (
ui32 i = 0; i < width; i++, x += num_comps)
1005 line_buffer_of_interleaved_components[x] =
1006 line_buffer_for_planar_support_uint8[i];
1009 else if (bytes_per_sample == 2)
1011 TIFFReadScanline(tiff_handle, line_buffer_for_planar_support_uint16,
1012 cur_line, (
ui16)color);
1014 ui16* line_buffer_of_interleaved_components = (
ui16*)line_buffer;
1015 for (
ui32 i = 0; i < width; i++, x += num_comps)
1017 line_buffer_of_interleaved_components[x] =
1018 line_buffer_for_planar_support_uint16[i];
1025 else if (planar_configuration == PLANARCONFIG_CONTIG && 0 == comp_num)
1027 TIFFReadScanline(tiff_handle, line_buffer, cur_line++);
1029 if (cur_line >= height)
1034 if (bytes_per_sample == 1)
1036 const ui8* sp = (
ui8*)line_buffer + comp_num;
1037 si32* dp = line->i32;
1038 if (bit_depth[comp_num] == 8)
1040 for (
ui32 i = width; i > 0; --i, sp += num_comps)
1043 else if (bit_depth[comp_num] < 8)
1046 const int bits_to_shift = 8 - (int)bit_depth[comp_num];
1047 const int bit_mask = (1 << bit_depth[comp_num]) - 1;
1048 for (
ui32 i = width; i > 0; --i, sp += num_comps)
1049 *dp++ = (
si32) (((*sp) >> bits_to_shift) & bit_mask);
1051 else if (bit_depth[comp_num] > 8)
1053 const int bits_to_shift = (int)bit_depth[comp_num] - 8;
1054 const int bit_mask = (1 << bit_depth[comp_num]) - 1;
1055 for (
ui32 i = width; i > 0; --i, sp += num_comps)
1056 *dp++ = (
si32)(((*sp) << bits_to_shift) & bit_mask);
1059 else if(bytes_per_sample == 2)
1061 if (bit_depth[comp_num] == 16)
1063 const ui16* sp = (
ui16*)line_buffer + comp_num;
1064 si32* dp = line->i32;
1065 for (
ui32 i = width; i > 0; --i, sp += num_comps)
1068 else if (bit_depth[comp_num] < 16)
1071 const int bits_to_shift = 16 - (int)bit_depth[comp_num];
1072 const int bit_mask = (1 << bit_depth[comp_num]) - 1;
1073 const ui16* sp = (
ui16*)line_buffer + comp_num;
1074 si32* dp = line->i32;
1075 for (
ui32 i = width; i > 0; --i, sp += num_comps)
1076 *dp++ = (
si32)(((*sp) >> bits_to_shift) & bit_mask);
1078 else if (bit_depth[comp_num] > 16)
1080 const int bits_to_shift = (int)bit_depth[comp_num] - 16;
1081 const int bit_mask = (1 << bit_depth[comp_num]) - 1;
1082 const ui16* sp = (
ui16*)line_buffer + comp_num;
1083 si32* dp = line->i32;
1084 for (
ui32 i = width; i > 0; --i, sp += num_comps)
1085 *dp++ = (
si32)(((*sp) << bits_to_shift) & bit_mask);
1102 void tif_out::open(
char* filename)
1105 ui32 max_bitdepth = 0;
1106 for (
ui32 c = 0; c < num_components; c++)
1108 if (bit_depth_of_data[c] > max_bitdepth)
1109 max_bitdepth = bit_depth_of_data[c];
1111 if (max_bitdepth > 16)
1113 OJPH_WARN(0x030000B1,
"TIFF output is currently limited to files "
1114 "with max_bitdepth = 16, the source codestream has max_bitdepth=%d"
1115 ", the decoded data will be truncated to 16 bits", max_bitdepth);
1117 if (num_components > 4)
1119 OJPH_ERROR(0x030000B2,
"TIFF IO is currently limited to files with "
1120 "num_components=1 to 4");
1123 assert(tiff_handle == NULL && buffer == NULL);
1124 if ((tiff_handle = TIFFOpen(filename,
"w")) == NULL)
1126 OJPH_ERROR(0x030000B3,
"unable to open file %s for writing", filename);
1129 buffer_size = width * (size_t)num_components * (
size_t)bytes_per_sample;
1130 buffer = (
ui8*)malloc(buffer_size);
1137 TIFFSetField(tiff_handle, TIFFTAG_IMAGEWIDTH, width);
1138 TIFFSetField(tiff_handle, TIFFTAG_IMAGELENGTH, height);
1140 TIFFSetField(tiff_handle, TIFFTAG_BITSPERSAMPLE, bytes_per_sample * 8);
1141 TIFFSetField(tiff_handle, TIFFTAG_SAMPLESPERPIXEL, num_components);
1143 planar_configuration = PLANARCONFIG_CONTIG;
1144 TIFFSetField(tiff_handle, TIFFTAG_PLANARCONFIG, planar_configuration);
1146 if (num_components == 1)
1148 TIFFSetField(tiff_handle, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
1150 else if (num_components == 2)
1152 TIFFSetField(tiff_handle, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
1155 const ui16 extra_samples_description[1] = { EXTRASAMPLE_ASSOCALPHA };
1156 TIFFSetField(tiff_handle, TIFFTAG_EXTRASAMPLES, (uint16_t)1,
1157 &extra_samples_description);
1159 else if (num_components == 3)
1161 TIFFSetField(tiff_handle, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
1163 else if (num_components == 4)
1165 TIFFSetField(tiff_handle, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
1168 const ui16 extra_samples_description[1] = { EXTRASAMPLE_ASSOCALPHA };
1169 TIFFSetField(tiff_handle, TIFFTAG_EXTRASAMPLES, (uint16_t)1,
1170 &extra_samples_description);
1173 TIFFSetField(tiff_handle, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
1174 TIFFSetField(tiff_handle, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
1176 TIFFSetField(tiff_handle, TIFFTAG_ROWSPERSTRIP, height);
1181 void tif_out::configure(
ui32 width,
ui32 height,
ui32 num_components,
1184 assert(tiff_handle == NULL);
1186 this->width = width;
1187 this->height = height;
1188 this->num_components = num_components;
1189 ui32 max_bitdepth = 0;
1190 for (
ui32 c = 0; c < num_components; c++)
1192 this->bit_depth_of_data[c] = bit_depth[c];
1193 if (bit_depth[c] > max_bitdepth)
1194 max_bitdepth = bit_depth[c];
1197 bytes_per_sample = (max_bitdepth + 7) / 8;
1198 if (bytes_per_sample > 2)
1202 bytes_per_sample = 2;
1204 samples_per_line = num_components * width;
1205 bytes_per_line = bytes_per_sample * (size_t)samples_per_line;
1212 assert(tiff_handle);
1214 if (bytes_per_sample == 1)
1216 int max_val = (1 << bit_depth_of_data[comp_num]) - 1;
1217 const si32* sp = line->i32;
1218 ui8* dp = buffer + comp_num;
1219 if (bit_depth_of_data[comp_num] == 8)
1221 for (
ui32 i = width; i > 0; --i, dp += num_components)
1225 val = val >= 0 ? val : 0;
1226 val = val <= max_val ? val : max_val;
1230 else if (bit_depth_of_data[comp_num] < 8)
1232 const int bits_to_shift = 8 - (int)bit_depth_of_data[comp_num];
1233 const int bit_mask = (1 << bit_depth_of_data[comp_num]) - 1;
1234 for (
ui32 i = width; i > 0; --i, dp += num_components)
1238 val = val >= 0 ? val : 0;
1239 val = val <= max_val ? val : max_val;
1242 *dp = (
ui8)((val & bit_mask) << bits_to_shift);
1245 else if (bit_depth_of_data[comp_num] > 8)
1247 const int bits_to_shift = (int)bit_depth_of_data[comp_num] - 8;
1248 const int bit_mask = (1 << bit_depth_of_data[comp_num]) - 1;
1249 for (
ui32 i = width; i > 0; --i, dp += num_components)
1253 val = val >= 0 ? val : 0;
1254 val = val <= max_val ? val : max_val;
1257 *dp = (
ui8)((val >> bits_to_shift) & bit_mask);
1262 else if(bytes_per_sample == 2)
1264 int max_val = (1 << bit_depth_of_data[comp_num]) - 1;
1265 const si32* sp = line->i32;
1266 ui16* dp = (
ui16*)buffer + comp_num;
1268 if (bit_depth_of_data[comp_num] == 16)
1270 for (
ui32 i = width; i > 0; --i, dp += num_components)
1274 val = val >= 0 ? val : 0;
1275 val = val <= max_val ? val : max_val;
1279 else if (bit_depth_of_data[comp_num] < 16)
1281 const int bits_to_shift = 16 - (int)bit_depth_of_data[comp_num];
1282 const int bit_mask = (1 << bit_depth_of_data[comp_num]) - 1;
1283 for (
ui32 i = width; i > 0; --i, dp += num_components)
1287 val = val >= 0 ? val : 0;
1288 val = val <= max_val ? val : max_val;
1292 *dp = (
ui16)((val & bit_mask) << bits_to_shift);
1295 else if (bit_depth_of_data[comp_num] > 16)
1297 const int bits_to_shift = (int)bit_depth_of_data[comp_num] - 16;
1298 const int bit_mask = (1 << bit_depth_of_data[comp_num]) - 1;
1299 for (
ui32 i = width; i > 0; --i, dp += num_components)
1303 val = val >= 0 ? val : 0;
1304 val = val <= max_val ? val : max_val;
1308 *dp = (
ui16)((val >> bits_to_shift) & bit_mask);
1314 if (comp_num == num_components-1)
1316 int result = TIFFWriteScanline(tiff_handle, buffer, cur_line++);
1318 OJPH_ERROR(0x030000C1,
"error writing to file %s", fname);
1336 fh = fopen(filename,
"rb");
1338 OJPH_ERROR(0x030000D1,
"Unable to open file %s", filename);
1363 if (result !=
width[comp_num])
1372 si32* dp = line->i32;
1373 for (
ui32 i =
width[comp_num]; i > 0; --i, ++sp)
1380 si32* dp = line->i32;
1381 for (
ui32 i =
width[comp_num]; i > 0; --i, ++sp)
1382 *dp++ = (
si32)swap_bytes_if_be(*sp);
1385 return width[comp_num];
1392 if (num_components != 1 && num_components !=3)
1393 OJPH_ERROR(0x030000F1,
"yuv_in support 1 or 3 components");
1394 this->
num_com = num_components;
1396 if (num_downsamplings < 1)
1397 OJPH_ERROR(0x030000F2,
"one or more downsampling must be provided");
1399 ui32 last_downsamp_idx = 0;
1400 for (
ui32 i = 0; i < num_components; ++i)
1403 last_downsamp_idx += last_downsamp_idx + 1 < num_downsamplings ? 1 : 0;
1405 this->subsampling[i] = cp_ds;
1408 for (
ui32 i = 0; i < num_components; ++i)
1418 if (num_bit_depths < 1)
1419 OJPH_ERROR(0x03000101,
"one or more bit_depths must be provided");
1420 ui32 last_bd_idx = 0;
1421 for (
ui32 i = 0; i < 3; ++i)
1424 last_bd_idx += last_bd_idx + 1 < num_bit_depths ? 1 : 0;
1426 this->bit_depth[i] = bd;
1459 fh = fopen(filename,
"wb");
1461 OJPH_ERROR(0x03000111,
"Unable to open file %s", filename);
1477 tw =
ojph_max(tw, this->comp_width[i]);
1494 const si32 *sp = line->i32;
1496 for (
ui32 i = w; i > 0; --i)
1499 val = val >= 0 ? val : 0;
1500 val = val <= max_val ? val : max_val;
1501 *dp++ = swap_bytes_if_be((
ui16)val);
1508 const si32 *sp = line->i32;
1510 for (
ui32 i = w; i > 0; --i)
1513 val = val >= 0 ? val : 0;
1514 val = val <= max_val ? val : max_val;
1536 fh = fopen(filename,
"rb");
1538 OJPH_ERROR(0x03000131,
"Unable to open file %s", filename);
1551 assert(comp_num == 0);
1553 if (result !=
width)
1561 si32* dp = line->i32;
1565 *dp++ = (
si32)swap_bytes_if_be((
ui32)*sp);
1570 *dp++ = (
si32)swap_bytes_if_be(*sp);
1575 si32* dp = line->i32;
1578 for (
ui32 i =
width; i > 0; --i, sp += 3) {
1581 val |= (val & 0x800000) ? (
si32)0xFF000000 : 0;
1586 for (
ui32 i =
width; i > 0; --i, sp += 3)
1593 si32* dp = line->i32;
1602 *dp++ = (
si32)swap_bytes_if_be(*sp);
1607 si32* dp = line->i32;
1658 fh = fopen(filename,
"wb");
1660 OJPH_ERROR(0x03000141,
"Unable to open file %s", filename);
1670 this->width =
width;
1690 assert(comp_num == 0);
1696 const si32* sp = line->i32;
1710 const si32* sp = line->i32;
1718 *dp++ = (
ui8)(val >> 8);
1719 *dp++ = (
ui8)(val >> 16);
1726 const si32* sp = line->i32;
1740 const si32* sp = line->i32;
1764 *dp++ = swap_bytes_if_be((
ui32)val);
1780 *dp++ = (
ui8)(val >> 8);
1781 *dp++ = (
ui8)(val >> 16);
1795 *dp++ = swap_bytes_if_be((
ui16)val);
1835 OJPH_ERROR(0x03000161,
"Unable to open file %s", filename);
1843 OJPH_ERROR(0x03000162,
"Error reading file %s", filename);
1847 const ui32 dpx_magic_number = 0x53445058;
1848 if (dpx_magic_number == magic_number)
1853 else if (dpx_magic_number ==
be2le(magic_number))
1862 OJPH_ERROR(0x03000163,
"Error reading file %s - this does not appear "
1863 "to be a valid DPX file. It has magic number = 0x%08X. The magic "
1864 "number of a DPX file is 0x%08X.", filename, magic_number,
1873 OJPH_ERROR(0x03000164,
"Error reading file %s", filename);
1881 OJPH_ERROR(0x03000165,
"Error reading file %s", filename);
1888 OJPH_ERROR(0x03000166,
"Error reading file %s", filename);
1897 OJPH_ERROR(0x03000167,
"Error reading file %s", filename);
1904 OJPH_ERROR(0x03000168,
"Error reading file %s", filename);
1914 OJPH_ERROR(0x03000169,
"Error reading file %s", filename);
1923 OJPH_ERROR(0x0300016A,
"Error reading file %s", filename);
1932 OJPH_ERROR(0x0300016B,
"Error reading file %s", filename);
1941 OJPH_ERROR(0x0300016C,
"Error reading file %s", filename);
1949 OJPH_ERROR(0x0300016E,
"Error reading file %s", filename);
1958 OJPH_ERROR(0x0300016F,
"Error reading file %s", filename);
1966 OJPH_ERROR(0x03000170,
"Error reading file %s", filename);
1974 OJPH_ERROR(0x03000171,
"Error reading file %s", filename);
1982 OJPH_ERROR(0x03000172,
"Error reading file %s", filename);
1990 OJPH_ERROR(0x03000173,
"Error reading file %s", filename);
1998 OJPH_ERROR(0x03000174,
"Error reading file %s", filename);
2008 OJPH_ERROR(0x03000175,
"Error reading file %s", filename);
2018 OJPH_ERROR(0x03000176,
"Error reading file %s", filename);
2028 OJPH_ERROR(0x03000177,
"Error reading file %s", filename);
2047 / number_of_samples_per_32_bit_word;
2054 OJPH_ERROR(0x03000178,
"Unable to allocate %d bytes for line_buffer[] "
2062 OJPH_ERROR(0x03000179,
"Unable to allocate %d bytes for "
2063 "line_buffer_16bit_samples[] for file %s",
2094 line_buffer_ptr[i] =
be2le(line_buffer_ptr[i]);
2102 line_buffer_ptr[i] =
be2le(line_buffer_ptr[i]);
2109 ui32 word_index = 0;
2118 (
ui16) ((line_buffer_ptr[word_index] & 0xFFC00000) >> 22);
2121 (
ui16) ((line_buffer_ptr[word_index] & 0x003FF000) >> 12);
2124 (
ui16) ((line_buffer_ptr[word_index] & 0x00000FFC) >> 2);
2138 OJPH_ERROR(0x03000182,
"file %s uses DPX image formats that are not "
2139 "yet supported by this software\n bitdepth_for_image_element_1 = "
2140 "%d\n num_comps=%d\npacking_for_image_element_1=%d\n "
2141 "descriptor_for_image_element_1=%d",
fname,
2152 si32* dp = line->i32;
ui32 get_num_components()
size_t number_of_32_bit_words_per_line
void open(const char *filename)
ui16 packing_for_image_element_1
ui8 descriptor_for_image_element_1
ui32 offset_to_data_for_image_element_1
ui32 total_image_file_size_in_bytes
ui16 number_of_image_elements
ui8 bitdepth_for_image_element_1
virtual ui32 read(const line_buf *line, ui32 comp_num)
ui16 * line_buffer_16bit_samples
ui32 offset_to_image_data_in_bytes
ui16 encoding_for_image_element_1
ui32 number_of_samples_per_line
ui8 transfer_characteristic_for_image_element_1
ui8 colormetric_specification_for_image_element_1
ui32 lines_per_image_element
bool is_byte_swapping_necessary
ui32 data_sign_for_image_element_1
void pre_alloc_data(size_t num_ele, ui32 pre_size)
T * post_alloc_data(size_t num_ele, ui32 pre_size)
virtual ui32 read(const line_buf *line, ui32 comp_num)
mem_fixed_allocator * alloc_p
void open(const char *filename)
size_t temp_buf_byte_size
void open(char *filename)
void configure(ui32 width, ui32 height, ui32 num_components, float scale, ui32 *bit_depth)
virtual ui32 write(const line_buf *line, ui32 comp_num)
void open(const char *filename)
mem_fixed_allocator * alloc_p
virtual ui32 read(const line_buf *line, ui32 comp_num)
void open(char *filename)
virtual ui32 write(const line_buf *line, ui32 comp_num)
void configure(ui32 width, ui32 height, ui32 num_components, ui32 bit_depth)
void set_img_props(const size &s, ui32 bit_depth, bool is_signed)
void open(const char *filename)
virtual ui32 read(const line_buf *line, ui32 comp_num=0)
void open(char *filename)
void configure(bool is_signed, ui32 bit_depth, ui32 width)
virtual ui32 write(const line_buf *line, ui32 comp_num=0)
virtual ui32 read(const line_buf *line, ui32 comp_num)
void open(const char *filename)
void set_img_props(const size &s, ui32 num_components, ui32 num_downsampling, const point *downsampling)
void set_bit_depth(ui32 num_bit_depths, ui32 *bit_depth)
void open(char *filename)
void configure(ui32 bit_depth, ui32 num_components, ui32 *comp_width)
virtual ui32 write(const line_buf *line, ui32 comp_num)
void sse41_cvrt_32b3c_to_8ub3c(const line_buf *ln0, const line_buf *ln1, const line_buf *ln2, void *dp, ui32 bit_depth, ui32 count)
int ojph_fseek(FILE *stream, si64 offset, int origin)
static void eat_white_spaces(FILE *fh)
si64 ojph_ftell(FILE *stream)
const bool is_machine_little_endian
void avx2_cvrt_32b3c_to_8ub3c(const line_buf *ln0, const line_buf *ln1, const line_buf *ln2, void *dp, ui32 bit_depth, ui32 count)
void sse41_cvrt_32b1c_to_16ub1c_be(const line_buf *ln0, const line_buf *ln1, const line_buf *ln2, void *dp, ui32 bit_depth, ui32 count)
void avx2_cvrt_32b1c_to_16ub1c_be(const line_buf *ln0, const line_buf *ln1, const line_buf *ln2, void *dp, ui32 bit_depth, ui32 count)
void sse41_cvrt_32b1c_to_8ub1c(const line_buf *ln0, const line_buf *ln1, const line_buf *ln2, void *dp, ui32 bit_depth, ui32 count)
void avx2_cvrt_32b1c_to_8ub1c(const line_buf *ln0, const line_buf *ln1, const line_buf *ln2, void *dp, ui32 bit_depth, ui32 count)
void gen_cvrt_32b3c_to_16ub3c_be(const line_buf *ln0, const line_buf *ln1, const line_buf *ln2, void *dp, ui32 bit_depth, ui32 count)
static ui16 be2le(const ui16 v)
OJPH_EXPORT int get_cpu_ext_level()
void gen_cvrt_32b1c_to_8ub1c(const line_buf *ln0, const line_buf *ln1, const line_buf *ln2, void *dp, ui32 bit_depth, ui32 count)
void sse41_cvrt_32b3c_to_16ub3c_be(const line_buf *ln0, const line_buf *ln1, const line_buf *ln2, void *dp, ui32 bit_depth, ui32 count)
const bool is_force_pfm_write_as_little_endian_on_disk
void gen_cvrt_32b1c_to_16ub1c_be(const line_buf *ln0, const line_buf *ln1, const line_buf *ln2, void *dp, ui32 bit_depth, ui32 count)
void gen_cvrt_32b3c_to_8ub3c(const line_buf *ln0, const line_buf *ln1, const line_buf *ln2, void *dp, ui32 bit_depth, ui32 count)
@ X86_CPU_EXT_LEVEL_SSE41
#define ojph_div_ceil(a, b)
#define OJPH_ERROR(t,...)
point(ui32 x=0, ui32 y=0)