From: libtiff@apexinternetsoftware.com (Ross Finlayson)
Date: Mon, 17 Nov 2003 22:59:23 -0800
Subject: [Tiff] TIFF, v3.6.0. Fetching "custom" tags
In-Reply-To: <200311180201.hAI21Q6l003110@whale2.fsr.net>
Message-ID: <200311180647.hAI6lY6l004915@whale2.fsr.net>
Content-Type: multipart/alternative;
     boundary="Apple-Mail-1445449218-1"

--Apple-Mail-1445449218-1
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
	format=flowed;
	charset=us-ascii

Hi,

I figure I should look at the source code here and see what is happening 
with the tags and fields and see how they work.

The primary access point to getting and setting fields for a directory 
of a TIFF are TIFFGetField and TIFFSetField as prototyped in the header 
file tiffio.h and implemented in tif_dir.c.

The TIFFGetField function calls TIFFVGetField.  Maybe V is for variable, 
for varargs.  The comment for TIFFVGetField says "like TIFFGetField, but 
taking a varargs parameter list".  The TIFFGetVField function calls a 
function through a function pointer that is an element of the 
TIFFTagMethods struct that is an element of the TIFF struct.  The 
TIFFTagMethods struct contains three function pointers, one is 
prototyped as a TIFFVGetMethod and another as a TIFFVSetMethod.

So, when TIFFGetField is called, then a varargs list is instantiated, 
the varargs is a "compiler facility" or something, then TIFFVGetField is 
called.  Then, in TIFFVGetField, a TIFFFieldInfo struct for the tag that 
is requested is gotten using _TIFFFindFieldInfo.  Then, where that is 
not null, ie the TIFFFieldInfo is found, then if either it's a codec 
pseudo-tag or the TIFFFieldSet function returns non-zero then the 
directory contains those field contents as something has called 
TIFFSetFieldBit for that field or class/category of fields.  Then, the 
get field function pointer from the TIFFTagMethods struct is called with 
the arguments of the TIFF*, the tag, and the va_list from TIFFVGetField.

The TIFFVGetMethod is installed in the TIFFTagMethods member of the TIFF 
struct when the codec is setup.  The encoder is setup when the image is 
opened when the compression tag is set with TIFFSetField(tif, 
TIFFTAG_COMPRESSION, ...).  So now the TIFFGetField function has called 
TIFFVGetField, which is calling the xx_TIFFGetField function of the 
codec, as defined in tif_jpeg.c, tif_lzw.c etcetera.

Then, each codec in its implementation of that TIFFVGetMethod, which 
returns an int, each codec either handles the method, fills the va_list, 
and returns, or returns the result of calling the TIFFVGetMethod that is 
an element of the codec state struct that is installed in the codec 
setup.

So at this point, TIFFGetField was called and the codec has either 
handled the field or the TIFFVGetMethod.  Aha, the TIFFVGetMethod is not 
installed in the state struct on codec setup, but rather in TIFFInitxx 
for the codec xx, eg TIFFInitJPEG.  So what happened when the codec was 
installed was it installed the original TIFFVGetMethod from the 
TIFFTagMethods struct of the TIFF struct to the codec state struct, eg 
JPEGState, and the TIFFVGetMethod of the codec was installed in the 
TIFFTagMethods of the TIFF struct, pointer.

So now the codec's implementation of a TIFFVGetMethod is calling the 
function prototype that was originally installed in the TIFF struct by 
TIFFDefaultDirectory, in tif_open.c, which was initially called through 
TIFFOpen->TIFFFdOpen->TIFFClientOpen.  Also called in 
TIFFDefaultDirectory() is a function _TIFFSetupFieldInfo.  Also in 
TIFFDefaultDirectory is a reference to the _TIFFextender, which has to 
do with the tag extensions.

The default function for getting the field is implemented as 
_TIFFVGetField.  In _TIFFVGetField is where there is a big switch 
statement for many of the tags, and that is where the va_list is 
populated with values that are stored explicitly in the TIFFDirectory 
struct.  Mostly, the variable argument is set to the value of the 
variable as it is stored in the TIFFDirectory, it's important to call 
TIFFGetField with the correct argument types.  At the default label of 
the switch, then there is the function to handle the tags that aren't 
explicitly enumerated in the switch.  This function could presumably 
handle all fields except there have been over time various 
specifications and usages of the fields in terms of their variables that 
require special handling.

Once again the _TIFFFindFieldInfo method is called on the tag type to 
get the TIFFFieldInfo struct.  There is enough information in the 
TIFFFieldInfo struct for most purposes of knowing the valid type and 
count of elements of the field, mostly.

Newt lines from Aliens: "They mostly come at night, ... mostly."  
"Ahhh!"  "Aiiieeee!"  "Rip-ley!"

So anyways here in _TIFFVGetField now there is a const TIFFFieldInfo* 
named fip.  It is non-null because _TIFFFindFieldInfo already returned 
non-null back in TIFFVGetField.  In the function here it is checked or 
null and also that the fip's field_bit member is set to FIELD_CUSTOM, or 
else an error is reported and zero is returned.  It seems that the 
field_bit member of the TIFFFieldInfo of the struct  returned from 
_TIFFFindFieldInfo is expected to be FIELD_CUSTOM or else it is not 
handled.  Where it is, then the function continues...

There is a for loop that increments from zero to one less than the 
td_customValueCount member of the TIFFDirectory struct.  This is 
presumably set in TIFFSetField, which is of course directly related to 
TIFFGetField.  So anyways forr each then it is referencing a 
TIFFTagValue struct that is stored in the td_customValues member, that 
is presumably of type TIFFTagValue*,  of the TIFFDirectory struct.  When 
it finds a TIFFTagValue that has a field_tag member that is equal to the 
tag that was an argument to TIFFGetField way back when, then 
_TIFFVGetField accomplishes one of two things:  if the TIFFTagValue's 
field_passcount is set, then a short value is set in the va_arg as the 
length of the data, and then the second of the variably-many arguments 
is set to TIFFTagValue's value member.  Either that, or if the 
TIFFFieldInfo's field_type is TIFF_ASCII then the argument is set to a 
char pointer of the TIFFTagValue's value member.  If not that, then it 
returns saying "TIFFGetField... pass by value not imp."

There doesn't seem to be implementation of things like the extension tag 
being a plain old uint32 or something.

So it looks like the tag extension logic is in where the customValue is 
set, and to do with TIFFTagValue.

On the flip side is TIFFSetField.  TIFFSetField calls TIFFVSetField.  
TIFFVSetField checks to see if the tag is OK to change with the 
OkToChange function, which basically checks if the mode is to write and 
the directory is in the middle of being written and changing it would 
upset the codec or data write functions.  Then it is calling the TIFF's 
TIFFTagMethods.vsetfield, the codec's, which is then often calling the 
_TIFFVSetField function.  I think the extender functions have added 
TIFFFieldInfo struct that are returned with _TIFFFindFieldInfo.

The _TIFFVSetField sets any explicitly stored variable in the 
TIFFDirectory, and then for others the default label of the switch looks 
in the customValues for the TIFFTagValue and sets its value if it is 
already there or grows the pointer through _TIFFrealloc.

I think it is key to examine _TIFFFindFieldInfo.  I look at the list on 
globals.html of the doxygen output and see _TIFFFindFieldInfo is defined 
in tif_dir.h and implemented in tif_dirinfo.c .  I think I've found an 
error here, the _TIFFFindOrRegisterInfo function is defined in 
tif_dir.h, but it's implemented as _TIFFFindOrRegisterFieldInfo in 
tif_dirinfo.c.

The _TIFFFindFieldInfo goes through the TIFFFieldInfo* member of the 
TIFF struct called tif_fieldinfo, tif_nfields many of them.  Then, here 
in tif_dirinfo.c are other functions to consider.

The tif_fieldinfo struct is initialized with _TIFFSetFieldInfo function 
which merges each of the element of the static fieldInfo array using 
_TIFFMergeFieldInfo.  Then, presumably the codec merges the 
TIFFFieldInfo pointers of the codec.  Then, TIFFFindOrRegisterFieldInfo 
installs private field info structs.

The TIFFFieldInfo struct is as so:

http://www.tiki-lounge.com/~raf/tiff/libtiff-
doxygen/structTIFFFieldInfo.html

from tiffio.h:

typedef	struct {
	ttag_t	field_tag;		/* field's tag */
	short	field_readcount;	/* read count/TIFF_VARIABLE/TIFF_SPP */
	short	field_writecount;	/* write count/TIFF_VARIABLE */
	TIFFDataType field_type;	/* type of associated data */
         unsigned short field_bit;	/* bit in fieldsset bit vector */
	unsigned char field_oktochange;	/* if true, can change while writing */
	unsigned char field_passcount;	/* if true, pass dir count on set */
	char	*field_name;		/* ASCII name */
} TIFFFieldInfo;

typedef struct _TIFFTagValue {
     const TIFFFieldInfo  *info;
     int             count;
     void           *value;
} TIFFTagValue;

There is also there in tiffio.h the TIFFMergeFieldInfo function.

There is much to consider, whether ir not the specific field or a 
category of fields (eg image dimensions) is set in the image is checked 
and set variously in the logic.

Maybe we could introduce a union pointer of TIFF data types to the 
TIFFTagValue, switch around the TIFFDataType, etcetera.

There's quite a bit more to the field logic, I'll try and help figure it 
out.

I fixed up the tiffoj2j.c file a little bit.

Ross F.

<Attachment missing>
--Apple-Mail-1445449218-1
Content-Type: multipart/mixed;
	boundary=Apple-Mail-658735891-2


--Apple-Mail-658735891-2
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
	charset=us-ascii;
	format=flowed

Hi,

I figure I should look at the source code here and see what is happening 
with the tags and fields and see how they work.

The primary access point to getting and setting fields for a directory 
of a TIFF are TIFFGetField and TIFFSetField as prototyped in the header 
file tiffio.h and implemented in tif_dir.c.

The TIFFGetField function calls TIFFVGetField.  Maybe V is for variable, 
for varargs.  The comment for TIFFVGetField says "like TIFFGetField, but 
taking a varargs parameter list".  The TIFFGetVField function calls a 
function through a function pointer that is an element of the 
TIFFTagMethods struct that is an element of the TIFF struct.  The 
TIFFTagMethods struct contains three function pointers, one is 
prototyped as a TIFFVGetMethod and another as a TIFFVSetMethod.

So, when TIFFGetField is called, then a varargs list is instantiated, 
the varargs is a "compiler facility" or something, then TIFFVGetField is 
called.  Then, in TIFFVGetField, a TIFFFieldInfo struct for the tag that 
is requested is gotten using _TIFFFindFieldInfo.  Then, where that is 
not null, ie the TIFFFieldInfo is found, then if either it's a codec 
pseudo-tag or the TIFFFieldSet function returns non-zero then the 
directory contains those field contents as something has called 
TIFFSetFieldBit for that field or class/category of fields.  Then, the 
get field function pointer from the TIFFTagMethods struct is called with 
the arguments of the TIFF*, the tag, and the va_list from TIFFVGetField.

The TIFFVGetMethod is installed in the TIFFTagMethods member of the TIFF 
struct when the codec is setup.  The encoder is setup when the image is 
opened when the compression tag is set with TIFFSetField(tif, 
TIFFTAG_COMPRESSION, ...).  So now the TIFFGetField function has called 
TIFFVGetField, which is calling the xx_TIFFGetField function of the 
codec, as defined in tif_jpeg.c, tif_lzw.c etcetera.

Then, each codec in its implementation of that TIFFVGetMethod, which 
returns an int, each codec either handles the method, fills the va_list, 
and returns, or returns the result of calling the TIFFVGetMethod that is 
an element of the codec state struct that is installed in the codec 
setup.

So at this point, TIFFGetField was called and the codec has either 
handled the field or the TIFFVGetMethod.  Aha, the TIFFVGetMethod is not 
installed in the state struct on codec setup, but rather in TIFFInitxx 
for the codec xx, eg TIFFInitJPEG.  So what happened when the codec was 
installed was it installed the original TIFFVGetMethod from the 
TIFFTagMethods struct of the TIFF struct to the codec state struct, eg 
JPEGState, and the TIFFVGetMethod of the codec was installed in the 
TIFFTagMethods of the TIFF struct, pointer.

So now the codec's implementation of a TIFFVGetMethod is calling the 
function prototype that was originally installed in the TIFF struct by 
TIFFDefaultDirectory, in tif_open.c, which was initially called through 
TIFFOpen->TIFFFdOpen->TIFFClientOpen.  Also called in 
TIFFDefaultDirectory() is a function _TIFFSetupFieldInfo.  Also in 
TIFFDefaultDirectory is a reference to the _TIFFextender, which has to 
do with the tag extensions.

The default function for getting the field is implemented as 
_TIFFVGetField.  In _TIFFVGetField is where there is a big switch 
statement for many of the tags, and that is where the va_list is 
populated with values that are stored explicitly in the TIFFDirectory 
struct.  Mostly, the variable argument is set to the value of the 
variable as it is stored in the TIFFDirectory, it's important to call 
TIFFGetField with the correct argument types.  At the default label of 
the switch, then there is the function to handle the tags that aren't 
explicitly enumerated in the switch.  This function could presumably 
handle all fields except there have been over time various 
specifications and usages of the fields in terms of their variables that 
require special handling.

Once again the _TIFFFindFieldInfo method is called on the tag type to 
get the TIFFFieldInfo struct.  There is enough information in the 
TIFFFieldInfo struct for most purposes of knowing the valid type and 
count of elements of the field, mostly.

Newt lines from Aliens: "They mostly come at night, ... mostly."  
"Ahhh!"  "Aiiieeee!"  "Rip-ley!"

So anyways here in _TIFFVGetField now there is a const TIFFFieldInfo* 
named fip.  It is non-null because _TIFFFindFieldInfo already returned 
non-null back in TIFFVGetField.  In the function here it is checked or 
null and also that the fip's field_bit member is set to FIELD_CUSTOM, or 
else an error is reported and zero is returned.  It seems that the 
field_bit member of the TIFFFieldInfo of the struct  returned from 
_TIFFFindFieldInfo is expected to be FIELD_CUSTOM or else it is not 
handled.  Where it is, then the function continues...

There is a for loop that increments from zero to one less than the 
td_customValueCount member of the TIFFDirectory struct.  This is 
presumably set in TIFFSetField, which is of course directly related to 
TIFFGetField.  So anyways forr each then it is referencing a 
TIFFTagValue struct that is stored in the td_customValues member, that 
is presumably of type TIFFTagValue*,  of the TIFFDirectory struct.  When 
it finds a TIFFTagValue that has a field_tag member that is equal to the 
tag that was an argument to TIFFGetField way back when, then 
_TIFFVGetField accomplishes one of two things:  if the TIFFTagValue's 
field_passcount is set, then a short value is set in the va_arg as the 
length of the data, and then the second of the variably-many arguments 
is set to TIFFTagValue's value member.  Either that, or if the 
TIFFFieldInfo's field_type is TIFF_ASCII then the argument is set to a 
char pointer of the TIFFTagValue's value member.  If not that, then it 
returns saying "TIFFGetField... pass by value not imp."

There doesn't seem to be implementation of things like the extension tag 
being a plain old uint32 or something.

So it looks like the tag extension logic is in where the customValue is 
set, and to do with TIFFTagValue.

On the flip side is TIFFSetField.  TIFFSetField calls TIFFVSetField.  
TIFFVSetField checks to see if the tag is OK to change with the 
OkToChange function, which basically checks if the mode is to write and 
the directory is in the middle of being written and changing it would 
upset the codec or data write functions.  Then it is calling the TIFF's 
TIFFTagMethods.vsetfield, the codec's, which is then often calling the 
_TIFFVSetField function.  I think the extender functions have added 
TIFFFieldInfo struct that are returned with _TIFFFindFieldInfo.

The _TIFFVSetField sets any explicitly stored variable in the 
TIFFDirectory, and then for others the default label of the switch looks 
in the customValues for the TIFFTagValue and sets its value if it is 
already there or grows the pointer through _TIFFrealloc.

I think it is key to examine _TIFFFindFieldInfo.  I look at the list on 
globals.html of the doxygen output and see _TIFFFindFieldInfo is defined 
in tif_dir.h and implemented in tif_dirinfo.c .  I think I've found an 
error here, the _TIFFFindOrRegisterInfo function is defined in 
tif_dir.h, but it's implemented as _TIFFFindOrRegisterFieldInfo in 
tif_dirinfo.c.

The _TIFFFindFieldInfo goes through the TIFFFieldInfo* member of the 
TIFF struct called tif_fieldinfo, tif_nfields many of them.  Then, here 
in tif_dirinfo.c are other functions to consider.

The tif_fieldinfo struct is initialized with _TIFFSetFieldInfo function 
which merges each of the element of the static fieldInfo array using 
_TIFFMergeFieldInfo.  Then, presumably the codec merges the 
TIFFFieldInfo pointers of the codec.  Then, TIFFFindOrRegisterFieldInfo 
installs private field info structs.

The TIFFFieldInfo struct is as so:

http://www.tiki-lounge.com/~raf/tiff/libtiff-
doxygen/structTIFFFieldInfo.html

from tiffio.h:

typedef	struct {
	ttag_t	field_tag;		/* field's tag */
	short	field_readcount;	/* read count/TIFF_VARIABLE/TIFF_SPP */
	short	field_writecount;	/* write count/TIFF_VARIABLE */
	TIFFDataType field_type;	/* type of associated data */
         unsigned short field_bit;	/* bit in fieldsset bit vector */
	unsigned char field_oktochange;	/* if true, can change while writing */
	unsigned char field_passcount;	/* if true, pass dir count on set */
	char	*field_name;		/* ASCII name */
} TIFFFieldInfo;

typedef struct _TIFFTagValue {
     const TIFFFieldInfo  *info;
     int             count;
     void           *value;
} TIFFTagValue;

There is also there in tiffio.h the TIFFMergeFieldInfo function.

There is much to consider, whether ir not the specific field or a 
category of fields (eg image dimensions) is set in the image is checked 
and set variously in the logic.

Maybe we could introduce a union pointer of TIFF data types to the 
TIFFTagValue, switch around the TIFFDataType, etcetera.

There's quite a bit more to the field logic, I'll try and help figure it 
out.

I fixed up the tiffoj2j.c file a little bit.

Ross F.


--Apple-Mail-658735891-2
Content-Disposition: attachment;
	filename="tiffoj2j.c"
Content-Type: application/octet-stream;
	name="tiffoj2j.c";
	x-unix-mode=0644
Content-Transfer-Encoding: quoted-printable

/*=0A=0A=20=20=20=20tiffoj2j=20-=20converts=20an=20OJPEG=20TIFF=20file=20=
to=20a=20JPEG=20TIFF=20file=0A=0A=20=20=20=20R.=20Finlayson=0A=20=20=20=20=
=0A=20=20=20=20a:=20=2011/14/2003=20-=0A=0A*/=0A=0A#include=20<stdio.h>=0A=
#include=20<stdlib.h>=0A#include=20<unistd.h>=0A#include=20"tiffiop.h"=0A=
=0A=0Atypedef=20struct=20{=0A=0A=09tdir_t=20oj2j_directory_count;=0A=09=0A=
=09uint16=20oj2j_component_count;=0A=09uint16=20oj2j_table_count;=0A=09=
uint16=20oj2j_h_samp;=0A=09uint16=20oj2j_v_samp;=0A=09uint32=20=
oj2j_image_width;=0A=09uint32=20oj2j_image_height;=0A=09uint16=20=
oj2j_image_bps;=0A=09=0A=09uint16=20oj2j_jpegproc;=0A=09uint32=20=
oj2j_jpegifoffset;=0A=09uint32=20oj2j_jpegifbytecount;=0A=09uint16=20=
oj2j_jpegrestartinterval;=0A=09uint16=20oj2j_has_jpegrestartinterval;=0A=09=
void*=20oj2j_jpeglosslesspredictors;=0A=09uint16=20=
oj2j_jpeglosslesspredictors_length;=0A=09void*=20=
oj2j_jpegpointtransform;=0A=09uint32=20oj2j_jpegpointtransform_length;=0A=
=09void*=20oj2j_jpegqtables;=0A=09uint32=20oj2j_jpegqtables_length;=0A=09=
void*=20oj2j_jpegactables;=0A=09uint32=20oj2j_jpegactables_length;=0A=09=
void*=20oj2j_jpegdctables;=0A=09uint32=20oj2j_jpegdctables_length;=0A=0A=09=
void*=20oj2j_jpegtables;=0A=09uint32=20oj2j_jpegtables_length;=0A=09=0A=09=
void*=20oj2j_SOF;=0A=09uint32=20oj2j_SOF_length;=0A=09void*=20oj2j_SOS;=0A=
=09uint32=20oj2j_SOS_length;=0A=09void*=20oj2j_DQTs;=0A=09uint32=20=
oj2j_DQTs_length;=0A=09void*=20oj2j_DHT_DCs;=0A=09uint32=20=
oj2j_DHT_DCs_length;=0A=09void*=20oj2j_DHT_ACs;=0A=09uint32=20=
oj2j_DHT_ACs_length;=0A=09void*=20oj2j_DRI;=0A=09uint32=20=
oj2j_DRI_length;=0A=09=0A}=20OJ2J;=0A=0A=0Avoid=20tiffoj2j_usage();=0A=
OJ2J*=20oj2j_init();=0Avoid=20oj2j_free(OJ2J*);=0Aint=20=
oj2j_check_compression(OJ2J*,=20TIFF*);=0Aint=20oj2j_convert(OJ2J*,=20=
TIFF*,=20TIFF*);=0Aint=20oj2j_process_directory(OJ2J*,=20TIFF*,=20TIFF*,=20=
tdir_t);=0Aint=20oj2j_convert_directory(OJ2J*,=20TIFF*,=20TIFF*);=0Aint=20=
oj2j_convert_directory_strips(OJ2J*,=20TIFF*,=20TIFF*);=0Aint=20=
oj2j_convert_directory_tiles(OJ2J*,=20TIFF*,=20TIFF*);=0Aint=20=
oj2j_copy_directory(TIFF*,=20TIFF*);=0Aint=20=
oj2j_copy_directory_basic_tags(TIFF*,=20TIFF*);=0Aint=20=
oj2j_copy_directory_compression_tags(TIFF*,=20TIFF*);=0Aint=20=
oj2j_copy_directory_other_tags(TIFF*,=20TIFF*);=0Aint=20=
oj2j_copy_directory_strips(TIFF*,=20TIFF*);=0Aint=20=
oj2j_copy_directory_tiles(TIFF*,=20TIFF*);=0Aint=20=
oj2j_count_tables(OJ2J*);=0Aint=20oj2j_allocate_SOF(OJ2J*);=0Aint=20=
oj2j_reconstitute_SOF(OJ2J*);=0Aint=20oj2j_allocate_SOS(OJ2J*);=0Aint=20=
oj2j_reconstitute_SOS(OJ2J*);=0Aint=20oj2j_allocate_DQTs(OJ2J*);=0Aint=20=
oj2j_reconstitute_DQTs(OJ2J*);=0Aint=20oj2j_allocate_DHT_ACs(OJ2J*);=0A=
int=20oj2j_reconstitute_DHT_ACs(OJ2J*);=0Aint=20=
oj2j_allocate_DHT_DCs(OJ2J*);=0Aint=20oj2j_reconstitute_DHT_DCs(OJ2J*);=0A=
int=20oj2j_allocate_DRI(OJ2J*);=0Aint=20oj2j_reconstitute_DRI(OJ2J*);=0A=
int=20oj2j_make_jpegtables(OJ2J*);=0A=0Astatic=20const=20char*=20=
oj2j_module=3D"tiffoj2j";=0A=0A/*=20#define=20DOUBT=20*/=0A#ifdef=20=
DOUBT=0Astatic=20FILE*=20dbgout;=0A#define=20dout(name)=20=
if(oj2j->oj2j_##name##_length>0){=20\=0Adbgout=3Dfopen("../../output/"=20=
#name=20".dat",=20"w");\=0Afwrite(oj2j->oj2j_##name,=20=
oj2j->oj2j_##name##_length,=201,=20dbgout);\=0Afclose(dbgout);=20\=0A}=20=
(void)0=0A/*=0A=09The=20##=20preprocessor=20operator=20merges=20the=20=
identifer,=20#=20merges=20the=20const=20char*=20of=20the=20identifier=0A=09=
Learn=20and=20use=20something=20new=20every=20day...=0A*/=0A#endif=0A=
#ifndef=20DOUBT=0A#define=20dout(name)=20(void)0=0A#endif=0A=0A/*=0A=20=20=
=20=20This=20is=20the=20main=20function.=0A=20=20=20=20=0A=20=20=20=20=
The=20usage=20of=20tiffoj2j=20is:=0A=09=0A=09=20tiffoj2j=20-o=20=
output.tiff=20input.tiff=0A=20=20=20=20=0A=20=20=20=20An=20alternate=20=
usage=20is=20tiffoj2j=20-p=20"newjpeg"=20input.tiff=20...=0A=20=20=20=20=0A=
=20=20=20=20The=20postfix=20is=20appended=20to=20the=20end=20of=20output=20=
file=20names.=20=20If=20it=20is=20the=20empty=20string=20then=20=0A=20=20=
=20=20a=20temporary=20file=20is=20the=20output=20and=20it=20is=20moved=20=
over=20the=20input=20file=20upon=20successful=20=0A=20=20=20=20=
completion.=0A=0A=09The=20-f=20option=20for=20fail=20will=20cause=20the=20=
program=20to=20fail=20if=20it=20encounters=20private=20tags=20it=20=
doesn't=20know=20=0A=09how=20to=20copy.=20=20If=20the=20required=20=
argument=20is=20not=20the=20empty=20string=20then=20the=20file=20names=20=
of=20input=20=0A=09files=20where=20tiffoj2j=20fails=20will=20be=20=
written=20to=20that=20log=20file.=0A=09=0A=09The=20-l=20option=20for=20=
log=20will=20write=20a=20log=20file=20with=20file=20names=20of=20input=20=
files=20converted=20by=20tiffoj2j=20=0A=09separated=20by=20newlines.=0A=09=
=0A=09The=20-u=20option=20for=20unsure=20will=20write=20a=20log=20file=20=
with=20file=20names=20of=20input=20files=20converted=20by=20tiffoj2j=20=0A=
=09that=20are=20unsure,=20separated=20by=20newlines.=0A=0A=09tiffoj2j=20=
-f=20"nogo.txt"=20-l=20"went.txt"=20-u=20"unsure.txt"=20-p=20"newjpeg"=20=
*.TIF=0A=0A=20=20=20=20The=20output=20file=20has=20the=20same=20strip=20=
or=20tile=20configuration=20as=20the=20input=20file.=0A=0A=20=20=20=20=
The=20output=20file=20has=20compression=20set=20to=20COMPRESSION_JPEG=3D7=20=
where=20the=20input=20was=20=0A=09COMPRESION_OJPEG=3D6,=20a=20correct=20=
JPEGTables=20field,=20and=20correct=20strip/tile=20contents.=0A=0A=20=20=20=
=20If=20the=20input=20file=20has=20the=20JPEGInterchangeOffset=20and=20=
JPEGInterchangeLength=20fields=20set=20=0A=20=20=20=20but=20is=20broken=20=
and=20missing=20the=20other=20required=20fields=20for=20OJPEG,=20then=20=
it=20is=20converted=20=0A=20=20=20=20to=20a=20valid=20single=20strip=20=
JPEG=20compressed=20TIFF,=20if=20possible.=0A=20=20=20=20=0A=20=20=20=20=
If=20the=20file=20contains=20an=20image=20file=20directory=20that=20does=20=
not=20contain=20OJPEG=20compressed=20=0A=20=20=20=20data,=20it=20is=20=
copied=20over=20without=20modification,=20as=20possible.=0A=09=0A=09=
Reference=20TIFF=206.0,=20ttn2drft.pdf/TIFFphotoshop.pdf,=20ITU-T=20=
T.81.=0A=09=0A=09See=20also=20libtiff.=0A=0A*/=0A=0Aint=20main(int=20=
argc,=20char**=20argv){=0A=0A=09OJ2J*=20oj2j=3DNULL;=0A=09TIFF*=20=
input=3DNULL;=0A=09TIFF*=20output=3DNULL;=0A=09char*=20oj2j_output;=0A=09=
int=20c=3D0;=0A=0A=09while=20((c=20=3D=20getopt(argc,=20argv,=20=
"l:f:o:p:u:"))=20!=3D=20-1){=0A=09=09switch=20(c)=20{=0A=09=09=09case=20=
'l':=0A=09=09=09=0A=09=09=09=09break;=0A=09=09=09case=20'f':=0A=09=09=09=0A=
=09=09=09=09break;=0A=09=09=09=09=0A=09=09=09case=20'o':=0A=09=09=09=09=
oj2j_output=3Doptarg;=0A=09=09=09=09break;=0A=09=09=09=09=0A=09=09=09=
case=20'p':=0A=09=09=09=0A=09=09=09=09break;=0A=09=09=09case=20'u':=0A=09=
=09=09=0A=09=09=09=09break;=0A=09=09=09case=20'h':=0A=09=09=09case=20=
'?':=20=0A=09=09=09=09tiffoj2j_usage();=0A=09=09=09=09goto=20=
failfreeoj2j;=0A=09=09=09=09break;=0A=09=09}=0A=09}=0A=0A=0A=09=
if(argc>optind){=0A=09=09input=20=3D=20TIFFOpen(argv[optind++],=20"r");=0A=
=09=09if(input=3D=3DNULL){=0A=09=09=09TIFFError(=0A=09=09=09=09=
oj2j_module,=20=0A=09=09=09=09"Can't=20open=20input=20file=20%s=20for=20=
reading",=20=0A=09=09=09=09argv[optind-1]);=0A=09=09=09goto=20=
failfreeoj2j;=0A=09=09}=0A=09}=20else=20{=0A=09=09TIFFError(=0A=09=09=09=
oj2j_module,=20=0A=09=09=09"No=20input=20file=20specified");=20=0A=09=09=
tiffoj2j_usage();=0A=09=09goto=20failfreeoj2j;=0A=09}=0A=0A=0A=09=
oj2j=3Doj2j_init();=0A=0A=09oj2j->oj2j_directory_count=20=3D=20=
TIFFNumberOfDirectories(input);=0A=0A=09if(oj2j_check_compression(oj2j,=20=
input)=20!=3D=200){=0A=09=09output=3DTIFFOpen(oj2j_output,=20"w");=0A=09=09=
if(output=3D=3DNULL){=0A=20=20=20=20=20=20=20=20=20=20=20=20//error=0A=09=
=09=09goto=20failcloseinput;=0A=09=09}=0A=09=09oj2j_convert(oj2j,=20=
input,=20output);=0A=09}=20else=20{=0A=20=20=20=20=20=20=20=20//=20input=20=
file=20has=20no=20OJPEG=0A=09}=0A=0A=0A=09TIFFClose(output);=0A=09=
TIFFClose(input);=0A=20=20=20=20oj2j_free(oj2j);=0A=09=0A=09=
return(EXIT_SUCCESS);=0A=09=0A=09TIFFClose(output);=0A=09failcloseinput:=0A=
=09TIFFClose(input);=0A=09failfreeoj2j:=0A=09oj2j_free(oj2j);=0A=09=
return(EXIT_FAILURE);=0A}=0A=0Avoid=20tiffoj2j_usage(){=0A=09=
fprintf(stderr,=20"Aw,=20damn,=20usage!\n");=0A}=0A=0AOJ2J*=20=
oj2j_init(){=0A=0A=09OJ2J*=20oj2j=3DNULL;=0A=09oj2j=3D(OJ2J*)=20=
_TIFFmalloc(sizeof(OJ2J));=0A=09=0A=09_TIFFmemset(oj2j,=200x00,=20=
sizeof(OJ2J));=0A=09return(oj2j);=0A}=0A=0Avoid=20oj2j_free(OJ2J*=20=
oj2j){=0A=0A=09if(oj2j=20!=3D=20NULL){=0A=09=09=
if(oj2j_jpeglosslesspredictors=20!=3D=20=
NULL)_TIFFfree(oj2j_jpeglosslesspredictors);=0A=09=09=
if(oj2j_jpegpointtransform=20!=3D=20=
NULL)_TIFFfree(oj2j_jpegpointtransform);=0A=09=09if(oj2j_jpegqtables=20=
!=3D=20NULL)_TIFFfree(oj2j_jpegqtables);=0A=09=09if(oj2j_jpegactables=20=
!=3D=20NULL)_TIFFfree(oj2j_jpegactables);=0A=09=09if(oj2j_jpegdctables=20=
!=3D=20NULL)_TIFFfree(oj2j_jpegdctables);=0A=09=09if(oj2j_jpegtables=20=
!=3D=20NULL)_TIFFfree(oj2j_jpegtables);=0A=09=09if(oj2j_SOF=20!=3D=20=
NULL)_TIFFfree(oj2j_SOF);=0A=09=09if(oj2j_SOS=20!=3D=20=
NULL)_TIFFfree(oj2j_SOS);=0A=09=09if(oj2j_DQTs=20!=3D=20=
NULL)_TIFFfree(oj2j_DQTs);=0A=09=09if(oj2j_DHT_DCs=20!=3D=20=
NULL)_TIFFfree(oj2j_DHT_DCs);=0A=09=09if(oj2j_DHT_ACs=20!=3D=20=
NULL)_TIFFfree(oj2j_DHT_ACs);=0A=09=09if(oj2j_DRI=20!=3D=20=
NULL)_TIFFfree(oj2j_DRI);=0A=09=09_TIFFfree(oj2j);=0A=09}=0A=09=0A=09=
return;=0A=0A}=0A=0Aint=20oj2j_check_compression(OJ2J*=20oj2j,=20TIFF*=20=
input){=0A=0A=09tdir_t=20i=3D0;=0A=09uint16=20compression=3D0;=0A=20=20=20=
=20=0A=09for(i=3D0;i<oj2j->oj2j_directory_count;i++){=0A=09=09=
TIFFSetDirectory(input,=20i);=0A=09=09TIFFGetField(input,=20=
TIFFTAG_COMPRESSION,=20&compression);=0A=09=09=
if(compression=3D=3DCOMPRESSION_OJPEG){=0A=09=09=09return(1);=0A=09=09}=0A=
=09}=0A=0A=09return(0);=0A=0A}=0A=0A/*=0A=20=20=20=20The=20arguments=20=
to=20oj2j_convert=20is=20the=20input=20TIFF=20file=20which=20has=20been=20=
opened=20for=20reading,=20and=20the=20=0A=20=20=20=20output=20TIFF=20=
file=20which=20has=20been=20opened=20for=20writing=20with=20no=20other=20=
functions=20called=20upon=20it.=0A*/=0A=0Aint=20oj2j_convert(OJ2J*=20=
oj2j,=20TIFF*=20input,=20TIFF*=20output){=0A=0A=0A=20=20=20=20//=20for=20=
each=20directory=20of=20the=20input,=20if=20the=20directory=20is=20=
compressed=20with=20OJPEG,=20convert=20it=20to=20JPEG=0A=20=20=20=20//=20=
otherwise:=20=20copy=0A=09tdir_t=20i=3D0;=0A=09int=20ret=3D0;=0A=20=20=20=
=20=0A=09for(i=3D0;i<oj2j->oj2j_directory_count;i++){=0A=09=09=
ret=3Doj2j_process_directory(oj2j,=20input,=20output,=20i);=0A=09=09=
if(ret=20!=3D=200=20){=0A=09=09=09//=20error=0A=09=09=09return(1);=0A=09=09=
}=0A=09}=0A=20=20=20=20=0A=09return(0);=0A}=0A=0Aint=20=
oj2j_process_directory(OJ2J*=20oj2j,=20TIFF*=20input,=20TIFF*=20output,=20=
tdir_t=20directory){=0A=0A=09uint16=20xuint16=3D0;=0A=0A=09=
TIFFSetDirectory(input,=20directory);=0A=20=20=20=20=0A=09=
TIFFGetField(input,=20TIFFTAG_COMPRESSION,=20&xuint16);=0A=0A=09=
if(xuint16=20=3D=3D=20COMPRESSION_OJPEG){=0A=09=09=
oj2j_convert_directory(oj2j,=20input,=20output);=0A=09}=20else=20{=0A=09=09=
oj2j_copy_directory(input,=20output);=0A=09}=0A=0A=09return(0);=0A}=0A=0A=
int=20oj2j_convert_directory(OJ2J*=20oj2j,=20TIFF*=20input,=20TIFF*=20=
output){=0A=0A=0A=09//=20copy=20"pre-compression=20tags"=0A=09=0A=09=
oj2j_copy_directory_basic_tags(input,=20output);=0A=0A=09=
TIFFGetField(input,=20TIFFTAG_IMAGEWIDTH,=20&(oj2j->oj2j_image_width));=0A=
=09TIFFGetField(input,=20TIFFTAG_IMAGELENGTH,=20=
&(oj2j->oj2j_image_height));=0A=09TIFFGetField(input,=20=
TIFFTAG_BITSPERSAMPLE,=20&(oj2j->oj2j_image_bps));=0A=09=
TIFFGetField(input,=20TIFFTAG_SAMPLESPERPIXEL,=20=
&(oj2j->oj2j_component_count));=0A=09TIFFGetField(input,=20=
TIFFTAG_YCBCRSUBSAMPLING,=20&(oj2j->oj2j_h_samp),=20=
&(oj2j->oj2j_v_samp));=20//=20wrong=0A=09=0A=09//=20if=20ojpeg=20is=20=
broken=20with=20only=20JPEGIFOffset=20and=20JPEGIFBytecount,=20get=20=
those=20fields=0A=0A=09TIFFGetField(=0A=09=09input,=20=0A=09=09=
TIFFTAG_JPEGIFOFFSET,=20=20=0A=09=09&(oj2j->oj2j_jpegifoffset));=0A=09=
TIFFGetField(=0A=09=09input,=20=0A=09=09TIFFTAG_JPEGIFBYTECOUNT,=20=0A=09=
=09&(oj2j->oj2j_jpegifbytecount));=0A=0A=09//=20check=20ifbyte=20count=20=
for=20being=20either=20a:=20good,=20or=20b:=20minus=20the=20strips,=20or=20=
c:=20minus=20the=20strips=20plus=20one=0A=0A=09//=20if=20it's=20less=20=
than=20the=20sum=20of=20the=20strip=20sizes=20then=20it's=20hopefully=20=
b=20or=20c=0A=0A=09//=20get=20OJPEG=20fields,=20if=20they're=20missing=20=
then=20try=20and=20use=20the=20interchange=20format,=20or=20fail=20and=20=
call=20image=20broken=0A=09=0A=09TIFFGetField(=0A=09=09input,=20=0A=09=09=
TIFFTAG_JPEGPROC,=20=0A=09=09&(oj2j->oj2j_jpegproc));=0A=0A=09=
oj2j->oj2j_has_jpegrestartinterval=3DTIFFGetField(=0A=09=09input,=20=0A=09=
=09TIFFTAG_JPEGRESTARTINTERVAL,=20=0A=09=09=
&(oj2j->oj2j_jpegrestartinterval));=0A=0A=09TIFFGetField(=0A=09=09input,=20=
=0A=09=09TIFFTAG_JPEGDCTABLES,=20=0A=09=09=
&(oj2j->oj2j_jpegdctables_length),=20=0A=09=09=
&(oj2j->oj2j_jpegdctables));=0A=0A=09switch(=20oj2j->oj2j_jpegproc){=0A=09=
=0A=09=09case=20JPEGPROC_BASELINE:=0A=09=09=09TIFFGetField(=0A=09=09=09=09=
input,=20=0A=09=09=09=09TIFFTAG_JPEGQTABLES,=20=0A=09=09=09=09=
&(oj2j->oj2j_jpegqtables_length),=0A=09=09=09=09=
&(oj2j->oj2j_jpegqtables));=0A=09=09=09TIFFGetField(=0A=09=09=09=09=
input,=20=0A=09=09=09=09TIFFTAG_JPEGACTABLES,=20=20=0A=09=09=09=09=
&(oj2j->oj2j_jpegactables_length),=20=0A=09=09=09=09=
&(oj2j->oj2j_jpegactables));=0A=09=09=09=09break;=0A=09=09case=20=
JPEGPROC_LOSSLESS:=0A=09=09=09TIFFGetField(=0A=09=09=09=09input,=20=0A=09=
=09=09=09TIFFTAG_JPEGLOSSLESSPREDICTORS,=20=0A=09=09=09=09=
&(oj2j->oj2j_jpeglosslesspredictors_length),=20=0A=09=09=09=09=
&(oj2j->oj2j_jpeglosslesspredictors));=0A=09=09=09TIFFGetField(=0A=09=09=09=
=09input,=20=0A=09=09=09=09TIFFTAG_JPEGPOINTTRANSFORM,=20=0A=09=09=09=09=
&(oj2j->oj2j_jpegpointtransform_length),=0A=09=09=09=09=
&(oj2j->oj2j_jpegpointtransform));=0A=09=09=09=09break;=0A=09=09=09=0A=09=
=09=09default:=0A=09=09=09=09//=20unknown=20JPEG=20process,=20error=0A=09=
=09=09=09return(0);=0A=09=09=09}=0A=20=20=20=20=0A=09dout(jpegqtables);=0A=
=09dout(jpegdctables);=0A=09dout(jpegactables);=0A=0A=09=
oj2j_count_tables(oj2j);=0A=09=0A=09//=20reconstitute=20JPEG=20marker=20=
segments=20from=20OJPEG=20tables=20/=20input=20components,=20samples=0A=09=
=0A=09oj2j_allocate_SOF(oj2j);=0A=09oj2j_reconstitute_SOF(oj2j);=0A=09=
oj2j_allocate_SOS(oj2j);=0A=09oj2j_reconstitute_SOS(oj2j);=0A=09=
if(oj2j->oj2j_jpegrestartinterval=20!=3D=200=20){=0A=09=09=
oj2j_allocate_DRI(oj2j);=0A=09=09oj2j_reconstitute_DRI(oj2j);=0A=09}=0A=09=
oj2j_allocate_DHT_DCs(oj2j);=0A=09oj2j_reconstitute_DHT_DCs(oj2j);=0A=09=
if(oj2j->oj2j_jpegproc=3D=3DJPEGPROC_BASELINE){=0A=09=09=
oj2j_allocate_DQTs(oj2j);=0A=09=09oj2j_reconstitute_DQTs(oj2j);=0A=09=09=
oj2j_allocate_DHT_ACs(oj2j);=0A=09=09oj2j_reconstitute_DHT_ACs(oj2j);=0A=09=
}=0A=20=20=20=20=0A=09dout(SOF);=0A=09dout(SOS);=0A=09dout(DQTs);=0A=09=
dout(DHT_DCs);=0A=09dout(DHT_ACs);=0A=09dout(DRI);=0A=0A=20=20=20=20//=20=
generate=20JPEGTables=20field=20from=20tables=0A=09=0A=09=
oj2j_make_jpegtables(oj2j);=0A=09=0A=09dout(jpegtables);=0A=09=0A=20=20=20=
=20//=20set=20JPEG=20compression=20and=20JPEGTables=20field=20in=20=
output=0A=20=20=20=20=0A=09TIFFSetField(=0A=09=09output,=20=0A=09=09=
TIFFTAG_COMPRESSION,=20=0A=09=09COMPRESSION_JPEG);=0A=09TIFFSetField(=0A=09=
=09output,=20=0A=09=09TIFFTAG_JPEGTABLES,=20=0A=09=09=
oj2j->oj2j_jpegtables_length,=20=0A=09=09oj2j->oj2j_jpegtables);=0A=20=20=
=20=20//=20copy=20all=20other=20tags=0A=20=20=20=20=0A=09=
oj2j_copy_directory_other_tags(input,=20output);=0A=20=20=20=20=0A=20=20=20=
=20//=20copy=20modified=20/=20convert=20strips=20or=20tiles=0A=0A=09=
if(TIFFIsTiled(input)){=0A=09=09oj2j_convert_directory_tiles(oj2j,=20=
input,=20output);=0A=09}=20else=20{=0A=09=09=
oj2j_convert_directory_strips(oj2j,=20input,=20output);=0A=09}=0A=0A=09=
return(0);=0A=0A}=0A=0Aint=20oj2j_convert_directory_strips(OJ2J*=20oj2j,=20=
TIFF*=20input,=20TIFF*=20output){=0A=0A=09uint32*=20strip_byte_counts;=0A=
=09uint32=20max_strip_byte_count=3D0;=0A=09tstrip_t=20strip_count=3D0;=0A=
=09tstrip_t=20i=3D0;=0A=09tsize_t=20bytes_read=3D0;=0A=09tsize_t=20=
bytes_written=3D0;=0A=09tsize_t=20bufoff=3D0;=0A=09tdata_t=20=
buffer=3DNULL;=0A=09=0A=09//=20restart=20marker=0A/*=09toff_t*=20=
strip_offsets;=20hack=20*/=0A=09=0A=09=
strip_count=3DTIFFNumberOfStrips(input);=0A=09TIFFGetField(input,=20=
TIFFTAG_STRIPBYTECOUNTS,=20&strip_byte_counts);=0A=09=
for(i=3D0;i<strip_count;i++){=0A=09=09=
if(strip_byte_counts[i]>max_strip_byte_count){=0A=09=09=09=
max_strip_byte_count+=3Dstrip_byte_counts[i];=0A=09=09}=0A=09}=0A=09=0A=09=
//=20add=20the=20SOI,=20SOF,=20SOS,=20EOI=20overhead=20to=20=
max_strip_byte_count=0A=0A=09=0A=09bufoff+=3D2;=0A=09=
bufoff+=3Doj2j->oj2j_SOF_length;=0A=09if(oj2j->oj2j_jpegrestartinterval=20=
!=3D=200){=0A=09=09bufoff+=3Doj2j->oj2j_DRI_length;=0A=09}=0A=09=
bufoff+=3Doj2j->oj2j_SOS_length;=0A=09max_strip_byte_count+=3Dbufoff;=0A=09=
max_strip_byte_count+=3D2;=0A=09=0A=09buffer=20=3D=20=
_TIFFmalloc(max_strip_byte_count=20/*=20+557=20hack=20*/);=0A=09=0A=09=
((unsigned=20char*)buffer)[0]=3D0xff;=0A=09((unsigned=20=
char*)buffer)[1]=3D0xd8;=0A=09bufoff=3D2;=0A=09=0A=09=
if(oj2j->oj2j_SOF_length=20!=3D=200){=0A=09=09_TIFFmemcpy(=20=0A=09=09=09=
&(((unsigned=20char*)buffer)[bufoff]),=20=0A=09=09=09oj2j->oj2j_SOF,=0A=09=
=09=09oj2j->oj2j_SOF_length=0A=09=09);=0A=09=09=
bufoff+=3Doj2j->oj2j_SOF_length;=0A=09}=0A=09if(oj2j->oj2j_DRI_length=20=
!=3D=200){=0A=09=09_TIFFmemcpy(=20=0A=09=09=09&(((unsigned=20=
char*)buffer)[bufoff]),=20=0A=09=09=09oj2j->oj2j_DRI,=0A=09=09=09=
oj2j->oj2j_DRI_length=0A=09=09);=0A=09=09bufoff+=3Doj2j->oj2j_DRI_length;=0A=
=09}=0A=09if(oj2j->oj2j_SOS_length=20!=3D=200){=0A=09=09_TIFFmemcpy(=20=0A=
=09=09=09&(((unsigned=20char*)buffer)[bufoff]),=20=0A=09=09=09=
oj2j->oj2j_SOS,=0A=09=09=09oj2j->oj2j_SOS_length=0A=09=09);=0A=09=09=
bufoff+=3Doj2j->oj2j_SOS_length;=0A=09}=0A=0A=09//=20might=20have=20to=20=
add=20restart=20markers...,=20a=20different=20one=20for=20each=20strip=20=
or=20tile...=0A=09=0A=09//=20each=20output=20strip=20is=20to=20be=20an=20=
abbreviated=20JPEG=20data=20stream=0A=09=0A=09//=20the=20input=20strip=20=
is=20the=20OJPEG=20data=20segment=0A=09=0A=09//=20it=20is=20missing=20=
restart=20markers=0A=0A=09//=20this=20is=20possibly=20a=20hack=20about=20=
the=20restart=20interval,=20smallliz.tif=20has=20restart=20set=20to=20=
zero=0A=09//=20but=20the=20strip=20starts=20three=20bytes=20before=20=
entropy=20coded=20data=0A=09//=20the=20tag=20is=20set,=20but=20it=20is=20=
zero=0A/*=09if((oj2j->oj2j_has_jpegrestartinterval=20!=3D0)=20&&=20=
(oj2j->oj2j_jpegrestartinterval=20=3D=3D0)){=0A=09=09TIFFGetField(input,=20=
TIFFTAG_STRIPOFFSETS,=20&strip_offsets);=0A=09}=0A*/=09=0A=20=09=0A=09//=20=
write=20the=20output=20strip=0A=09=0A=09for(i=3D0;i<strip_count;i++){=0A=09=
//=20this=20is=20possibly=20a=20hack=20about=20the=20restart=20interval,=20=
smallliz.tif=20has=20restart=20set=20to=20zero=0A=09//=20but=20the=20=
strip=20starts=20three=20bytes=20before=20entropy=20coded=20data=0A=09//=20=
the=20tag=20is=20set,=20but=20it=20is=20zero=0A/*=09=09=
if((oj2j->oj2j_has_jpegrestartinterval=20!=3D0)=20&&=20=
(oj2j->oj2j_jpegrestartinterval=20=3D=3D0)){=0A=09=09=09=
strip_offsets[i]+=3D3;=0A=09=09=09strip_byte_counts[i]-=3D3;=0A=09=09=09=
strip_byte_counts[i]+=3D557;=20//=20wrong=20strip_byte_count=20hack=20=0A=
=09=09}=0A*/=0A=09=09bytes_read=3DTIFFReadRawStrip(input,=20i,=20=
&(((unsigned=20char*)buffer)[bufoff]),=20strip_byte_counts[i]);=0A=09=09=
if(bytes_read=3D=3D-1){=0A=09=09=09//=20error=20on=20read=20raw=20strip=0A=
=09=09=09_TIFFfree(buffer);=0A=09=09=09return(0);=0A=09=09}=0A=09=09=
if(bytes_read<strip_byte_counts[i]){=0A=09=09=09//=20error=0A=09=09}=0A=09=
=09((unsigned=20char*)buffer)[bufoff+strip_byte_counts[i]]=3D0xff;=0A=09=09=
((unsigned=20char*)buffer)[bufoff+strip_byte_counts[i]+1]=3D0xd9;=0A=09=09=
=0A=09=09bytes_written=3DTIFFWriteRawStrip(output,=20i,=20buffer,=20=
bufoff+strip_byte_counts[i]+2);=0A=09=09if(bytes_written=3D=3D-1){=0A=09=09=
=09//=20error=20on=20write=20raw=20strip=0A=09=09=09_TIFFfree(buffer);=0A=
=09=09=09return(0);=0A=09=09}=0A=09=09if(bytes_written<bytes_read){=0A=09=
=09=09//=20error=0A=09=09}=0A=09}=0A=09=0A=09return(0);=0A}=0A=0Aint=20=
oj2j_convert_directory_tiles(OJ2J*=20oj2j,=20TIFF*=20input,=20TIFF*=20=
output){=0A=0A=09uint32*=20tile_byte_counts;=0A=09uint32=20=
max_tile_byte_count=3D0;=0A=09tstrip_t=20tile_count=3D0;=0A=09tstrip_t=20=
i=3D0;=0A=09tsize_t=20bytes_read=3D0;=0A=09tsize_t=20bytes_written=3D0;=0A=
=09tsize_t=20bufoff=3D0;=0A=09tdata_t=20buffer=3DNULL;=0A=09=0A=09=
tile_count=3DTIFFNumberOfTiles(input);=0A=09TIFFGetField(input,=20=
TIFFTAG_TILEBYTECOUNTS,=20&tile_byte_counts);=0A=09=
for(i=3D0;i<tile_count;i++){=0A=09=09=
if(tile_byte_counts[i]>max_tile_byte_count){=0A=09=09=09=
max_tile_byte_count+=3Dtile_byte_counts[i];=0A=09=09}=0A=09}=0A=09=0A=09=
//=20add=20the=20SOI,=20SOF,=20SOS,=20EOI=20overhead=20to=20=
max_strip_byte_count=0A=0A=09=0A=09bufoff+=3D2;=0A=09=
bufoff+=3Doj2j->oj2j_SOF_length;=0A=09if(oj2j->oj2j_jpegrestartinterval=20=
!=3D=200){=0A=09=09bufoff+=3Doj2j->oj2j_DRI_length;=0A=09}=0A=09=
bufoff+=3Doj2j->oj2j_SOS_length;=0A=09max_tile_byte_count+=3Dbufoff;=0A=09=
max_tile_byte_count+=3D2;=0A=09=0A=09buffer=20=3D=20=
_TIFFmalloc(max_tile_byte_count);=0A=09=0A=09((unsigned=20=
char*)buffer)[0]=3D0xff;=0A=09((unsigned=20char*)buffer)[1]=3D0xd8;=0A=09=
bufoff=3D2;=0A=09=0A=09if(oj2j->oj2j_SOF_length=20!=3D=200){=0A=09=09=
_TIFFmemcpy(=20=0A=09=09=09&(((unsigned=20char*)buffer)[bufoff]),=20=0A=09=
=09=09oj2j->oj2j_SOF,=0A=09=09=09oj2j->oj2j_SOF_length=0A=09=09);=0A=09=09=
bufoff+=3Doj2j->oj2j_SOF_length;=0A=09}=0A=09if(oj2j->oj2j_DRI_length=20=
!=3D=200){=0A=09=09_TIFFmemcpy(=20=0A=09=09=09&(((unsigned=20=
char*)buffer)[bufoff]),=20=0A=09=09=09oj2j->oj2j_DRI,=0A=09=09=09=
oj2j->oj2j_DRI_length=0A=09=09);=0A=09=09bufoff+=3Doj2j->oj2j_DRI_length;=0A=
=09}=0A=09if(oj2j->oj2j_SOS_length=20!=3D=200){=0A=09=09_TIFFmemcpy(=20=0A=
=09=09=09&(((unsigned=20char*)buffer)[bufoff]),=20=0A=09=09=09=
oj2j->oj2j_SOS,=0A=09=09=09oj2j->oj2j_SOS_length=0A=09=09);=0A=09=09=
bufoff+=3Doj2j->oj2j_SOS_length;=0A=09}=0A=0A=09//=20might=20have=20to=20=
add=20restart=20markers...,=20a=20different=20one=20for=20each=20strip=20=
or=20tile...=0A=09=0A=09//=20each=20output=20tile=20is=20to=20be=20an=20=
abbreviated=20JPEG=20data=20stream=0A=09=0A=09//=20the=20input=20tile=20=
is=20the=20OJPEG=20data=20segment=0A=09=0A=09//=20it=20is=20missing=20=
restart=20markers=0A=09=0A=09//=20write=20the=20output=20tile=0A=09=0A=09=
for(i=3D0;i<tile_count;i++){=0A=09=09bytes_read=3DTIFFReadRawTile(input,=20=
i,=20&(((unsigned=20char*)buffer)[bufoff]),=20tile_byte_counts[i]);=0A=09=
=09if(bytes_read=3D=3D-1){=0A=09=09=09//=20error=20on=20read=20raw=20=
tile=0A=09=09=09_TIFFfree(buffer);=0A=09=09=09return(0);=0A=09=09}=0A=09=09=
if(bytes_read<tile_byte_counts[i]){=0A=09=09=09//=20error=0A=09=09}=0A=09=
=09((unsigned=20char*)buffer)[bufoff+tile_byte_counts[i]]=3D0xff;=0A=09=09=
((unsigned=20char*)buffer)[bufoff+tile_byte_counts[i]+1]=3D0xd9;=0A=09=09=
=0A=09=09bytes_written=3DTIFFWriteRawTile(output,=20i,=20buffer,=20=
bufoff+tile_byte_counts[i]+2);=0A=09=09if(bytes_written=3D=3D-1){=0A=09=09=
=09//=20error=20on=20write=20raw=20tile=0A=09=09=09_TIFFfree(buffer);=0A=09=
=09=09return(0);=0A=09=09}=0A=09=09if(bytes_written<bytes_read){=0A=09=09=
=09//=20error=0A=09=09}=0A=09}=0A=09=0A=09return(0);=0A}=0A=0A=0A=0A/*=0A=
=20=20=20=20This=20function=20gathers=20information=20about=20the=20=
currently=20set=20input=20directory=20and=20writes=20all=20=0A=20=20=20=20=
the=20tags=20of=20it,=20except=20for=20TIFFTAG_COMPRESSION,=20OJPEG,=20=
and=20JPEG=20tags,=20into=20the=20output=20directory.=0A=20=20=20=20=0A=20=
=20=20=20Each=20raw=20strip/tile=20of=20the=20input=20directory=20is=20=
read=20into=20a=20buffer=20and=20written=20raw=20to=20the=20=0A=20=20=20=20=
output=20directory.=20=20=0A=20=20=20=20=0A=20=20=20=20Before=20calling=20=
this=20function,=20set=20the=20input=20directory=20with=20=
TIFFSetDirectory.=20=20After=20=0A=20=20=20=20calling=20this=20function,=20=
write=20the=20output=20directory=20with=20TIFFWriteDirectory.=0A=20=20=20=
=20=0A*/=0A=0Aint=20oj2j_copy_directory(TIFF*=20input,=20TIFF*=20=
output){=0A=0A=09oj2j_copy_directory_basic_tags(input,=20output);=0A=09=
oj2j_copy_directory_compression_tags(input,=20output);=0A=09=
oj2j_copy_directory_other_tags(input,=20output);=0A=20=20=20=20=0A=09=
if(TIFFIsTiled(input)){=0A=09=09oj2j_copy_directory_tiles(input,=20=
output);=0A=09}=20else=20{=0A=09=09oj2j_copy_directory_strips(input,=20=
output);=0A=09}=0A=20=20=20=20=0A=09return(0);=0A=0A}=0A=0A/*=0A=09These=20=
macros=20provide=20shorthand=20for=20copying=20known=20tags.=0A*/=0A=
#define=20OJ2JCOPYTAG(tag,=20var)=20if(TIFFGetField(input,=20tag,=20\=0A=
&(var))!=3D0)=20\=0ATIFFSetField(output,=20tag,=20(var))=0A#define=20=
OJ2JCOPYTA2(tag,=20var1,=20var2)=20if(TIFFGetField(input,=20tag,=20\=0A=
&(var1),&(var2))!=3D0)=20\=0ATIFFSetField(output,=20tag,=20(var1),=20=
(var2))=0A#define=20OJ2JCOPYTA3(tag,=20var1,=20var2,=20var3)=20=
if(TIFFGetField(input,=20tag,=20\=0A&(var1),&(var2),&(var3))!=3D0)\=0A=
TIFFSetField(output,=20tag,=20(var1),=20(var2),=20(var3))=0A#define=20=
OJ2JCOPYTA4(tag,=20var1,=20var2,=20var3,=20var4)=20=
if(TIFFGetField(input,=20tag,=20\=0A&(var1),&(var2),&(var3),&(var4))!=3D0)=
=20\=0ATIFFSetField(output,=20tag,=20(var1),=20(var2),=20(var3),=20=
(var4))=0A=0A=0A/*=0A=09Copy=20"pre-compression"=20tags.=0A=0A*/=0A=0A=
int=20oj2j_copy_directory_basic_tags(TIFF*=20input,=20TIFF*=20output){=0A=
=0A=09uint32=20xuint32=3D0;=0A=09uint16=20xuint16=3D0;=0A=09uint16*=20=
xuint16p=3DNULL;=0A=09=0A=09OJ2JCOPYTAG(TIFFTAG_IMAGEWIDTH,=20xuint32);=09=
/*=20image=20width=20in=20pixels=20*/=0A=09=
OJ2JCOPYTAG(TIFFTAG_IMAGELENGTH,=20xuint32);=09/*=20image=20height=20in=20=
pixels=20*/=0A=09OJ2JCOPYTAG(TIFFTAG_BITSPERSAMPLE,=20xuint16);=09/*=20=
bits=20per=20channel=20(sample)=20*/=0A=09=
OJ2JCOPYTAG(TIFFTAG_PHOTOMETRIC,=20xuint16);=09/*=20photometric=20=
interpretation=20*/=0A=09OJ2JCOPYTAG(TIFFTAG_FILLORDER,=20xuint16);=09/*=20=
data=20order=20within=20a=20byte=20*/=0A=09=
OJ2JCOPYTAG(TIFFTAG_SAMPLESPERPIXEL,=20xuint16);=09/*=20samples=20per=20=
pixel=20*/=0A=09OJ2JCOPYTAG(TIFFTAG_PLANARCONFIG,=20xuint16);=09/*=20=
storage=20organization=20*/=0A=09OJ2JCOPYTAG(TIFFTAG_ROWSPERSTRIP,=20=
xuint32);=09/*=20rows=20per=20strip=20of=20data=20*/=0A=09=
OJ2JCOPYTAG(TIFFTAG_TILEWIDTH,=20xuint32);=09/*=20!rows/data=20tile=20*/=0A=
=09OJ2JCOPYTAG(TIFFTAG_TILELENGTH,=20xuint32);=09/*=20!cols/data=20tile=20=
*/=0A=09OJ2JCOPYTA2(TIFFTAG_EXTRASAMPLES,=20xuint16,=20xuint16p);=09/*=20=
!info=20about=20extra=20samples=20*/=0A=09OJ2JCOPYTAG(TIFFTAG_INDEXED,=20=
xuint16);=09/*=20%Indexed=20[Adobe=20TIFF=20Technote=203]=20*/=0A=0A=09=
return(1);=0A}=0A=0A/*=0A=20=20=20=20For=20the=20compression=20tag=20and=20=
related=20tags=20in=20the=20input=20TIFF's=20current=20directory,=20set=20=
=0A=20=20=20=20those=20fields'=20values=20in=20the=20output=20directory,=20=
except=20OJPEG=20tags.=0A*/=0A=0Aint=20=
oj2j_copy_directory_compression_tags(TIFF*=20input,=20TIFF*=20output){=0A=
=0A=09uint16=20compression=3D0;=0A=09=0A=09uint32=20xuint32=3D0;=0A=09=
uint16=20xuint16=3D0;=0A=09char*=20xcharp;=0A=09int=20xint=3D0;=0A=09=
void*=20xvoidp;=0A=09=0A=09TIFFGetField(input,=20TIFFTAG_COMPRESSION,=20=
&compression);=0A=09=0A=09=0A=09switch(compression){=0A=09=09case=20=
COMPRESSION_NONE:=09/*=20dump=20mode=20*/=0A=09=09=09=
TIFFSetField(output,=20TIFFTAG_COMPRESSION,=20compression);=0A=09=09=09=
break;=0A=09=09case=20COMPRESSION_CCITTRLE:=09/*=20CCITT=20modified=20=
Huffman=20RLE=20*/=0A=09=09case=20COMPRESSION_CCITTRLEW:=09/*=20#1=20w/=20=
word=20alignment=20*/=0A=09=09case=20COMPRESSION_CCITTFAX3:=09/*=20CCITT=20=
Group=203=20fax=20encoding=20*/=0A=09=09case=20COMPRESSION_CCITTFAX4:=09=
/*=20CCITT=20Group=204=20fax=20encoding=20*/=0A=09=09=09=
TIFFSetField(output,=20TIFFTAG_COMPRESSION,=20compression);=0A=09=09=09=
OJ2JCOPYTAG(TIFFTAG_GROUP3OPTIONS,=20xuint32);=0A=09=09=09=
OJ2JCOPYTAG(TIFFTAG_GROUP4OPTIONS,=20xuint32);=0A=09=09=09=
OJ2JCOPYTAG(TIFFTAG_BADFAXLINES,=20xuint32);=0A=09=09=09=
OJ2JCOPYTAG(TIFFTAG_CLEANFAXDATA,=20xuint16);=0A=09=09=09=
OJ2JCOPYTAG(TIFFTAG_CONSECUTIVEBADFAXLINES,=20xuint32);=0A=09=09=09=
OJ2JCOPYTAG(TIFFTAG_FAXRECVPARAMS,=20xuint32);=0A=09=09=09=
OJ2JCOPYTAG(TIFFTAG_FAXSUBADDRESS,=20xcharp);=0A=09=09=09=
OJ2JCOPYTAG(TIFFTAG_FAXRECVTIME,=20xuint32);=0A=09=09=09break;=0A=09=09=
case=20COMPRESSION_DEFLATE:=09/*=20Deflate=20compression=20*/=0A=09=09=
case=20COMPRESSION_ADOBE_DEFLATE:=09/*=20Deflate=20compression,=20as=20=
recognized=20by=20Adobe=20*/=0A=09=09case=20COMPRESSION_LZW:=09/*=20=
Lempel-Ziv=20=20&=20Welch=20*/=0A=09=09=09TIFFSetField(output,=20=
TIFFTAG_COMPRESSION,=20compression);=0A=09=09=09=
OJ2JCOPYTAG(TIFFTAG_PREDICTOR,=20xuint16);=0A=09=09=09break;=0A=09=09/*=20=
case=20COMPRESSION_OJPEG:=09*//*=20!6.0=20JPEG=20*/=0A=09=09case=20=
COMPRESSION_JPEG:=09/*=20%JPEG=20DCT=20compression=20*/=0A=09=09=09=
TIFFSetField(output,=20TIFFTAG_COMPRESSION,=20compression);=0A=09=09=09=
OJ2JCOPYTA2(TIFFTAG_JPEGTABLES,=20xuint32,=20xvoidp);=0A=09=09=09break;=0A=
=09=09case=20COMPRESSION_NEXT:=09/*=20NeXT=202-bit=20RLE=20*/=0A=09=09=
case=20COMPRESSION_PACKBITS:=09/*=20Macintosh=20RLE=20*/=0A=09=09case=20=
COMPRESSION_THUNDERSCAN:=09/*=20ThunderScan=20RLE=20*/=0A=09=09=09=
TIFFSetField(output,=20TIFFTAG_COMPRESSION,=20compression);=0A=09=09=09=
break;=0A=0A=09=09case=20COMPRESSION_IT8CTPAD:=09/*=20IT8=20CT=20=
w/padding=20*/=0A=09=09case=20COMPRESSION_IT8LW:=09/*=20IT8=20Linework=20=
RLE=20*/=0A=09=09case=20COMPRESSION_IT8MP:=09/*=20IT8=20Monochrome=20=
picture=20*/=0A=09=09case=20COMPRESSION_IT8BL:=09/*=20IT8=20Binary=20=
line=20art=20*/=0A=09=09=09//=20error=0A=09=09=09return(0);=0A=09=09=09=
break;=0A=0A=09=09case=20COMPRESSION_PIXARFILM:=09/*=20Pixar=20companded=20=
10bit=20LZW=20*/=0A=09=09case=20COMPRESSION_PIXARLOG:=09/*=20Pixar=20=
companded=2011bit=20ZIP=20*/=0A=09=09=09=
OJ2JCOPYTAG(TIFFTAG_PIXARLOGQUALITY,=20xint);=0A=09=09=09=
OJ2JCOPYTAG(TIFFTAG_PIXARLOGDATAFMT,=20xint);=0A=09=09=09//=20error=0A=09=
=09=09return(0);=0A=09=09=09break;=0A=0A=09/*=20compression=20code=20=
32947=20is=20reserved=20for=20Oceana=20Matrix=20<dev@oceana.com>=20*/=0A=09=
=09case=20COMPRESSION_DCS:=09/*=20Kodak=20DCS=20encoding=20*/=0A=09=09=09=
//=20error=0A=09=09=09return(0);=0A=09=09=09break;=0A=09=09case=20=
COMPRESSION_JBIG:=09/*=20ISO=20JBIG=20*/=0A=09=09=09=
OJ2JCOPYTAG(TIFFTAG_JBIGOPTIONS,=20xuint32);=0A=09=09=09break;=0A=09=09=
case=20COMPRESSION_SGILOG:=09/*=20SGI=20Log=20Luminance=20RLE=20*/=0A=09=09=
case=20=20COMPRESSION_SGILOG24:=09/*=20SGI=20Log=2024-bit=20packed=20*/=0A=
=09=09=09//=20error=0A=09=09=09return(0);=0A=09=09=09break;=0A=09=09case=20=
=20COMPRESSION_JP2000:=09/*=20Leadtools=20JPEG2000=20*/=0A=09=09=09//=20=
error=0A=09=09=09return(0);=0A=09=09=09break;=0A=09=09default:=0A=09=09=09=
//=20error=0A=09=09=09return(0);=0A=09=09=09break;=0A=09}=0A=09=0A=09=
return(1);=0A}=0A=0A/*=0A=20=20=20=20For=20each=20field=20set=20in=20the=20=
input=20TIFF's=20current=20directory,=20set=20that=20field's=20value=20=
in=20the=20=0A=20=20=20=20output=20directory,=20except=20for=20=
TIFFTAG_COMPRESSION,=20OJPEG,=20and=20JPEG=20tags.=0A*/=0Aint=20=
oj2j_copy_directory_other_tags(TIFF*=20input,=20TIFF*=20output){=0A=0A=09=
uint32=20xuint32=3D0;=0A=09uint16=20xuint16=3D0;=0A=09uint16=20=
xuint16_1=3D0;=0A=09uint16=20xuint16_2=3D0;=0A=09float=20xfloat=3D0.0;=0A=
=09dblparam_t=20xdouble=3D0.0;=0A=09char*=20xcharp=3DNULL;=0A=09float*=20=
xfloatp=3DNULL;=0A=09uint16*=20xuint16p1=3DNULL;=0A=09uint16*=20=
xuint16p2=3DNULL;=0A=09uint16*=20xuint16p3=3DNULL;=0A=09uint16*=20=
xuint16p4=3DNULL;=0A=09tdata_t=20xvoidp=3DNULL;=0A=09=0A=20=20=20=20//=20=
for=20each=20tag=20in=20input=20if=20set,=20set=20in=20ouput=0A=09=0A=09=
//=20it's=20unclear=20how=20to=20best=20do=20this=0A=09=0A=09//=20here's=20=
an=20idea,=20go=20through=20the=20field=20bits,=20TIFFFieldInfo,=20and=20=
then=20=0A=09//=20if=20it's=20known=20then=20pass=20to=20this=20switch=20=
function=0A=09=0A=09/*=0A=09=0A=09This=20function=20should=20be=20=
erroring=20for=20a=20number=20of=20reasons.=0A=09=0A=09If=20there=20are=20=
private=20tags,=20or=20tags=20of=20unknown=20format,=20then=20the=20=
function=20should=20=0A=09warn=20or=20error=20and=20fail.=0A=09=0A=09=
This=20would=20be=20coded=20into=20the=20OJ2JCOPYTAx=20macros.=0A=09=0A=09=
*/=0A=0A=09OJ2JCOPYTAG(TIFFTAG_SUBFILETYPE,=20xuint32);=09/*=20subfile=20=
data=20descriptor=20*/=0A=09OJ2JCOPYTAG(TIFFTAG_OSUBFILETYPE,=20=
xuint16);=09/*=20+kind=20of=20data=20in=20subfile=20*/=0A=09=
OJ2JCOPYTAG(TIFFTAG_THRESHHOLDING,=20xuint16);=09/*=20+thresholding=20=
used=20on=20data=20*/=0A=09OJ2JCOPYTAG(TIFFTAG_CELLWIDTH,=20xuint16);=09=
/*=20+dithering=20matrix=20width=20*/=0A=09=
OJ2JCOPYTAG(TIFFTAG_CELLLENGTH,=20xuint16);=09/*=20+dithering=20matrix=20=
height=20*/=0A=09OJ2JCOPYTAG(TIFFTAG_DOCUMENTNAME,=20xcharp);=09/*=20=
name=20of=20doc.=20image=20is=20from=20*/=0A=09=
OJ2JCOPYTAG(TIFFTAG_IMAGEDESCRIPTION,=20xcharp);=09/*=20info=20about=20=
image=20*/=0A=09OJ2JCOPYTAG(TIFFTAG_MAKE,=20xcharp);=09/*=20scanner=20=
manufacturer=20name=20*/=0A=09OJ2JCOPYTAG(TIFFTAG_MODEL,=20xcharp);=09/*=20=
scanner=20model=20name/number=20*/=0A=09OJ2JCOPYTAG(TIFFTAG_ORIENTATION,=20=
xuint16);=09/*=20+image=20orientation=20*/=0A=09=
OJ2JCOPYTAG(TIFFTAG_MINSAMPLEVALUE,=20xuint16);=09/*=20+minimum=20sample=20=
value=20*/=0A=09OJ2JCOPYTAG(TIFFTAG_MAXSAMPLEVALUE,=20xuint16);=09/*=20=
+maximum=20sample=20value=20*/=0A=09OJ2JCOPYTAG(TIFFTAG_XRESOLUTION,=20=
xfloat);=09/*=20pixels/resolution=20in=20x=20*/=0A=09=
OJ2JCOPYTAG(TIFFTAG_YRESOLUTION,=20xfloat);=09/*=20pixels/resolution=20=
in=20y=20*/=0A=09OJ2JCOPYTAG(TIFFTAG_PAGENAME,=20xcharp);=09/*=20page=20=
name=20image=20is=20from=20*/=0A=09OJ2JCOPYTAG(TIFFTAG_XPOSITION,=20=
xfloat);=09/*=20x=20page=20offset=20of=20image=20lhs=20*/=0A=09=
OJ2JCOPYTAG(TIFFTAG_YPOSITION,=20xfloat);=09/*=20y=20page=20offset=20of=20=
image=20lhs=20*/=0A=09/*=09TIFFTAG_FREEOFFSETS=20-=20lost=20*/=0A=09/*=09=
TIFFTAG_FREEBYTECOUNTS=20-=20lost=20*/=0A=09=
OJ2JCOPYTAG(TIFFTAG_GRAYRESPONSEUNIT,=20xuint16);=09/*=20$gray=20scale=20=
curve=20accuracy=20*/=0A=09OJ2JCOPYTAG(TIFFTAG_GRAYRESPONSECURVE,=20=
xuint16);=09/*=20$gray=20scale=20response=20curve=20*/=0A=09=
OJ2JCOPYTAG(TIFFTAG_RESOLUTIONUNIT,=20xuint16);=09/*=20units=20of=20=
resolutions=20*/=0A=09OJ2JCOPYTA2(TIFFTAG_PAGENUMBER,=20xuint16_1,=20=
xuint16_2);=09/*=20page=20numbers=20of=20multi-page=20*/=0A=09=
OJ2JCOPYTAG(TIFFTAG_COLORRESPONSEUNIT,=20xuint16);=09/*=20$color=20curve=20=
accuracy=20*/=0A=09OJ2JCOPYTA3(TIFFTAG_TRANSFERFUNCTION,=20xuint16p1,=20=
xuint16p2,=20xuint16p3);=09/*=20!colorimetry=20info=20*/=0A=09=
OJ2JCOPYTAG(TIFFTAG_SOFTWARE,=20xcharp);=09/*=20name=20&=20release=20*/=0A=
=09OJ2JCOPYTAG(TIFFTAG_DATETIME,=20xcharp);=09/*=20creation=20date=20and=20=
time=20*/=0A=09OJ2JCOPYTAG(TIFFTAG_ARTIST,=20xcharp);=09/*=20creator=20=
of=20image=20*/=0A=09OJ2JCOPYTAG(TIFFTAG_HOSTCOMPUTER,=20xcharp);=09/*=20=
machine=20where=20created=20*/=0A=09OJ2JCOPYTAG(TIFFTAG_WHITEPOINT,=20=
xfloatp);=09/*=20image=20white=20point=20*/=0A=09=
OJ2JCOPYTAG(TIFFTAG_PRIMARYCHROMATICITIES,=20xfloatp);=09/*=20!primary=20=
chromaticities=20*/=0A=09OJ2JCOPYTA4(TIFFTAG_COLORMAP,=20xuint16p1,=20=
xuint16p2,=20xuint16p3,=20xuint16p4);=09/*=20palette=20image=20*/=0A=09=
OJ2JCOPYTA2(TIFFTAG_HALFTONEHINTS,=20xuint16_1,=20xuint16_2);=09/*=20=
!highlight+shadow=20info=20*/=0A=09OJ2JCOPYTAG(TIFFTAG_INKSET,=20=
xuint16);=09/*=20!inks=20in=20separated=20image=20*/=0A=09=
OJ2JCOPYTA2(TIFFTAG_INKNAMES,=20xuint16,=20xcharp);=09/*=20!ascii=20=
names=20of=20inks=20*/=0A=09OJ2JCOPYTAG(TIFFTAG_NUMBEROFINKS,=20=
xuint16);=09/*=20!number=20of=20inks=20*/=0A=09=
OJ2JCOPYTA2(TIFFTAG_DOTRANGE,=20xuint16_1,=20xuint16_2);=09/*=20!0%=20=
and=20100%=20dot=20codes=20*/=0A=09OJ2JCOPYTAG(TIFFTAG_TARGETPRINTER,=20=
xcharp);=09/*=20!separation=20target=20*/=0A=09=
OJ2JCOPYTAG(TIFFTAG_SAMPLEFORMAT,=20xuint16);=09/*=20!data=20sample=20=
format=20*/=0A=09OJ2JCOPYTAG(TIFFTAG_SMINSAMPLEVALUE,=20xdouble);=09/*=20=
!variable=20MinSampleValue=20*/=0A=09=
OJ2JCOPYTAG(TIFFTAG_SMAXSAMPLEVALUE,=20xdouble);=09/*=20!variable=20=
MaxSampleValue=20*/=0A=09OJ2JCOPYTA2(TIFFTAG_CLIPPATH,=20xuint32,=20=
xvoidp);=09/*=20%ClipPath=20[Adobe=20TIFF=20technote=202]=20*/=0A=09=
OJ2JCOPYTAG(TIFFTAG_XCLIPPATHUNITS,=20xuint32);=09/*=20%XClipPathUnits=20=
[Adobe=20TIFF=20technote=202]=20*/=0A=09=
OJ2JCOPYTAG(TIFFTAG_YCLIPPATHUNITS,=20xuint32);=09/*=20%YClipPathUnits=20=
[Adobe=20TIFF=20technote=202]=20*/=0A=09OJ2JCOPYTAG(TIFFTAG_OPIPROXY,=20=
xuint16);=09/*=20%OPI=20Proxy=20[Adobe=20TIFF=20technote]=20*/=0A=0A=09=
OJ2JCOPYTAG(TIFFTAG_YCBCRCOEFFICIENTS,=20xfloatp);=09/*=20!RGB=20->=20=
YCbCr=20transform=20*/=0A=09OJ2JCOPYTA2(TIFFTAG_YCBCRSUBSAMPLING,=20=
xuint16_1,=20xuint16_2);=09/*=20!YCbCr=20subsampling=20factors=20*/=0A=09=
OJ2JCOPYTAG(TIFFTAG_YCBCRPOSITIONING,=20xuint16);=09/*=20!subsample=20=
positioning=20*/=0A=09OJ2JCOPYTAG(TIFFTAG_REFERENCEBLACKWHITE,=20=
xfloatp);=09/*=20!colorimetry=20info=20*/=0A=09=
OJ2JCOPYTA2(TIFFTAG_XMLPACKET,=20xuint32,=20xvoidp);=09/*=20%XML=20=
packet=20[Adobe=20XMP=20technote=209-14-02]=20(dkelly@apago.com)=20*/=0A=09=
OJ2JCOPYTAG(TIFFTAG_OPIIMAGEID,=20xcharp);=09/*=20%OPI=20ImageID=20=
[Adobe=20TIFF=20technote]=20*/=0A/*=20tags=2032952-32956=20are=20private=20=
tags=20registered=20to=20Island=20Graphics=20*/=0A/*=20=
OJ2JCOPYTAG(TIFFTAG_REFPTS=20*/=09/*=20image=20reference=20points=20*/=0A=
/*=20OJ2JCOPYTAG(TIFFTAG_REGIONTACKPOINT=20*/=09/*=20region-xform=20tack=20=
point=20*/=0A/*=20OJ2JCOPYTAG(TIFFTAG_REGIONWARPCORNERS=20*/=09/*=20warp=20=
quadrilateral=20*/=0A/*=20OJ2JCOPYTAG(TIFFTAG_REGIONAFFINE=20*/=09/*=20=
affine=20transformation=20mat=20*/=0A/*=20tags=2032995-32999=20are=20=
private=20tags=20registered=20to=20SGI=20*/=0A=09=
OJ2JCOPYTAG(TIFFTAG_MATTEING,=20xuint16);=09/*=20$use=20ExtraSamples=20=
*/=0A=09OJ2JCOPYTAG(TIFFTAG_DATATYPE,=20xuint16);=09/*=20$use=20=
SampleFormat=20*/=0A=09OJ2JCOPYTAG(TIFFTAG_IMAGEDEPTH,=20xuint32);=09/*=20=
z=20depth=20of=20image=20*/=0A=09OJ2JCOPYTAG(TIFFTAG_TILEDEPTH,=20=
xuint32);=09/*=20z=20depth/data=20tile=20*/=0A/*=20tags=2033300-33309=20=
are=20private=20tags=20registered=20to=20Pixar=20*/=0A/*=0A=20*=20=
TIFFTAG_PIXAR_IMAGEFULLWIDTH=20and=20TIFFTAG_PIXAR_IMAGEFULLLENGTH=0A=20=
*=20are=20set=20when=20an=20image=20has=20been=20cropped=20out=20of=20a=20=
larger=20image.=20=20=0A=20*=20They=20reflect=20the=20size=20of=20the=20=
original=20uncropped=20image.=0A=20*=20The=20TIFFTAG_XPOSITION=20and=20=
TIFFTAG_YPOSITION=20can=20be=20used=0A=20*=20to=20determine=20the=20=
position=20of=20the=20smaller=20image=20in=20the=20larger=20one.=0A=20*/=0A=
=09OJ2JCOPYTAG(TIFFTAG_PIXAR_IMAGEFULLWIDTH,=20xuint32);=09/*=20full=20=
image=20size=20in=20x=20*/=0A=09=
OJ2JCOPYTAG(TIFFTAG_PIXAR_IMAGEFULLLENGTH,=20xuint32);=09/*=20full=20=
image=20size=20in=20y=20*/=0A=20/*=20Tags=2033302-33306=20are=20used=20=
to=20identify=20special=20image=20modes=20and=20data=0A=20=20*=20used=20=
by=20Pixar's=20texture=20formats.=0A=20=20*/=0A=09=
OJ2JCOPYTAG(TIFFTAG_PIXAR_TEXTUREFORMAT,=20xcharp);=09/*=20texture=20map=20=
format=20*/=0A=09OJ2JCOPYTAG(TIFFTAG_PIXAR_WRAPMODES,=20xcharp);=09/*=20=
s=20&=20t=20wrap=20modes=20*/=0A=09OJ2JCOPYTAG(TIFFTAG_PIXAR_FOVCOT,=20=
xdouble);=09/*=20cotan(fov)=20for=20env.=20maps=20*/=0A=09=
OJ2JCOPYTAG(TIFFTAG_PIXAR_MATRIX_WORLDTOSCREEN,=20xfloatp);=0A=09=
OJ2JCOPYTAG(TIFFTAG_PIXAR_MATRIX_WORLDTOCAMERA,=20xfloatp);=0A/*=20tag=20=
33405=20is=20a=20private=20tag=20registered=20to=20Eastman=20Kodak=20*/=0A=
/*=20OJ2JCOPYTAG(TIFFTAG_WRITERSERIALNUMBER=20*/=09/*=20device=20serial=20=
number=20*/=0A/*=20tag=2033432=20is=20listed=20in=20the=206.0=20spec=20=
w/=20unknown=20ownership=20*/=0A=09OJ2JCOPYTAG(TIFFTAG_COPYRIGHT,=20=
xcharp);=09/*=20copyright=20string=20*/=0A/*=20IPTC=20TAG=20from=20=
RichTIFF=20specifications=20*/=0A=09OJ2JCOPYTA2(TIFFTAG_RICHTIFFIPTC,=20=
xuint32,=20xvoidp);=0A/*=20tags=2034232-34236=20are=20private=20tags=20=
registered=20to=20Texas=20Instruments=20*/=0A/*=20=
OJ2JCOPYTAG(TIFFTAG_FRAMECOUNT=20*/=09/*=20Sequence=20Frame=20Count=20*/=0A=
/*=20tag=2034750=20is=20a=20private=20tag=20registered=20to=20Adobe?=20=
*/=0A=09OJ2JCOPYTA2(TIFFTAG_ICCPROFILE,=20xuint32,=20xvoidp);=09/*=20ICC=20=
profile=20data=20*/=0A/*=20tag=2034377=20is=20private=20tag=20registered=20=
to=20Adobe=20for=20PhotoShop=20*/=0A=09OJ2JCOPYTA2(TIFFTAG_PHOTOSHOP,=20=
xuint32,=20xvoidp);=0A/*=20tags=2037439-37443=20are=20registered=20to=20=
SGI=20<gregl@sgi.com>=20*/=0A=09OJ2JCOPYTAG(TIFFTAG_STONITS,=20xdouble);=09=
/*=20Sample=20value=20to=20Nits=20*/=0A/*=20tag=2034929=20is=20a=20=
private=20tag=20registered=20to=20FedEx=20*/=0A/*=20=
OJ2JCOPYTAG(TIFFTAG_FEDEX_EDR=20*/=09/*=20unknown=20use=20*/=0A=0A=0A=09=
//=20go=20through=20the=20remaining=20tags=0A=0A=09return(0);=0A}=0A=0A=
/*=0A=20=20=20=20This=20function=20copies=20the=20strips=20of=20the=20=
input=20TIFF=20IFD=20to=20the=20output=20TIFF=20IFD.=0A*/=0A=0Aint=20=
oj2j_copy_directory_strips(TIFF*=20input,=20TIFF*=20output){=0A=20=20=20=20=
=0A=09//=20for=20each=20strip=20of=20input=20in=20order=20read=20the=20=
raw=20strip=20and=20write=20it=20to=20output=0A=09tstrip_t=20=
strip_count=3D0;=0A=09tstrip_t=20i=3D0;=0A=09uint32*=20=
strip_byte_counts=3DNULL;=0A=09uint32=20max_strip_byte_count=3D0;=0A=09=
tdata_t=20buffer=3DNULL;=0A=09tsize_t=20bytes_read=3D0;=0A=09tsize_t=20=
bytes_written=3D0;=0A=20=20=20=20=0A=09=
strip_count=3DTIFFNumberOfStrips(input);=0A=20=20=20=20=0A=09=
TIFFGetField(input,=20TIFFTAG_STRIPBYTECOUNTS,=20&strip_byte_counts);=0A=20=
=20=20=20=0A=09//=20allocate=20buffer=20of=20maximum=20strip=20size=0A=09=
for(i=3D0;i<strip_count;i++){=0A=09=09=
if(strip_byte_counts[i]>max_strip_byte_count){=0A=09=09=09=
max_strip_byte_count=3Dstrip_byte_counts[i];=0A=09=09}=0A=09}=0A=20=20=20=
=20=0A=09buffer=20=3D=20_TIFFmalloc(max_strip_byte_count);=0A=09=
if(buffer=3D=3DNULL){=0A=09=09//=20error=0A=09=09return(0);=0A=09}=0A=20=20=
=20=20=0A=09for(i=3D0;i<strip_count;i++){=0A=09=09=
bytes_read=3DTIFFReadRawStrip(input,=20i,=20buffer,=20=
strip_byte_counts[i]);=0A=09=09if(bytes_read=3D=3D-1){=0A=09=09=09//=20=
error=20on=20read=20raw=20strip=0A=09=09=09_TIFFfree(buffer);=0A=09=09=09=
return(0);=0A=09=09}=0A=09=09if(bytes_read<strip_byte_counts[i]){=0A=09=09=
=09//=20error=0A=09=09}=0A=09=09bytes_written=3DTIFFWriteRawStrip(output,=20=
i,=20buffer,=20bytes_read);=0A=09=09if(bytes_written=3D=3D-1){=0A=09=09=09=
//=20error=20on=20write=20raw=20strip=0A=09=09=09_TIFFfree(buffer);=0A=09=
=09=09return(0);=0A=09=09}=0A=09=09if(bytes_written<bytes_read){=0A=09=09=
=09//=20error=0A=09=09}=0A=09}=0A=20=20=20=20=0A=09return(i);=0A}=0A=0A=
int=20oj2j_copy_directory_tiles(TIFF*=20input,=20TIFF*=20output){=0A=20=20=
=20=20//=20for=20each=20tile=20of=20input=20in=20order=20read=20the=20=
raw=20tile=20and=20write=20it=20to=20output=0A=09ttile_t=20tile_count=3D0;=
=0A=09ttile_t=20i=3D0;=0A=09uint32*=20tile_byte_counts=3DNULL;=0A=09=
uint32=20max_tile_byte_count=3D0;=0A=09tdata_t=20buffer=3DNULL;=0A=09=
tsize_t=20bytes_read=3D0;=0A=09tsize_t=20bytes_written=3D0;=0A=20=20=20=20=
=0A=09tile_count=3DTIFFNumberOfTiles(input);=0A=20=20=20=20=0A=09=
TIFFGetField(input,=20TIFFTAG_TILEBYTECOUNTS,=20&tile_byte_counts);=0A=20=
=20=20=20=0A=20=20=20=20//=20allocate=20buffer=20of=20maximum=20tile=20=
size=0A=09for(i=3D0;i<tile_count;i++){=0A=09=09=
if(tile_byte_counts[i]>max_tile_byte_count){=0A=09=09=09=
max_tile_byte_count=3Dtile_byte_counts[i];=0A=09=09}=0A=09}=0A=20=20=20=20=
=0A=09buffer=20=3D=20_TIFFmalloc(max_tile_byte_count);=0A=09=
if(buffer=3D=3DNULL){=0A=09=09//=20error=0A=09=09return(0);=0A=09}=0A=20=20=
=20=20=0A=09for(i=3D0;i<tile_count;i++){=0A=09=09=
bytes_read=3DTIFFReadRawTile(input,=20i,=20buffer,=20=
tile_byte_counts[i]);=0A=09=09if(bytes_read=3D=3D-1){=0A=09=09=09//=20=
error=20on=20read=20raw=20strip=0A=09=09=09_TIFFfree(buffer);=0A=09=09=09=
return(0);=0A=09=09}=0A=09=09if(bytes_read<tile_byte_counts[i]){=0A=09=09=
=09//=20error=0A=09=09}=0A=09=09bytes_written=3DTIFFWriteRawTile(output,=20=
i,=20buffer,=20bytes_read);=0A=09=09if(bytes_written=3D=3D-1){=0A=09=09=09=
//=20error=20on=20write=20raw=20strip=0A=09=09=09_TIFFfree(buffer);=0A=09=
=09=09return(0);=0A=09=09}=0A=09=09if(bytes_written<bytes_read){=0A=09=09=
=09//=20error=0A=09=09}=0A=09}=0A=20=20=20=20=0A=09return(i);=0A=0A}=0A=0A=
=0A//=20functions=20to=20get=20JPEG=20marker=20segments=20from=20OJPEG=20=
data=0A=0A//=20have=20them=20run=20off=20of=20the=20OJ2J=20struct=0A=0A=0A=
/*=0A=09There=20are=20four=20table=20destinations=20each=20for=20=
Quantization,=20Huffman/AC,=20and=20Huffman/DC=20in=20the=20JPEG=20=
decoder,=20=0A=09baseline=20process=20allows=20only=20two=20for=20=
Huffman=20tables,=20there=20are=20component=20count=20many=20in=20the=20=
OJPEG=20file,=20=0A=09for=20component=20count=20>=202=20some=20are=20=
duplicates.=20=0A*/=0Aint=20oj2j_count_tables(OJ2J*=20oj2j){=0A=0A=09=
oj2j->oj2j_table_count=3Doj2j->oj2j_component_count;=0A=09=0A=09=
if(oj2j->oj2j_jpegproc=20=3D=3D=20JPEGPROC_BASELINE){=0A=09=09=
if(oj2j->oj2j_table_count=20>=202){=0A=09=09=09oj2j->oj2j_table_count=3D2;=
=0A=09=09}=0A=09}=0A=0A=09//=20check=20if=20DQTs=20are=20identical=0A=09=0A=
=09//=20actually=20compare=20the=20tables=20for=20equality=20=0A=0A=09=
return(0);=0A}=0A=0A/*=0A=20=20=20=20This=20function=20allocates=20a=20=
buffer=20and=20fills=20it=20with=20an=20SOFn=20marker=20segment=20from=20=
the=20input=20data.=0A=20=20=20=20=0A=20=20=20=20Caller=20is=20=
responsible=20for=20freeing=20the=20SOFn=20marker=20with=20_TIFFfree.=0A=20=
=20=20=20=0A=20=20=20=20For=20JPEGProc=201,=20the=20marker=20is=20SOF0.=20=
=20For=20JPEGProc=2014,=20the=20marker=20is=20SOF3.=0A=20=20=20=20=0A*/=0A=
=0Aint=20oj2j_allocate_SOF(OJ2J*=20oj2j){=0A=0A=09=
oj2j->oj2j_SOF_length=3D10;=0A=09oj2j->oj2j_SOF_length+=3D=203=20*=20=
oj2j->oj2j_component_count;=0A=09=0A=09oj2j->oj2j_SOF=3D=20=
_TIFFmalloc(oj2j->oj2j_SOF_length);=0A=09if(oj2j->oj2j_SOF=3D=3DNULL){=0A=
=09=09//=20error=0A=09=09return(1);=0A=09}=0A=09=
_TIFFmemset(oj2j->oj2j_SOF,=200x00,=20oj2j->oj2j_SOF_length);=0A=09=0A=09=
return(0);=0A}=0A=0Aint=20oj2j_reconstitute_SOF(OJ2J*=20oj2j){=0A=0A=09=
int=20i=3D0;=0A=09=0A=09((unsigned=20char*)(oj2j->oj2j_SOF))[0]=3D0xff;=0A=
=09if(oj2j->oj2j_jpegproc=3D=3DJPEGPROC_BASELINE){=0A=09=09((unsigned=20=
char*)(oj2j->oj2j_SOF))[1]=3D0xc0;=0A=09}=20else=20{=0A=09=09((unsigned=20=
char*)(oj2j->oj2j_SOF))[1]=3D0xc3;=0A=09}=0A=0A=09((unsigned=20=
char*)(oj2j->oj2j_SOF))[2]=3D0;=0A=09((unsigned=20=
char*)(oj2j->oj2j_SOF))[3]=3D(oj2j->oj2j_SOF_length-2);=0A=09=0A=09=
((unsigned=20char*)(oj2j->oj2j_SOF))[4]=3Doj2j->oj2j_image_bps=20&=20=
0xff;=20/*=20P,=20sample=20precision=20*/=0A=09=0A=09((unsigned=20=
char*)(oj2j->oj2j_SOF))[5]=3D(oj2j->oj2j_image_height=20>>=208)=20&=20=
0xff;=0A=09((unsigned=20=
char*)(oj2j->oj2j_SOF))[6]=3D(oj2j->oj2j_image_height)=20&=200xff;=0A=09=
((unsigned=20char*)(oj2j->oj2j_SOF))[7]=3D(oj2j->oj2j_image_width=20>>=20=
8)=20&=200xff;=0A=09((unsigned=20=
char*)(oj2j->oj2j_SOF))[8]=3D(oj2j->oj2j_image_width)=20&=200xff;=0A=0A=09=
((unsigned=20char*)(oj2j->oj2j_SOF))[9]=3D(oj2j->oj2j_component_count)=20=
&=200xff;=0A=0A=09for(i=3D0;i<oj2j->oj2j_component_count;i++){=0A=09=09=
((unsigned=20char*)(oj2j->oj2j_SOF))[(i*3)+10]=3D=20i=20&=200xff;=0A=09=09=
if(i=3D=3D0){=0A=09=09=09((unsigned=20char*)(oj2j->oj2j_SOF))[(i*3)+11]=20=
|=3D=20((oj2j->oj2j_h_samp<<4)=20&=200xf0);=0A=09=09=09((unsigned=20=
char*)(oj2j->oj2j_SOF))[(i*3)+11]=20|=3D=20(oj2j->oj2j_v_samp)=20&=20=
0x0f;=0A=09=09}=20else=20{=0A=09=09=09((unsigned=20=
char*)(oj2j->oj2j_SOF))[(i*3)+11]=20|=3D=200x10;=0A=09=09=09((unsigned=20=
char*)(oj2j->oj2j_SOF))[(i*3)+11]=20|=3D=200x01;=0A=09=09}=0A=09=09=
((unsigned=20char*)(oj2j->oj2j_SOF))[(i*3)+12]=3D=20i=20&=200xff;=0A=09}=0A=
=0A=09return(0);=0A}=0A=0A=0A/*=0A=20=20=20=20This=20function=20=
allocates=20a=20buffer=20and=20fills=20it=20with=20an=20SOS=20marker=20=
segment=20from=20the=20input=20data.=0A=20=20=20=20=0A=20=20=20=20Caller=20=
is=20responsible=20for=20freeing=20the=20SOS=20marker=20with=20=
_TIFFfree.=0A=0A=20=20=20=20"The=20predictors=20[Px],=20one=20of=20which=20=
is=20selected=20in=20the=20scan=20header..."=20SOS->Ss=0A=20=20=20=20=
point=20transform=20is=20with=20predictor=20SOS->Al=0A=20=20=20=20=0A*/=0A=
=0Aint=20oj2j_allocate_SOS(OJ2J*=20oj2j){=0A=0A=09=
oj2j->oj2j_SOS_length=3D8;=0A=09oj2j->oj2j_SOS_length+=3D=202=20*=20=
oj2j->oj2j_component_count;=0A=09=0A=09oj2j->oj2j_SOS=3D=20=
_TIFFmalloc(oj2j->oj2j_SOS_length);=0A=09if(oj2j->oj2j_SOS=3D=3DNULL){=0A=
=09=09//=20error=0A=09=09return(1);=0A=09}=0A=09=
_TIFFmemset(oj2j->oj2j_SOS,=200x00,=20oj2j->oj2j_SOS_length);=0A=09=0A=09=
return(0);=0A}=0A=0Aint=20oj2j_reconstitute_SOS(OJ2J*=20oj2j){=0A=0A=09=
int=20i=3D0;=0A=0A=09((unsigned=20char*)(oj2j->oj2j_SOS))[0]=3D0xff;=0A=09=
((unsigned=20char*)(oj2j->oj2j_SOS))[1]=3D0xda;=0A=09=0A=09((unsigned=20=
char*)(oj2j->oj2j_SOS))[2]=3D0;=0A=09((unsigned=20=
char*)(oj2j->oj2j_SOS))[3]=3D(oj2j->oj2j_SOS_length-2);=0A=09=0A=09=
((unsigned=20char*)(oj2j->oj2j_SOS))[4]=3D(oj2j->oj2j_component_count)=20=
&=200xff;=0A=0A=09for(i=3D0;i<oj2j->oj2j_component_count;i++){=0A=09=09=
((unsigned=20char*)(oj2j->oj2j_SOS))[(i*2)+5]=3D=20i=20&=200xff;=0A=09=09=
//=20high=20four=20bits=20of=20next=20byte=20is=20DC=20table=20location=0A=
=09=09//=20low=20four=20bits=20of=20next=20byte=20is=20AC=20table=20=
location=20for=20lossy=0A=09=09//=20lossy=20baseline=20table=20is=20only=20=
0=20or=201=0A=09=09if(oj2j->oj2j_jpegproc=3D=3DJPEGPROC_LOSSLESS){=0A=09=09=
=09((unsigned=20char*)(oj2j->oj2j_SOS))[(i*2)+6]=20|=3D=20((i<<4)=20&=20=
0xf0);=0A=09=09}=20else=20if(oj2j->oj2j_jpegproc=3D=3DJPEGPROC_BASELINE){=0A=
=09=09=09((unsigned=20char*)(oj2j->oj2j_SOS))[(i*2)+6]=20|=3D=20=0A=09=09=
=09=09(((i>(oj2j->oj2j_table_count-1)?(oj2j->oj2j_table_count-1):i)<<4)=20=
&=200xf0);=0A=09=09=09=0A=09=09=09((unsigned=20=
char*)(oj2j->oj2j_SOS))[(i*2)+6]=20|=3D=20=0A=09=09=09=09=
(i>(oj2j->oj2j_table_count-1)?(oj2j->oj2j_table_count-1):i)=20&=200x0f;=0A=
=09=09}=0A=09}=0A=09switch(oj2j->oj2j_jpegproc){=0A=09=09case=20=
JPEGPROC_BASELINE:=0A=09=09=09((unsigned=20=
char*)(oj2j->oj2j_SOS))[(i*2)+6]=20=3D=200x3f;=0A=09=09=09//=20set=20Ah=20=
and=20Al=20as=20required,=20from=20oj2j->oj2j_jpegpointtransform[scan]?=0A=
=09=09=09break;=0A=09=09case=20JPEGPROC_LOSSLESS:=0A=09=09=09((unsigned=20=
char*)(oj2j->oj2j_SOS))[(i*2)+5]=20=3D=20=0A=09=09=09=09=
((uint16*)oj2j->oj2j_jpeglosslesspredictors)[i]=20&=200xff;=0A=09=09=09=
((unsigned=20char*)(oj2j->oj2j_SOS))[(i*2)+7]=20|=3D=20=0A=09=09=09=09=
((uint16*)oj2j->oj2j_jpegpointtransform)[i]=20&=200x0f;=0A=09=09=09=09=0A=
=09=09=09//=20Ss=20set=20as=20required,=20from=20=
oj2j->oj2j_jpeglosslesspredictors[scan]?=0A=09=09=09//=20set=20Al=20as=20=
required,=20from=20oj2j->oj2j_jpegpointtransform[scan]?=0A=09=09=09=
break;=0A=09=09default:=0A=09=09=09break;=0A=09}=0A=0A=09return(0);=0A}=0A=
=0A=0A/*=0A=20=20=20=20This=20function=20allocates=20a=20buffer=20and=20=
fills=20it=20with=20a=20DQT=20marker=20segment=20from=20the=20input=20=
data.=0A=20=20=20=20=0A=20=20=20=20Caller=20is=20responsible=20for=20=
freeing=20DQT=20buffer=20with=20_TIFFfree.=0A*/=0A=0Aint=20=
oj2j_allocate_DQTs(OJ2J*=20oj2j){=0A=0A=09oj2j->oj2j_DQTs_length=3D5;=0A=09=
oj2j->oj2j_DQTs_length+=3D64;=0A=09oj2j->oj2j_DQTs_length=20*=3D=20=
oj2j->oj2j_component_count;=0A=0A=09oj2j->oj2j_DQTs=20=3D=20=
_TIFFmalloc(oj2j->oj2j_DQTs_length);=0A=09if(oj2j->oj2j_DQTs=20=3D=3D=20=
NULL){=0A=09=09return(1);=0A=09}=0A=09_TIFFmemset(oj2j->oj2j_DQTs,=20=
0x00,=20oj2j->oj2j_DQTs_length);=0A=09=0A=09return(0);=0A}=0A=0Aint=20=
oj2j_reconstitute_DQTs(OJ2J*=20oj2j){=0A=0A=09int=20i=3D0;=0A=09int=20=
dest=3D0;=0A=09uint32=20offset=3D0;=0A=0A=09=
for(dest=3D0;dest<oj2j->oj2j_component_count;dest++){=0A=09=09((unsigned=20=
char*)(oj2j->oj2j_DQTs))[0+offset]=3D0xff;=0A=09=09((unsigned=20=
char*)(oj2j->oj2j_DQTs))[1+offset]=3D0xdb;=0A=09=09((unsigned=20=
char*)(oj2j->oj2j_DQTs))[2+offset]=3D0;=0A=09=09((unsigned=20=
char*)(oj2j->oj2j_DQTs))[3+offset]=3D67;=0A=09=09((unsigned=20=
char*)(oj2j->oj2j_DQTs))[4+offset]=3D=20dest=20&=200x0f;=0A=09=09=
for(i=3D0;i<64;i++){=0A=09=09=09((unsigned=20=
char*)(oj2j->oj2j_DQTs))[i+5+offset]=3D=20=0A=09=09=09=09((unsigned=20=
char*)(oj2j->oj2j_jpegqtables))[(dest*64)=20+=20i];=0A=09=09}=0A=09=09=
offset=20+=3D69;=0A=09}=0A=09=0A=09return(0);=0A}=0A=0A/*=0A=20=20=20=20=
This=20function=20allocates=20a=20buffer=20and=20fills=20it=20with=20a=20=
DHT=20marker=20segment=20from=20the=20input=20data.=0A=20=20=20=20=0A=20=20=
=20=20Caller=20is=20responsible=20for=20freeing=20DHT=20buffer=20with=20=
_TIFFfree.=0A*/=0A=0A=0Aint=20oj2j_allocate_DHT_ACs(OJ2J*=20oj2j){=0A=0A=09=
oj2j->oj2j_DHT_ACs_length=3D5;=0A=09oj2j->oj2j_DHT_ACs_length+=3D272;=0A=09=
oj2j->oj2j_DHT_ACs_length=20*=3D=20oj2j->oj2j_component_count;=0A=0A=09=
oj2j->oj2j_DHT_ACs=20=3D=20_TIFFmalloc(oj2j->oj2j_DHT_ACs_length);=0A=09=
if(oj2j->oj2j_DHT_ACs=20=3D=3D=20NULL){=0A=09=09return(1);=0A=09}=0A=09=
_TIFFmemset(oj2j->oj2j_DHT_ACs,=200x00,=20oj2j->oj2j_DHT_ACs_length);=0A=0A=
=09return(0);=0A}=0A=0Aint=20oj2j_reconstitute_DHT_ACs(OJ2J*=20oj2j){=0A=0A=
=09int=20i=3D0;=0A=09int=20dest=3D0;=0A=09uint32=20ms_offset=3D0;=0A=09=
uint32=20code_count=3D0;=0A=09uint32=20table_offset=3D0;=0A=09=0A=09=
for(dest=3D0;dest<oj2j->oj2j_table_count;dest++){=0A=09=09((unsigned=20=
char*)(oj2j->oj2j_DHT_ACs))[0+ms_offset]=3D0xff;=0A=09=09((unsigned=20=
char*)(oj2j->oj2j_DHT_ACs))[1+ms_offset]=3D0xc4;=0A=09=09((unsigned=20=
char*)(oj2j->oj2j_DHT_ACs))[4+ms_offset]=20|=3D=200x10;=0A=09=09=
((unsigned=20char*)(oj2j->oj2j_DHT_ACs))[4+ms_offset]=20|=3D=20dest=20&=20=
0x0f;=0A=09=09code_count=3D0;=0A=09=09for(i=3D0;i<16;i++){=0A=09=09=09=
((unsigned=20char*)(oj2j->oj2j_DHT_ACs))[i+5+ms_offset]=3D=20=0A=09=09=09=
=09((unsigned=20char*)(oj2j->oj2j_jpegactables))[i+table_offset];=0A=09=09=
=09code_count+=3D((unsigned=20=
char*)(oj2j->oj2j_DHT_ACs))[i+5+ms_offset];=0A=09=09}=0A=09=09=
table_offset+=3D16;=0A=09=09((unsigned=20=
char*)(oj2j->oj2j_DHT_ACs))[2+ms_offset]=3D((code_count+19)=20>>=208)=20=
&=200xff;=0A=09=09((unsigned=20=
char*)(oj2j->oj2j_DHT_ACs))[3+ms_offset]=3D(code_count+19)=20&=200xff;=0A=
=09=09for(i=3D0;i<code_count;i++){=0A=09=09=09((unsigned=20=
char*)(oj2j->oj2j_DHT_ACs))[i+21+ms_offset]=3D=0A=09=09=09=09((unsigned=20=
char*)(oj2j->oj2j_jpegactables))[i+table_offset];=0A=09=09}=0A=09=09=
table_offset+=3Dcode_count;=0A=09=09ms_offset+=3D21;=0A=09=09=
ms_offset+=3Dcode_count;=0A=09}=0A=09=
oj2j->oj2j_DHT_ACs_length=3Dms_offset;=0A=09=0A=09return(0);=0A}=0A=0A/*=0A=
=20=20=20=20This=20function=20allocates=20a=20buffer=20and=20fills=20it=20=
with=20a=20DHT=20marker=20segment=20from=20the=20input=20data.=0A=20=20=20=
=20=0A=20=20=20=20Caller=20is=20responsible=20for=20freeing=20DHT=20=
buffer=20with=20_TIFFfree.=0A*/=0A=0Aint=20oj2j_allocate_DHT_DCs(OJ2J*=20=
oj2j){=0A=0A=09oj2j->oj2j_DHT_DCs_length=3D5;=0A=09=
oj2j->oj2j_DHT_DCs_length+=3D272;=0A=09oj2j->oj2j_DHT_DCs_length=20*=3D=20=
oj2j->oj2j_component_count;=0A=09=0A=09oj2j->oj2j_DHT_DCs=20=3D=20=
_TIFFmalloc(oj2j->oj2j_DHT_DCs_length);=0A=09if(oj2j->oj2j_DHT_DCs=20=3D=3D=
=20NULL){=0A=09=09return(1);=0A=09}=0A=09_TIFFmemset(oj2j->oj2j_DHT_DCs,=20=
0x00,=20oj2j->oj2j_DHT_DCs_length);=0A=09=0A=09return(0);=0A=0A}=0A=0A=0A=
int=20oj2j_reconstitute_DHT_DCs(OJ2J*=20oj2j){=0A=0A=09int=20i=3D0;=0A=09=
int=20dest=3D0;=0A=09uint32=20ms_offset=3D0;=0A=09uint32=20code_count=3D0;=
=0A=09uint32=20table_offset=3D0;=0A=09=0A=09=
for(dest=3D0;dest<oj2j->oj2j_table_count;dest++){=0A=09=09((unsigned=20=
char*)(oj2j->oj2j_DHT_DCs))[0+ms_offset]=3D0xff;=0A=09=09((unsigned=20=
char*)(oj2j->oj2j_DHT_DCs))[1+ms_offset]=3D0xc4;=0A=09=09((unsigned=20=
char*)(oj2j->oj2j_DHT_DCs))[4+ms_offset]=3Ddest=20&=200x0f;=0A=09=09=
code_count=3D0;=0A=09=09for(i=3D0;i<16;i++){=0A=09=09=09((unsigned=20=
char*)(oj2j->oj2j_DHT_DCs))[i+5+ms_offset]=3D=20=0A=09=09=09=09=
((unsigned=20char*)(oj2j->oj2j_jpegdctables))[i+table_offset];=0A=09=09=09=
code_count+=3D((unsigned=20char*)(oj2j->oj2j_DHT_DCs))[i+5+ms_offset];=0A=
=09=09}=0A=09=09table_offset+=3D16;=0A=09=09((unsigned=20=
char*)(oj2j->oj2j_DHT_DCs))[2+ms_offset]=3D((code_count+19)=20>>=208)=20=
&=200xff;=0A=09=09((unsigned=20=
char*)(oj2j->oj2j_DHT_DCs))[3+ms_offset]=3D(code_count+19)=20&=200xff;=0A=
=09=09for(i=3D0;i<code_count;i++){=0A=09=09=09((unsigned=20=
char*)(oj2j->oj2j_DHT_DCs))[i+21+ms_offset]=3D=0A=09=09=09=09((unsigned=20=
char*)(oj2j->oj2j_jpegdctables))[i+table_offset];=0A=09=09}=0A=09=09=
table_offset+=3Dcode_count;=0A=09=09ms_offset+=3D21;=0A=09=09=
ms_offset+=3Dcode_count;=0A=09}=0A=09=
oj2j->oj2j_DHT_DCs_length=3Dms_offset;=0A=09=0A=09return(0);=0A}=0A=0A=0A=
int=20oj2j_allocate_DRI(OJ2J*=20oj2j){=0A=0A=09oj2j->oj2j_DRI_length=3D6;=0A=
=09=0A=09oj2j->oj2j_DRI=3D_TIFFmalloc(oj2j->oj2j_DRI_length);=0A=09=
if(oj2j->oj2j_DRI=20=3D=3D=20NULL){=0A=09=09return(1);=0A=09}=0A=0A=09=
return(0);=0A}=0A=0Aint=20oj2j_reconstitute_DRI(OJ2J*=20oj2j){=0A=0A=09=
((unsigned=20char*)(oj2j->oj2j_DRI))[0]=3D0xff;=0A=09((unsigned=20=
char*)(oj2j->oj2j_DRI))[1]=3D0xdd;=0A=09((unsigned=20=
char*)(oj2j->oj2j_DRI))[2]=3D0x00;=0A=09((unsigned=20=
char*)(oj2j->oj2j_DRI))[3]=3D0x06;=0A=09((unsigned=20=
char*)(oj2j->oj2j_DRI))[4]=3D((oj2j->oj2j_jpegrestartinterval)=20>>=208)=20=
&=200xff;=0A=09((unsigned=20=
char*)(oj2j->oj2j_DRI))[5]=3D(oj2j->oj2j_jpegrestartinterval)=20&=20=
0xff;=0A=0A=09return(0);=0A}=0A=0Aint=20oj2j_make_jpegtables(OJ2J*=20=
oj2j){=0A=0A=09oj2j->oj2j_jpegtables_length=3D0;=0A=09=
oj2j->oj2j_jpegtables_length+=3D4;=20/*=20EOI,=20SOI=20*/=0A=09=
oj2j->oj2j_jpegtables_length+=3Doj2j->oj2j_DQTs_length;=0A=09=
oj2j->oj2j_jpegtables_length+=3Doj2j->oj2j_DHT_DCs_length;=0A=09=
oj2j->oj2j_jpegtables_length+=3Doj2j->oj2j_DHT_ACs_length;=0A=09=
oj2j->oj2j_jpegtables_length+=3Doj2j->oj2j_DRI_length;=0A=0A=09=
oj2j->oj2j_jpegtables=3D=20_TIFFmalloc(oj2j->oj2j_jpegtables_length);=0A=09=
if(oj2j->oj2j_jpegtables=20=3D=3D=20NULL){=0A=09=09return(1);=0A=09}=0A=09=
=0A=09((unsigned=20char*)oj2j->oj2j_jpegtables)[0]=3D0xff;=0A=09=
((unsigned=20char*)oj2j->oj2j_jpegtables)[1]=3D0xd8;=0A=09=
oj2j->oj2j_jpegtables_length=3D2;=0A=0A=09if(oj2j->oj2j_DQTs_length=20!=3D=
=200){=0A=09=09_TIFFmemcpy(=20=0A=09=09=09&(((unsigned=20=
char*)oj2j->oj2j_jpegtables)[oj2j->oj2j_jpegtables_length]),=20=0A=09=09=09=
oj2j->oj2j_DQTs,=0A=09=09=09oj2j->oj2j_DQTs_length=0A=09=09);=0A=09=09=
oj2j->oj2j_jpegtables_length+=3Doj2j->oj2j_DQTs_length;=0A=09}=0A=09=
if(oj2j->oj2j_DHT_DCs_length=20!=3D=200){=0A=09=09_TIFFmemcpy(=20=0A=09=09=
=09&(((unsigned=20=
char*)oj2j->oj2j_jpegtables)[oj2j->oj2j_jpegtables_length]),=20=0A=09=09=09=
oj2j->oj2j_DHT_DCs,=0A=09=09=09oj2j->oj2j_DHT_DCs_length=0A=09=09);=0A=09=
=09oj2j->oj2j_jpegtables_length+=3Doj2j->oj2j_DHT_DCs_length;=0A=09}=0A=09=
if(oj2j->oj2j_DHT_ACs_length=20!=3D=200){=0A=09=09_TIFFmemcpy(=20=0A=09=09=
=09&(((unsigned=20=
char*)oj2j->oj2j_jpegtables)[oj2j->oj2j_jpegtables_length]),=20=0A=09=09=09=
oj2j->oj2j_DHT_ACs,=0A=09=09=09oj2j->oj2j_DHT_ACs_length=0A=09=09);=0A=09=
=09oj2j->oj2j_jpegtables_length+=3Doj2j->oj2j_DHT_ACs_length;=0A=09}=0A=09=
if(oj2j->oj2j_DRI_length=20!=3D=200){=0A=09=09_TIFFmemcpy(=20=0A=09=09=09=
&(((unsigned=20=
char*)oj2j->oj2j_jpegtables)[oj2j->oj2j_jpegtables_length]),=20=0A=09=09=09=
oj2j->oj2j_DRI,=0A=09=09=09oj2j->oj2j_DRI_length=0A=09=09);=0A=09=09=
oj2j->oj2j_jpegtables_length+=3Doj2j->oj2j_DRI_length;=0A=09}=0A=0A=09=
((unsigned=20=
char*)oj2j->oj2j_jpegtables)[oj2j->oj2j_jpegtables_length]=3D0xff;=0A=09=
((unsigned=20=
char*)oj2j->oj2j_jpegtables)[oj2j->oj2j_jpegtables_length+1]=3D0xd9;=0A=09=
oj2j->oj2j_jpegtables_length+=3D2;=0A=09=0A=09return(0);=0A}=

--Apple-Mail-658735891-2--

--Apple-Mail-1445449218-1--

