Return-Path: <tiff-bounces@lists.maptools.org>
X-Original-To: info@awaresystems.be
Delivered-To: info@awaresystems.be
Received: from mx-l.b-one.net (mx-l.one.com [91.198.169.59])
	by mail9.b-one.net (Postfix) with ESMTP id 326BC400124
	for <info@awaresystems.be>; Mon,  6 Apr 2009 17:39:32 +0200 (CEST)
X-Virus-Scanned: by one.com
X-Spam-Score: 0
X-Spam-Level: 
X-Spam-Status: No, score=0 tagged_above=0 required=6 tests=[none]
X-Greylist: domain auto-whitelisted by SQLgrey-1.7.4
Received: from lists.maptools.org (lists.maptools.org [65.39.174.144])
	by mx-l.b-one.net (Postfix) with ESMTP id 51B8C1CB961A
	for <info@awaresystems.be>; Mon,  6 Apr 2009 17:39:31 +0200 (CEST)
Received: from localhost.localdomain (localhost.localdomain [127.0.0.1])
	by lists.maptools.org (Postfix) with ESMTP id 497021D203684;
	Mon,  6 Apr 2009 10:37:47 -0500 (EST)
Received: from mail-gx0-f169.google.com (mail-gx0-f169.google.com
	[209.85.217.169])
	by lists.maptools.org (Postfix) with ESMTP id B09231D36DB02
	for <tiff@lists.maptools.org>; Mon,  6 Apr 2009 10:35:35 -0500 (EST)
Received: by gxk17 with SMTP id 17so4734823gxk.21
	for <tiff@lists.maptools.org>; Mon, 06 Apr 2009 08:35:35 -0700 (PDT)
MIME-Version: 1.0
Received: by 10.151.108.5 with SMTP id k5mr8595147ybm.137.1239032135032; Mon, 
	06 Apr 2009 08:35:35 -0700 (PDT)
Date: Mon, 6 Apr 2009 11:35:35 -0400
Message-ID: <949801310904060835p31b40e63h37e6c4a2073124f6@mail.gmail.com>
From: Christian Henning <chhenning@gmail.com>
To: libtiff - Liste <tiff@lists.maptools.org>
Subject: [Tiff] Writing tiff to in-memory buffer
X-BeenThere: tiff@lists.maptools.org
X-Mailman-Version: 2.1.9
Precedence: list
List-Id: TIFF and libtiff discussion <tiff.lists.maptools.org>
List-Unsubscribe: <http://lists.maptools.org/mailman/listinfo/tiff>,
	<mailto:tiff-request@lists.maptools.org?subject=unsubscribe>
List-Archive: <http://lists.maptools.org/mailman/private/tiff>
List-Post: <mailto:tiff@lists.maptools.org>
List-Help: <mailto:tiff-request@lists.maptools.org?subject=help>
List-Subscribe: <http://lists.maptools.org/mailman/listinfo/tiff>,
	<mailto:tiff-request@lists.maptools.org?subject=subscribe>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Sender: tiff-bounces@lists.maptools.org
Errors-To: tiff-bounces@lists.maptools.org

Hi there, I'm extending my code to allow writing to an in-memory
buffer using std::stringstream. I'm using stringstream successfully
with libpng and libjpeg. Though, I thought libtiff can do that too.
I'm actually pretty sure it can and the fault is on my side. Please
consider the following code:


static tsize_t read_proc( thandle_t fd
                        , tdata_t buf
                        , tsize_t size
                        )
{
    return size;
}

static tsize_t write_proc( thandle_t handle
                         , tdata_t   buf
                         , tsize_t   size
                         )
{
    std::ostream* os = reinterpret_cast< std::ostream* >( handle );
    boost::gil::detail::io_error_if( os->write( reinterpret_cast<char
const*>( buf )
                          , static_cast<std::streamsize>( size )
                          ).bad() == true
               , "ostream_device: Cannot write."
               );

    return size;
}

static toff_t seek_proc( thandle_t handle
                       , toff_t    off
                       , int       i
                       )
{
    std::ostream* os = reinterpret_cast< std::ostream* >( handle );

    os->seekp( off
             , i == SEEK_SET
             ? std::ios::beg
             : ( i == SEEK_CUR
                 ? std::ios::cur
                 : std::ios::end )
              );

    return off;
}

static int close_proc( thandle_t handle )
{
    std::ostream* os = reinterpret_cast< std::ostream* >( handle );
    os->operator<< ( std::flush );

    return 0;
}

static toff_t size_proc( thandle_t handle )
{
    return 0;
}

void error_handler( const char* module, const char* fmt, va_list ap )
{
}

int main()
{
    TIFFSetErrorHandler(&error_handler);

    stringstream ss( ios_base::in | ios_base::out | ios_base::binary );

    TIFF* t = TIFFClientOpen( "dummy"
                            , "w"
                            , &ss
                            , &read_proc
                            , &write_proc
                            , &seek_proc
                            , &close_proc
                            , &size_proc
                            , NULL
                            , NULL
                            );

}

TIFFClientOpen returns zero while the error handler tells me:

"Not a TIFF or MDI file, bad magic number %d (0x%x)"

I'm using libtiff version 3 something.

Someone any idea what I'm doing wrong here?

Thanks,
Christian
_______________________________________________
Tiff mailing list: Tiff@lists.maptools.org
http://lists.maptools.org/mailman/listinfo/tiff
http://www.remotesensing.org/libtiff/
