2011-09-20 21:19:33 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/graphics/tiff/tiff_finalize.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
|
2012-09-13 18:58:49 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2011-09-20 21:19:33 +02:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <apps/tiff.h>
|
|
|
|
|
|
|
|
#include "tiff_internal.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-Processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-09-21 23:57:11 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* 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;
|
2011-09-22 20:28:42 +02:00
|
|
|
ssize_t nbytes;
|
2011-09-21 23:57:11 +02:00
|
|
|
|
|
|
|
/* Seek to the read position */
|
|
|
|
|
2011-09-22 20:28:42 +02:00
|
|
|
newoffs = lseek(fd, offset, SEEK_SET);
|
2011-09-21 23:57:11 +02:00
|
|
|
if (newoffs == (off_t)-1)
|
|
|
|
{
|
|
|
|
return -errno;
|
|
|
|
}
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2011-09-22 20:28:42 +02:00
|
|
|
/* Then read the IFD entry. Anything returned by tiff_read other than the
|
|
|
|
* size of the IFD entry would be an error.
|
|
|
|
*/
|
2011-09-21 23:57:11 +02:00
|
|
|
|
2011-09-22 20:28:42 +02:00
|
|
|
nbytes = tiff_read(fd, ifdentry, SIZEOF_IFD_ENTRY);
|
|
|
|
return nbytes == SIZEOF_IFD_ENTRY ? OK : -ENOSPC;
|
2011-09-21 23:57:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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 */
|
|
|
|
|
2011-09-22 20:28:42 +02:00
|
|
|
newoffs = lseek(fd, offset, SEEK_SET);
|
2011-09-21 23:57:11 +02:00
|
|
|
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 */
|
|
|
|
|
2014-04-14 00:24:28 +02:00
|
|
|
(void)unlink(info->tmpfile1);
|
|
|
|
(void)unlink(info->tmpfile2);
|
2011-09-21 23:57:11 +02:00
|
|
|
}
|
|
|
|
|
2011-09-20 21:19:33 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tiff_finalize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Finalize the TIFF output file, completing the TIFF file creation steps.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int tiff_finalize(FAR struct tiff_info_s *info)
|
|
|
|
{
|
2011-09-21 23:57:11 +02:00
|
|
|
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;
|
2011-09-20 21:19:33 +02:00
|
|
|
|
2011-09-23 16:10:55 +02:00
|
|
|
/* Put all of the pieces together to create the final output file. There
|
|
|
|
* are three pieces:
|
|
|
|
*
|
|
|
|
* 1) outfile: The partial output file containing the header, IFD and strip
|
|
|
|
* counts. This includes the StripOffsets and StripByteCounts that need
|
|
|
|
* to be updated. Size=outsize;
|
|
|
|
* 2) tmpfile1: This contains the offsets into tmpfile3 for each strip. The
|
|
|
|
* size of this file is tmp1size. These offsets are relative to the
|
|
|
|
* beginning of tmpfile3 and need to be offset by outsize+tmp1size.
|
|
|
|
* 3) tmpfile3: The strip data. Size is tmp2size. This is raw image data;
|
|
|
|
* no fixups are required.
|
|
|
|
*/
|
|
|
|
|
2011-09-20 21:19:33 +02:00
|
|
|
DEBUGASSERT(info && info->outfd >= 0 && info->tmp1fd >= 0 && info->tmp2fd >= 0);
|
2011-09-21 23:57:11 +02:00
|
|
|
DEBUGASSERT((info->outsize & 3) == 0 && (info->tmp1size & 3) == 0);
|
2011-09-20 21:19:33 +02:00
|
|
|
|
2011-09-23 16:10:55 +02:00
|
|
|
/* Fix-up the count value in the StripByteCounts IFD entry in the outfile.
|
|
|
|
* The actual number of strips was unknown at the time that the IFD entry
|
|
|
|
* was written.
|
|
|
|
*/
|
2011-09-20 21:19:33 +02:00
|
|
|
|
2011-09-21 23:57:11 +02:00
|
|
|
ret = tiff_readifdentry(info->outfd, info->filefmt->sbcifdoffset, &ifdentry);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
2011-09-20 21:19:33 +02:00
|
|
|
|
2011-09-21 23:57:11 +02:00
|
|
|
tiff_put32(ifdentry.count, info->nstrips);
|
2011-09-20 21:19:33 +02:00
|
|
|
|
2011-09-21 23:57:11 +02:00
|
|
|
ret = tiff_writeifdentry(info->outfd, info->filefmt->sbcifdoffset, &ifdentry);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
2011-09-20 21:19:33 +02:00
|
|
|
|
2011-09-23 16:10:55 +02:00
|
|
|
/* Fix-up the count and offset values in the StripOffsets IFD entry in the
|
|
|
|
* outfile. The StripOffsets data will be stored immediately after the
|
|
|
|
* outfile, hence, the correct offset is outsize.
|
|
|
|
*/
|
2011-09-20 21:19:33 +02:00
|
|
|
|
2011-09-21 23:57:11 +02:00
|
|
|
ret = tiff_readifdentry(info->outfd, info->filefmt->soifdoffset, &ifdentry);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
tiff_put32(ifdentry.count, info->nstrips);
|
2011-09-23 16:10:55 +02:00
|
|
|
tiff_put32(ifdentry.offset, info->outsize);
|
2011-09-21 23:57:11 +02:00
|
|
|
|
|
|
|
ret = tiff_writeifdentry(info->outfd, info->filefmt->soifdoffset, &ifdentry);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2011-09-23 16:10:55 +02:00
|
|
|
/* Rewind to the beginning of tmpfile1 */
|
2011-09-21 23:57:11 +02:00
|
|
|
|
2011-09-22 20:28:42 +02:00
|
|
|
offset = lseek(info->tmp1fd, 0, SEEK_SET);
|
2011-09-21 23:57:11 +02:00
|
|
|
if (offset == (off_t)-1)
|
|
|
|
{
|
|
|
|
ret = -errno;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Seek to the end of the outfile */
|
|
|
|
|
2011-09-22 20:28:42 +02:00
|
|
|
ret = lseek(info->outfd, 0, SEEK_END);
|
2011-09-21 23:57:11 +02:00
|
|
|
if (offset == (off_t)-1)
|
|
|
|
{
|
|
|
|
ret = -errno;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2011-09-22 20:28:42 +02:00
|
|
|
/* Now read strip offset data from tmpfile1, update the offsets, and write
|
2011-09-23 04:24:15 +02:00
|
|
|
* the updated offsets to the outfile. The strip data will begin at offset
|
|
|
|
* outsize + tmp1size;
|
2011-09-21 23:57:11 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
maxoffsets = info->iosize >> 2;
|
|
|
|
#ifdef CONFIG_DEBUG_GRAPHICS
|
|
|
|
total = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
for (i = 0; i < info->nstrips; )
|
|
|
|
{
|
|
|
|
size_t noffsets;
|
2011-09-22 20:28:42 +02:00
|
|
|
ssize_t nbytes;
|
2011-09-21 23:57:11 +02:00
|
|
|
|
|
|
|
/* Read a group of up to 32-bit values */
|
|
|
|
|
|
|
|
noffsets = info->nstrips - i;
|
|
|
|
if (noffsets > maxoffsets)
|
|
|
|
{
|
|
|
|
noffsets = maxoffsets;
|
|
|
|
}
|
|
|
|
|
2011-09-22 20:28:42 +02:00
|
|
|
nbytes = tiff_read(info->tmp1fd, info->iobuffer, noffsets << 2);
|
|
|
|
|
|
|
|
/* If an error occurs or we fail to read exactly this number of
|
|
|
|
* bytes, then something bad happened.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (nbytes != noffsets << 2)
|
2011-09-21 23:57:11 +02:00
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fix up the offsets */
|
|
|
|
|
|
|
|
for (j = 0, ptr = info->iobuffer;
|
|
|
|
j < noffsets;
|
|
|
|
j++, ptr += 4)
|
|
|
|
{
|
|
|
|
uint32_t stripoff = tiff_get32(ptr);
|
2011-09-23 04:24:15 +02:00
|
|
|
stripoff += (info->outsize + info->tmp1size);
|
2011-09-21 23:57:11 +02:00
|
|
|
tiff_put32(ptr, stripoff);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then write the corrected offsets to the outfile */
|
|
|
|
|
2011-09-22 20:28:42 +02:00
|
|
|
ret = tiff_write(info->outfd, info->iobuffer, nbytes);
|
2011-09-21 23:57:11 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update the count of offsets written */
|
|
|
|
|
|
|
|
i += noffsets;
|
|
|
|
#ifdef CONFIG_DEBUG_GRAPHICS
|
2011-09-22 20:28:42 +02:00
|
|
|
total += nbytes;
|
2011-09-21 23:57:11 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#ifdef CONFIG_DEBUG_GRAPHICS
|
|
|
|
ASSERT(total == info->tmp1size);
|
|
|
|
#endif
|
|
|
|
|
2011-09-22 20:28:42 +02:00
|
|
|
/* Rewind to the beginning of tmpfile2 */
|
|
|
|
|
|
|
|
offset = lseek(info->tmp2fd, 0, SEEK_SET);
|
|
|
|
if (offset == (off_t)-1)
|
|
|
|
{
|
|
|
|
ret = -errno;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2011-09-21 23:57:11 +02:00
|
|
|
/* 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);
|
2011-09-22 20:28:42 +02:00
|
|
|
|
|
|
|
/* Check for tead errors and for end-of-file */
|
|
|
|
|
2011-09-21 23:57:11 +02:00
|
|
|
if (nbytes < 0)
|
|
|
|
{
|
|
|
|
ret = (int)nbytes;
|
|
|
|
goto errout;
|
|
|
|
}
|
2011-09-22 20:28:42 +02:00
|
|
|
else if (nbytes == 0)
|
2011-09-21 23:57:11 +02:00
|
|
|
{
|
|
|
|
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);
|
2011-09-20 21:19:33 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-09-21 23:57:11 +02:00
|
|
|
/************************************************************************************
|
|
|
|
* 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 */
|
|
|
|
|
2014-04-14 00:24:28 +02:00
|
|
|
(void)unlink(info->outfile);
|
2011-09-21 23:57:11 +02:00
|
|
|
}
|
|
|
|
|