Code complete on TIFF creation logic

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@3968 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
patacongo 2011-09-21 21:57:11 +00:00
parent 28cd17bbea
commit bd453b9c81
6 changed files with 816 additions and 161 deletions

View File

@ -67,6 +67,87 @@
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: tiff_addstrip
*
* Description:
* Convert an RGB565 strip to an RGB888 strip and write it to tmpfile2.
*
* Add an image data strip. The size of the strip in pixels must be equal
* to the RowsPerStrip x ImageWidth values that were provided to
* tiff_initialize().
*
* Input Parameters:
* info - A pointer to the caller allocated parameter passing/TIFF state instance.
* buffer - A buffer containing a single row of data.
*
* Returned Value:
* Zero (OK) on success. A negated errno value on failure.
*
****************************************************************************/
int tiff_convstrip(FAR struct tiff_info_s *info, FAR const uint8_t *strip)
{
#ifdef CONFIG_DEBUG_GRAPHICS
size_t ntotal;
#endif
size_t nbytes;
FAR uint16_t *src;
FAR uint8_t *dest;
uint16_t rgb565;
int ret;
int i;
DEBUGASSERT(info->iobuffer != NULL);
/* Convert each RGB565 pixel to RGB888 */
src = (FAR uint16_t *)strip;
dest = info->iobuffer;
nbytes = 0;
#ifdef CONFIG_DEBUG_GRAPHICS
ntotal = 0;
#endif
for (i = 0; i < info->pps; i++)
{
/* Convert RGB565 to RGB888 */
rgb565 = *src++;
*dest++ = (rgb565 >> 11);
*dest++ = (rgb565 >> 5) & 0x3f;
*dest++ = rgb565 & 0x1f;
/* Update the byte count */
nbytes += 3;
#ifdef CONFIG_DEBUG_GRAPHICS
ntotal += 3;
#endif
/* Flush the conversion buffer to tmpfile2 when it becomes full */
if (nbytes > (info->iosize-3))
{
ret = tiff_write(info->tmp2fd, info->iobuffer, nbytes);
if (ret < 0)
{
return ret;
}
/* Reset to refill the conversion buffer */
dest = info->iobuffer;
nbytes = 0;
}
}
/* Flush any buffer data to tmpfile2 */
ret = tiff_write(info->tmp2fd, info->iobuffer, nbytes);
DEBUGASSERT(ntotal == info->bps);
return ret;
}
/****************************************************************************
* Public Functions
****************************************************************************/
@ -88,9 +169,69 @@
*
****************************************************************************/
int tiff_addstrip(FAR struct tiff_info_s *info, FAR uint8_t *strip)
int tiff_addstrip(FAR struct tiff_info_s *info, FAR const uint8_t *strip)
{
#warning "Missing logic"
return -ENOSYS;
ssize_t newsize;
int ret;
/* Add the new strip based on the color format. For FB_FMT_RGB16_565,
* will have to perform a conversion to RGB888.
*/
if (info->colorfmt == FB_FMT_RGB16_565)
{
ret = tiff_convstrip(info, strip);
}
/* For other formats, it is a simple write using the number of bytes per strip */
else
{
ret = tiff_write(info->tmp2fd, strip, info->bps);
}
if (ret < 0)
{
goto errout;
}
/* Write the byte count to the outfile and the offset to tmpfile1 */
ret = tiff_putint32(info->outfd, info->bps);
if (ret < 0)
{
goto errout;
}
info->outsize += 4;
ret = tiff_putint32(info->tmp1fd, info->tmp2size);
if (ret < 0)
{
goto errout;
}
info->tmp1size += 4;
/* Increment the size of tmp2file. */
info->tmp2size += info->bps;
/* Pad tmpfile2 as necessary achieve word alignment */
newsize = tiff_wordalign(info->tmp2fd, info->tmp2size);
if (newsize < 0)
{
ret = (int)newsize;
goto errout;
}
info->tmp2size = (size_t)newsize;
/* Increment the number of strips in the TIFF file */
info->nstrips++;
return OK;
errout:
tiff_abort(info);
return ret;
}

View File

@ -68,6 +68,117 @@
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: tiff_readifdentry
*
* Description:
* Read the IFD entry at the specified offset.
*
* Input Parameters:
* fd - File descriptor to rad from
* offset - Offset to read from
* ifdentry - Location to read the data
*
* Returned Value:
* Zero (OK) on success. A negated errno value on failure.
*
****************************************************************************/
static int tiff_readifdentry(int fd, off_t offset,
FAR struct tiff_ifdentry_s *ifdentry)
{
off_t newoffs;
/* Seek to the read position */
newoffs = lseek(fd, SEEK_SET, offset);
if (newoffs == (off_t)-1)
{
return -errno;
}
/* Then read the IFD entry */
return tiff_read(fd, ifdentry, SIZEOF_IFD_ENTRY);
}
/****************************************************************************
* Name: tiff_writeifdentry
*
* Description:
* Write the IFD entry at the specified offset.
*
* Input Parameters:
* fd - File descriptor to rad from
* offset - Offset to read from
* ifdentry - Location to read the data
*
* Returned Value:
* Zero (OK) on success. A negated errno value on failure.
*
****************************************************************************/
static int tiff_writeifdentry(int fd, off_t offset,
FAR struct tiff_ifdentry_s *ifdentry)
{
off_t newoffs;
/* Seek to the write position */
newoffs = lseek(fd, SEEK_SET, offset);
if (newoffs == (off_t)-1)
{
return -errno;
}
/* Then write the IFD entry */
return tiff_write(fd, ifdentry, SIZEOF_IFD_ENTRY);
}
/****************************************************************************
* Name: tiff_cleanup
*
* Description:
* Normal clean-up after completion of the TIFF file creation
*
* Input Parameters:
* info - A pointer to the caller allocated parameter passing/TIFF
* state instance.
*
* Returned Value:
* None
*
****************************************************************************/
static void tiff_cleanup(FAR struct tiff_info_s *info)
{
/* Close all opened files */
if (info->outfd >= 0)
{
(void)close(info->outfd);
}
info->outfd = -1;
if (info->tmp1fd >= 0)
{
(void)close(info->tmp1fd);
}
info->tmp1fd = -1;
if (info->tmp2fd >= 0)
{
(void)close(info->tmp2fd);
}
info->tmp2fd = -1;
/* And remove the temporary files */
(void)unlink(info->tmpfile1);
(void)unlink(info->tmpfile2);
}
/****************************************************************************
* Public Functions
****************************************************************************/
@ -89,30 +200,199 @@
int tiff_finalize(FAR struct tiff_info_s *info)
{
int ret = -EINVAL;
struct tiff_ifdentry_s ifdentry;
FAR uint8_t *ptr;
size_t maxoffsets;
#ifdef CONFIG_DEBUG_GRAPHICS
size_t total;
#endif
off_t offset;
int ret;
int i;
int j;
DEBUGASSERT(info && info->outfd >= 0 && info->tmp1fd >= 0 && info->tmp2fd >= 0);
DEBUGASSERT((info->outsize & 3) == 0 && (info->tmp1size & 3) == 0);
/* Fix-up the StripOffsets and StripByteCounts IFD entries in the outfile */
#warning "Missing logic"
/* Fix-up the count value in the StripByteCounts IFD entry in the outfile */
/* Append the StripOffsets and StripByteCounts entries to the outfile */
#warning "Missing logic"
ret = tiff_readifdentry(info->outfd, info->filefmt->sbcifdoffset, &ifdentry);
if (ret < 0)
{
goto errout;
}
/* Append the strip image data to the outfile */
#warning "Missing logic"
tiff_put32(ifdentry.count, info->nstrips);
/* Close all files */
ret = tiff_writeifdentry(info->outfd, info->filefmt->sbcifdoffset, &ifdentry);
if (ret < 0)
{
goto errout;
}
(void)close(info->outfd);
(void)close(info->tmp1fd);
(void)close(info->tmp2fd);
info->outfd = -1;
info->tmp1fd = -1;
info->tmp2fd = -1;
/* Fix-up the count and offset values in the StripOffsets IFD entry in the outfile */
/* And remove the temporary files */
#warning "Missing logic"
ret = tiff_readifdentry(info->outfd, info->filefmt->soifdoffset, &ifdentry);
if (ret < 0)
{
goto errout;
}
tiff_put32(ifdentry.count, info->nstrips);
tiff_put32(ifdentry.offset, info->outsize + info->tmp1size);
ret = tiff_writeifdentry(info->outfd, info->filefmt->soifdoffset, &ifdentry);
if (ret < 0)
{
goto errout;
}
/* Seek to the beginning of tmpfile2 */
offset = lseek(info->tmp2fd, SEEK_SET, 0);
if (offset == (off_t)-1)
{
ret = -errno;
goto errout;
}
/* Seek to the end of the outfile */
ret = lseek(info->outfd, SEEK_END, 0);
if (offset == (off_t)-1)
{
ret = -errno;
goto errout;
}
/* Now read strip offset data from tmpfile2, update the offsets, and write
* the updated offsets to the outfile.
*/
maxoffsets = info->iosize >> 2;
#ifdef CONFIG_DEBUG_GRAPHICS
total = 0;
#endif
for (i = 0; i < info->nstrips; )
{
size_t noffsets;
/* Read a group of up to 32-bit values */
noffsets = info->nstrips - i;
if (noffsets > maxoffsets)
{
noffsets = maxoffsets;
}
ret = tiff_read(info->tmp1fd, info->iobuffer, noffsets << 2);
if (ret <= 0)
{
goto errout;
}
/* Fix up the offsets */
for (j = 0, ptr = info->iobuffer;
j < noffsets;
j++, ptr += 4)
{
uint32_t stripoff = tiff_get32(ptr);
stripoff += info->outsize;
tiff_put32(ptr, stripoff);
}
/* Then write the corrected offsets to the outfile */
ret = tiff_write(info->outfd, info->iobuffer, noffsets << 2);
if (ret < 0)
{
goto errout;
}
/* Update the count of offsets written */
i += noffsets;
#ifdef CONFIG_DEBUG_GRAPHICS
total += noffsets << 2;
#endif
}
#ifdef CONFIG_DEBUG_GRAPHICS
ASSERT(total == info->tmp1size);
#endif
/* Finally, copy the tmpfile2 to the end of the outfile */
#ifdef CONFIG_DEBUG_GRAPHICS
total = 0;
#endif
for (;;)
{
ssize_t nbytes;
/* Read a block of data from tmpfile2 */
nbytes = tiff_read(info->tmp2fd, info->iobuffer, info->iosize);
if (nbytes < 0)
{
ret = (int)nbytes;
goto errout;
}
else if (ret == 0)
{
break;
}
/* Then copy the data to the outfile */
ret = tiff_write(info->outfd, info->iobuffer, nbytes);
if (ret < 0)
{
goto errout;
}
#ifdef CONFIG_DEBUG_GRAPHICS
total += nbytes;
#endif
}
#ifdef CONFIG_DEBUG_GRAPHICS
ASSERT(total == info->tmp2size);
#endif
/* Close all files and return success */
tiff_cleanup(info);
return OK;
errout:
tiff_abort(info);
return ret;
}
/************************************************************************************
* Name: tiff_abort
*
* Description:
* Abort the TIFF file creation and create-up resources.
*
* Input Parameters:
* info - A pointer to the caller allocated parameter passing/TIFF state instance.
*
* Returned Value:
* None
*
************************************************************************************/
void tiff_abort(FAR struct tiff_info_s *info)
{
/* Perform normal cleanup */
tiff_cleanup(info);
/* But then delete the output file as well */
(void)unlink(info->outfile);
}

View File

@ -271,84 +271,6 @@ static const struct tiff_filefmt_s g_rgbinfo =
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: tiff_putint16
*
* Description:
* Write two bytes to the outfile.
*
* Input Parameters:
* info - A pointer to the caller allocated parameter passing/TIFF state
* instance.
* value - The 2-byte, uint16_t value to write
*
* Returned Value:
* Zero (OK) on success. A negated errno value on failure.
*
****************************************************************************/
static int tiff_putint16(FAR struct tiff_info_s *info, uint16_t value)
{
uint8_t bytes[2];
/* Write the two bytes to the output file */
tiff_put16(bytes, value);
return tiff_write(info->outfd, bytes, 2);
}
/****************************************************************************
* Name: tiff_putint32
*
* Description:
* Write four bytes to the outfile.
*
* Input Parameters:
* info - A pointer to the caller allocated parameter passing/TIFF state
* instance.
* value - The 4-byte, uint32_t value to write
*
* Returned Value:
* Zero (OK) on success. A negated errno value on failure.
*
****************************************************************************/
static int tiff_putint32(FAR struct tiff_info_s *info, uint32_t value)
{
uint8_t bytes[4];
/* Write the four bytes to the output file */
tiff_put32(bytes, value);
return tiff_write(info->outfd, bytes, 4);
}
/****************************************************************************
* Name: tiff_putstring
*
* Description:
* Write a string of fixed length to the outfile.
*
* Input Parameters:
* info - A pointer to the caller allocated parameter passing/TIFF state
* instance.
*
* Returned Value:
* Zero (OK) on success. A negated errno value on failure.
*
****************************************************************************/
static int tiff_putstring(FAR struct tiff_info_s *info, FAR const char *string,
int len)
{
#ifdef CONFIG_DEBUG_GRAPHICS
int actual = strlen(string);
ASSERT(len = actual+1);
#endif
return tiff_write(info->outfd, string, len);
}
/****************************************************************************
* Name: tiff_putheader
*
@ -397,7 +319,7 @@ static inline int tiff_putheader(FAR struct tiff_info_s *info)
/* Two pad bytes following the header */
ret = tiff_putint16(info, 0);
ret = tiff_putint16(info->outfd, 0);
return ret;
}
@ -534,56 +456,62 @@ int tiff_initialize(FAR struct tiff_info_s *info)
/* Open all output files */
info->outfd = open(info->outfile, O_WRONLY|O_CREAT|O_TRUNC, 0666);
info->outfd = open(info->outfile, O_RDWR|O_CREAT|O_TRUNC, 0666);
if (info->outfd < 0)
{
gdbg("Failed to open %s for writing: %d\n", info->outfile, errno);
gdbg("Failed to open %s for reading/writing: %d\n", info->outfile, errno);
goto errout;
}
info->tmp1fd = open(info->tmpfile1, O_WRONLY|O_CREAT|O_TRUNC, 0666);
info->tmp1fd = open(info->tmpfile1, O_RDWR|O_CREAT|O_TRUNC, 0666);
if (info->tmp1fd < 0)
{
gdbg("Failed to open %s for writing: %d\n", info->tmpfile1, errno);
goto errout_with_outfd;
gdbg("Failed to open %s for reading/writing: %d\n", info->tmpfile1, errno);
goto errout;
}
info->tmp2fd = open(info->tmpfile1, O_WRONLY|O_CREAT|O_TRUNC, 0666);
info->tmp2fd = open(info->tmpfile1, O_RDWR|O_CREAT|O_TRUNC, 0666);
if (info->tmp2fd < 0)
{
gdbg("Failed to open %s for writing: %d\n", info->tmpfile1, errno);
goto errout_with_tmp1fd;
gdbg("Failed to open %s for reading/writing: %d\n", info->tmpfile1, errno);
goto errout;
}
/* Make some decisions using the color format. Only the following are
* supported:
*/
info->pps = info->imgwidth * info->rps; /* Pixels per strip */
switch (info->colorfmt)
{
case FB_FMT_Y1: /* BPP=1, monochrome, 0=black */
info->filefmt = &g_bilevinfo; /* Bi-level file image file info */
info->imgflags = IMGFLAGS_FMT_Y1; /* Bit encoded image characteristics */
info->bps = (info->pps + 7) >> 3; /* Bytes per strip */
break;
case FB_FMT_Y4: /* BPP=4, 4-bit greyscale, 0=black */
info->filefmt = &g_greyinfo; /* Greyscale file image file info */
info->imgflags = IMGFLAGS_FMT_Y4; /* Bit encoded image characteristics */
info->bps = (info->pps + 1) >> 1; /* Bytes per strip */
break;
case FB_FMT_Y8: /* BPP=8, 8-bit greyscale, 0=black */
info->filefmt = &g_greyinfo; /* Greyscale file image file info */
info->imgflags = IMGFLAGS_FMT_Y8; /* Bit encoded image characteristics */
info->bps = info->pps; /* Bytes per strip */
break;
case FB_FMT_RGB16_565: /* BPP=16 R=6, G=6, B=5 */
info->filefmt = &g_rgbinfo; /* RGB file image file info */
info->imgflags = IMGFLAGS_FMT_RGB16_565; /* Bit encoded image characteristics */
info->bps = 3 * info->pps; /* Bytes per strip */
break;
case FB_FMT_RGB24: /* BPP=24 R=8, G=8, B=8 */
info->filefmt = &g_rgbinfo; /* RGB file image file info */
info->imgflags = IMGFLAGS_FMT_RGB24; /* Bit encoded image characteristics */
info->bps = 3 *info->pps; /* Bytes per strip */
break;
default:
@ -602,7 +530,7 @@ int tiff_initialize(FAR struct tiff_info_s *info)
ret = tiff_putheader(info);
if (ret < 0)
{
goto errout_with_tmp2fd;
goto errout;
}
tiff_offset(offset, TIFF_IFD_OFFSET);
@ -611,10 +539,10 @@ int tiff_initialize(FAR struct tiff_info_s *info)
* All formats: Offset 10 Number of Directory Entries 12
*/
ret = tiff_putint16(info, info->filefmt->nifdentries);
ret = tiff_putint16(info->outfd, info->filefmt->nifdentries);
if (ret < 0)
{
goto errout_with_tmp2fd;
goto errout;
}
tiff_offset(offset, 2);
@ -626,7 +554,7 @@ int tiff_initialize(FAR struct tiff_info_s *info)
ret = tiff_putifdentry16(info, IFD_TAG_NEWSUBFILETYPE, IFD_FIELD_LONG, 1, 0);
if (ret < 0)
{
goto errout_with_tmp2fd;
goto errout;
}
tiff_offset(offset, SIZEOF_IFD_ENTRY);
@ -644,7 +572,7 @@ int tiff_initialize(FAR struct tiff_info_s *info)
if (ret < 0)
{
goto errout_with_tmp2fd;
goto errout;
}
tiff_offset(offset, 2*SIZEOF_IFD_ENTRY);
@ -670,7 +598,7 @@ int tiff_initialize(FAR struct tiff_info_s *info)
ret = tiff_putifdentry16(info, IFD_TAG_BITSPERSAMPLE, IFD_FIELD_SHORT, 1, val16);
if (ret < 0)
{
goto errout_with_tmp2fd;
goto errout;
}
tiff_offset(offset, SIZEOF_IFD_ENTRY);
}
@ -679,7 +607,7 @@ int tiff_initialize(FAR struct tiff_info_s *info)
ret = tiff_putifdentry(info, IFD_TAG_BITSPERSAMPLE, IFD_FIELD_SHORT, 3, TIFF_RGB_BPSOFFSET);
if (ret < 0)
{
goto errout_with_tmp2fd;
goto errout;
}
tiff_offset(offset, SIZEOF_IFD_ENTRY);
}
@ -694,7 +622,7 @@ int tiff_initialize(FAR struct tiff_info_s *info)
ret = tiff_putifdentry16(info, IFD_TAG_COMPRESSION, IFD_FIELD_SHORT, 1, TAG_COMP_NONE);
if (ret < 0)
{
goto errout_with_tmp2fd;
goto errout;
}
tiff_offset(offset, SIZEOF_IFD_ENTRY);
@ -717,7 +645,7 @@ int tiff_initialize(FAR struct tiff_info_s *info)
ret = tiff_putifdentry16(info, IFD_TAG_PMI, IFD_FIELD_SHORT, 1, val16);
if (ret < 0)
{
goto errout_with_tmp2fd;
goto errout;
}
tiff_offset(offset, SIZEOF_IFD_ENTRY);
@ -732,7 +660,7 @@ int tiff_initialize(FAR struct tiff_info_s *info)
ret = tiff_putifdentry(info, IFD_TAG_STRIPOFFSETS, IFD_FIELD_LONG, 0, 0);
if (ret < 0)
{
goto errout_with_tmp2fd;
goto errout;
}
tiff_offset(offset, SIZEOF_IFD_ENTRY);
@ -748,7 +676,7 @@ int tiff_initialize(FAR struct tiff_info_s *info)
ret = tiff_putifdentry16(info, IFD_TAG_SAMPLESPERPIXEL, IFD_FIELD_SHORT, 1, 3);
if (ret < 0)
{
goto errout_with_tmp2fd;
goto errout;
}
tiff_offset(offset, SIZEOF_IFD_ENTRY);
}
@ -763,7 +691,7 @@ int tiff_initialize(FAR struct tiff_info_s *info)
ret = tiff_putifdentry16(info, IFD_TAG_ROWSPERSTRIP, IFD_FIELD_SHORT, 1, info->rps);
if (ret < 0)
{
goto errout_with_tmp2fd;
goto errout;
}
tiff_offset(offset, SIZEOF_IFD_ENTRY);
@ -778,7 +706,7 @@ int tiff_initialize(FAR struct tiff_info_s *info)
ret = tiff_putifdentry(info, IFD_TAG_STRIPCOUNTS, IFD_FIELD_LONG, 0, info->filefmt->sbcoffset);
if (ret < 0)
{
goto errout_with_tmp2fd;
goto errout;
}
tiff_offset(offset, SIZEOF_IFD_ENTRY);
@ -797,7 +725,7 @@ int tiff_initialize(FAR struct tiff_info_s *info)
if (ret < 0)
{
goto errout_with_tmp2fd;
goto errout;
}
tiff_offset(offset, 2*SIZEOF_IFD_ENTRY);
@ -811,7 +739,7 @@ int tiff_initialize(FAR struct tiff_info_s *info)
ret = tiff_putifdentry16(info, IFD_TAG_RESUNIT, IFD_FIELD_SHORT, 1, TAG_RESUNIT_INCH);
if (ret < 0)
{
goto errout_with_tmp2fd;
goto errout;
}
tiff_offset(offset, SIZEOF_IFD_ENTRY);
@ -826,7 +754,7 @@ int tiff_initialize(FAR struct tiff_info_s *info)
ret = tiff_putifdentry(info, IFD_TAG_SOFTWARE, IFD_FIELD_ASCII, TIFF_SOFTWARE_STRLEN, info->filefmt->swoffset);
if (ret < 0)
{
goto errout_with_tmp2fd;
goto errout;
}
tiff_offset(offset, SIZEOF_IFD_ENTRY);
@ -841,7 +769,7 @@ int tiff_initialize(FAR struct tiff_info_s *info)
ret = tiff_putifdentry(info, IFD_TAG_DATETIME, IFD_FIELD_ASCII, TIFF_DATETIME_STRLEN, info->filefmt->dateoffset);
if (ret < 0)
{
goto errout_with_tmp2fd;
goto errout;
}
tiff_offset(offset, SIZEOF_IFD_ENTRY);
@ -855,10 +783,10 @@ int tiff_initialize(FAR struct tiff_info_s *info)
* Offset 194, [2 bytes padding]
*/
ret = tiff_putint32(info, 0);
ret = tiff_putint32(info->outfd, 0);
if (ret < 0)
{
goto errout_with_tmp2fd;
goto errout;
}
tiff_offset(offset, 4);
@ -877,28 +805,28 @@ int tiff_initialize(FAR struct tiff_info_s *info)
*/
tiff_checkoffs(offset, info->filefmt->xresoffset);
ret = tiff_putint32(info, 300);
ret = tiff_putint32(info->outfd, 300);
if (ret == OK)
{
ret = tiff_putint32(info, 1);
ret = tiff_putint32(info->outfd, 1);
}
if (ret < 0)
{
goto errout_with_tmp2fd;
goto errout;
}
tiff_offset(offset, 8);
tiff_checkoffs(offset, info->filefmt->yresoffset);
ret = tiff_putint32(info, 300);
ret = tiff_putint32(info->outfd, 300);
if (ret == OK)
{
ret = tiff_putint32(info, 1);
ret = tiff_putint32(info->outfd, 1);
}
if (ret < 0)
{
goto errout_with_tmp2fd;
goto errout;
}
tiff_offset(offset, 8);
@ -911,10 +839,10 @@ int tiff_initialize(FAR struct tiff_info_s *info)
*/
tiff_checkoffs(offset, info->filefmt->xresoffset);
ret = tiff_putstring(info, TIFF_SOFTWARE_STRING, TIFF_SOFTWARE_STRLEN);
ret = tiff_putstring(info->outfd, TIFF_SOFTWARE_STRING, TIFF_SOFTWARE_STRLEN);
if (ret < 0)
{
goto errout_with_tmp2fd;
goto errout;
}
tiff_offset(offset, TIFF_SOFTWARE_STRLEN);
@ -930,40 +858,33 @@ int tiff_initialize(FAR struct tiff_info_s *info)
ret = tiff_datetime(timbuf, TIFF_DATETIME_STRLEN + 8);
if (ret < 0)
{
goto errout_with_tmp2fd;
goto errout;
}
ret = tiff_putstring(info, timbuf, TIFF_DATETIME_STRLEN);
ret = tiff_putstring(info->outfd, timbuf, TIFF_DATETIME_STRLEN);
if (ret < 0)
{
goto errout_with_tmp2fd;
goto errout;
}
tiff_offset(offset, TIFF_DATETIME_STRLEN);
/* Add two bytes of padding */
ret = tiff_putint16(info, 0);
ret = tiff_putint16(info->outfd, 0);
if (ret < 0)
{
goto errout_with_tmp2fd;
goto errout;
}
tiff_offset(offset, 2);
/* And that should do it! */
tiff_checkoffs(offset, info->filefmt->sbcoffset);
info->outsize = info->filefmt->sbcoffset;
return OK;
errout_with_tmp2fd:
(void)close(info->tmp2fd);
info->tmp2fd = -1;
errout_with_tmp1fd:
(void)close(info->tmp1fd);
info->tmp1fd = -1;
errout_with_outfd:
(void)close(info->outfd);
info->outfd = -1;
errout:
tiff_abort(info);
return ret;
}

View File

@ -51,7 +51,7 @@
/****************************************************************************
* Pre-Processor Definitions
****************************************************************************/
/* Image Type **************************************************************/
/* Image Type ***************************************************************/
#define IMGFLAGS_BILEV_BIT (1 << 0)
#define IMGFLAGS_GREY_BIT (1 << 1)
@ -96,6 +96,24 @@ extern "C" {
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: tiff_read
*
* Description:
* Read TIFF data from the specified file
*
* Input Parameters:
* fd - Open file descriptor to read from
* buffer - Read-only buffer containing the data to be written
* count - The number of bytes to write
*
* Returned Value:
* Zero (OK) on success. A negated errno value on failure.
*
****************************************************************************/
EXTERN int tiff_read(int fd, FAR void *buffer, size_t count);
/****************************************************************************
* Name: tiff_write
*
@ -114,6 +132,75 @@ extern "C" {
EXTERN int tiff_write(int fd, FAR const void *buffer, size_t count);
/****************************************************************************
* Name: tiff_putint16
*
* Description:
* Write two bytes to the outfile.
*
* Input Parameters:
* fd - File descriptor to be used.
* value - The 2-byte, uint16_t value to write
*
* Returned Value:
* Zero (OK) on success. A negated errno value on failure.
*
****************************************************************************/
EXTERN int tiff_putint16(int fd, uint16_t value);
/****************************************************************************
* Name: tiff_putint32
*
* Description:
* Write four bytes to the outfile.
*
* Input Parameters:
* fd - File descriptor to be used.
* value - The 4-byte, uint32_t value to write
*
* Returned Value:
* Zero (OK) on success. A negated errno value on failure.
*
****************************************************************************/
EXTERN int tiff_putint32(int fd, uint32_t value);
/****************************************************************************
* Name: tiff_putstring
*
* Description:
* Write a string of fixed length to the outfile.
*
* Input Parameters:
* fd - File descriptor to be used.
* string - A pointer to the memory containing the string
* len - The length of the string (including the NUL terminator)
*
* Returned Value:
* Zero (OK) on success. A negated errno value on failure.
*
****************************************************************************/
EXTERN int tiff_putstring(int fd, FAR const char *string, int len);
/****************************************************************************
* Name: tiff_wordalign
*
* Description:
* Pad a file with zeros as necessary to achieve word alignament.
*
* Input Parameters:
* fd - File descriptor to be used.
* size - The current size of the file
*
* Returned Value:
* The new size of the file on success. A negated errno value on failure.
*
****************************************************************************/
EXTERN ssize_t tiff_wordalign(int fd, size_t size);
#undef EXTERN
#if defined(__cplusplus)
}

View File

@ -39,6 +39,7 @@
#include <nuttx/config.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <errno.h>
@ -73,17 +74,20 @@
****************************************************************************/
/****************************************************************************
* Name: tiff_put16 and tiff_put32
* Name: tiff_put/get16/32
*
* Description:
* Put 16 and 32 values in the correct byte order at the specified position.
* Put and get 16 and 32 values in the correct byte order at the specified
* position.
*
* Input Parameters:
* dest - The location to store the multi-byte data
* value - The value to be stored
* dest - The location to store the multi-byte data (put only)
* src - The location to get the multi-byte data (get only)
* value - The value to be stored (put only)
*
* Returned Value:
* None
* None (put)
* The extracted value (get)
*
****************************************************************************/
@ -109,6 +113,86 @@ void tiff_put32(FAR uint8_t *dest, uint32_t value)
#endif
}
uint16_t tiff_get16(FAR uint8_t *src)
{
#ifdef CONFIG_ENDIAN_BIG
return (uint16_t)src[0] << 8 | (uint16_t)src[1];
#else
return (uint16_t)src[1] << 8 | (uint16_t)src[0];
#endif
}
uint32_t tiff_get32(FAR uint8_t *src)
{
#ifdef CONFIG_ENDIAN_BIG
return (uint32_t)tiff_get16(src) << 16 | (uint32_t)tiff_get16(src+2);
#else
return (uint32_t)tiff_get16(src+2) << 16 | (uint32_t)tiff_get16(src);
#endif
}
/****************************************************************************
* Name: tiff_read
*
* Description:
* Read TIFF data from the specified file
*
* Input Parameters:
* fd - Open file descriptor to read from
* buffer - Read-only buffer containing the data to be written
* count - The number of bytes to write
*
* Returned Value:
* Zero (OK) on success. A negated errno value on failure.
*
****************************************************************************/
int tiff_read(int fd, FAR void *buffer, size_t count)
{
ssize_t nbytes;
int errval;
/* This loop retries the write until either: (1) it completes successfully,
* or (2) until an irrecoverble error occurs.
*/
while (count > 0)
{
/* Do the read */
nbytes = read(fd, buffer, count);
/* Check for an error */
if (nbytes < 0)
{
/* EINTR is not an error.. this just means that the write was
* interrupted by a signal.
*/
errval = errno;
if (errval != EINTR)
{
/* Other errors are bad news and we will break out with an error */
return -errval;
}
}
/* What if read returns some number of bytes other than the requested number? */
else
{
DEBUGASSERT(nbytes < count && nbytes != 0);
buffer += nbytes;
count -= nbytes;
}
}
return OK;
}
/****************************************************************************
* Name: tiff_write
*
@ -125,7 +209,7 @@ void tiff_put32(FAR uint8_t *dest, uint32_t value)
*
****************************************************************************/
int tiff_write(int fd, FAR void *buffer, size_t count)
int tiff_write(int fd, FAR const void *buffer, size_t count)
{
ssize_t nbytes;
int errval;
@ -157,15 +241,127 @@ int tiff_write(int fd, FAR void *buffer, size_t count)
}
}
/* What if the the write returns some number of bytes other than the requested number? */
/* What if write returns some number of bytes other than the requested number? */
else
{
DEBUGASSERT(nbytes < count && nbytes != 0);
count -= nbytes;
buffer += nbytes;
count -= nbytes;
}
}
return OK;
}
/****************************************************************************
* Name: tiff_putint16
*
* Description:
* Write two bytes to the outfile.
*
* Input Parameters:
* fd - File descriptor to be used.
* value - The 2-byte, uint16_t value to write
*
* Returned Value:
* Zero (OK) on success. A negated errno value on failure.
*
****************************************************************************/
int tiff_putint16(int fd, uint16_t value)
{
uint8_t bytes[2];
/* Write the two bytes to the output file */
tiff_put16(bytes, value);
return tiff_write(fd, bytes, 2);
}
/****************************************************************************
* Name: tiff_putint32
*
* Description:
* Write four bytes to the outfile.
*
* Input Parameters:
* fd - File descriptor to be used.
* value - The 4-byte, uint32_t value to write
*
* Returned Value:
* Zero (OK) on success. A negated errno value on failure.
*
****************************************************************************/
int tiff_putint32(int fd, uint32_t value)
{
uint8_t bytes[4];
/* Write the four bytes to the output file */
tiff_put32(bytes, value);
return tiff_write(fd, bytes, 4);
}
/****************************************************************************
* Name: tiff_putstring
*
* Description:
* Write a string of fixed length to the outfile.
*
* Input Parameters:
* fd - File descriptor to be used.
* string - A pointer to the memory containing the string
* len - The length of the string (including the NUL terminator)
*
* Returned Value:
* Zero (OK) on success. A negated errno value on failure.
*
****************************************************************************/
int tiff_putstring(int fd, FAR const char *string, int len)
{
#ifdef CONFIG_DEBUG_GRAPHICS
int actual = strlen(string);
ASSERT(len = actual+1);
#endif
return tiff_write(fd, string, len);
}
/****************************************************************************
* Name: tiff_wordalign
*
* Description:
* Pad a file with zeros as necessary to achieve word alignament.
*
* Input Parameters:
* fd - File descriptor to be used.
* size - The current size of the file
*
* Returned Value:
* The new size of the file on success. A negated errno value on failure.
*
****************************************************************************/
ssize_t tiff_wordalign(int fd, size_t size)
{
unsigned int remainder;
int ret;
remainder = size & 3;
if (remainder > 0)
{
unsigned int nbytes = 4 - remainder;
uint32_t value = 0;
ret = tiff_write(fd, &value, nbytes);
if (ret < 0)
{
return (ssize_t)ret;
}
size += nbytes;
}
return size;
}

View File

@ -320,6 +320,14 @@ struct tiff_info_s
nxgl_coord_t imgwidth; /* TIFF ImageWidth, Number of columns in the image */
nxgl_coord_t imgheight; /* TIFF ImageLength, Number of rows in the image */
/* The caller must provide an I/O buffer as well. This I/O buffer will
* used for color conversions and as the intermediate buffer for copying
* files. The larger the buffer, the better the performance.
*/
FAR uint8_t *iobuffer; /* IO buffer allocated by the caller */
unsigned int iosize; /* The size of the I/O buffer in bytes */
/* The second set of fields are used only internally by the TIFF file
* creation logic. These fields must be set to zero initially by the
* caller of tiff_initialize(). User logic should not depend upon any
@ -330,11 +338,14 @@ struct tiff_info_s
uint8_t imgflags; /* Bit-encoded image flags */
nxgl_coord_t nstrips; /* Number of strips in tmpfile3 */
size_t pps; /* Pixels per strip */
size_t bps; /* Bytes per strip */
int outfd; /* outfile file descriptor */
int tmp1fd; /* tmpfile1 file descriptor */
int tmp2fd; /* tmpfile2 file descriptor */
off_t outsize; /* Current size of outfile */
off_t tmp1size; /* Current size of tmpfile1 */
off_t tmp2size; /* Current size of tmpfile2 */
/* Points to an internal constant structure of file offsets */
@ -390,7 +401,7 @@ EXTERN int tiff_initialize(FAR struct tiff_info_s *info);
*
************************************************************************************/
EXTERN int tiff_addstrip(FAR struct tiff_info_s *info, FAR uint8_t *strip);
EXTERN int tiff_addstrip(FAR struct tiff_info_s *info, FAR const uint8_t *strip);
/************************************************************************************
* Name: tiff_finalize
@ -409,22 +420,41 @@ EXTERN int tiff_addstrip(FAR struct tiff_info_s *info, FAR uint8_t *strip);
EXTERN int tiff_finalize(FAR struct tiff_info_s *info);
/************************************************************************************
* Name: tiff_put16 and tiff_put32
* Name: tiff_abort
*
* Description:
* Put 16 and 32 values in the correct byte order at the specified position.
* Abort the TIFF file creation and create-up resources.
*
* Input Parameters:
* dest - The location to store the multi-byte data
* value - The value to be stored
* info - A pointer to the caller allocated parameter passing/TIFF state instance.
*
* Returned Value:
* None
*
************************************************************************************/
EXTERN void tiff_abort(FAR struct tiff_info_s *info);
/************************************************************************************
* Name: tiff_put/get16/32
*
* Description:
* Put and get 16 and 32 values in the correct byte order at the specified position.
*
* Input Parameters:
* dest - The location to store the multi-byte data (put only)
* src - The location to get the multi-byte data (get only)
*
* Returned Value:
* None (put)
* The extracted value (get)
*
************************************************************************************/
EXTERN void tiff_put16(FAR uint8_t *dest, uint16_t value);
EXTERN void tiff_put32(FAR uint8_t *dest, uint32_t value);
EXTERN uint16_t tiff_get16(FAR uint8_t *dest);
EXTERN uint32_t tiff_get32(FAR uint8_t *dest);
#undef EXTERN
#ifdef __cplusplus