MESSAGE
DATE | 2016-12-11 |
FROM | ruben safir
|
SUBJECT | Subject: [Learn] Fwd: Re: png data format
|
From learn-bounces-at-nylxs.com Sun Dec 11 00:40:00 2016 Return-Path: X-Original-To: archive-at-mrbrklyn.com Delivered-To: archive-at-mrbrklyn.com Received: from www.mrbrklyn.com (www.mrbrklyn.com [96.57.23.82]) by mrbrklyn.com (Postfix) with ESMTP id 5FF1F161312; Sun, 11 Dec 2016 00:40:00 -0500 (EST) X-Original-To: learn-at-nylxs.com Delivered-To: learn-at-nylxs.com Received: from [10.0.0.62] (flatbush.mrbrklyn.com [10.0.0.62]) by mrbrklyn.com (Postfix) with ESMTP id 4C8D2160E77 for ; Sun, 11 Dec 2016 00:39:56 -0500 (EST) References: To: learn-at-nylxs.com From: ruben safir X-Forwarded-Message-Id: Message-ID: <885db5b8-cd28-1647-d931-d18f4bfb5e5a-at-mrbrklyn.com> Date: Sun, 11 Dec 2016 00:39:56 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.5.0 MIME-Version: 1.0 In-Reply-To: Subject: [Learn] Fwd: Re: png data format X-BeenThere: learn-at-nylxs.com X-Mailman-Version: 2.1.17 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: learn-bounces-at-nylxs.com Sender: "Learn"
-------- Forwarded Message -------- Subject: Re: png data format Date: 7 Dec 2016 19:49:12 GMT From: Jens Thoms Toerring Organization: Freie Universitaet Berlin Newsgroups: comp.unix.programmer References:
ruben safir wrote: > I'm having trouble with this imput of data from a PNG image. The > specification says that "chunks" have a 4 byte field that is the length of > the attached data segment. I tried to read the length in for a chunk that > has a length of 13, which was confirmed in a hexdump
> 0000000 211 120 116 107 015 012 032 012 -->>000 000 000 015<<-- 111 110 104 122 > 0000010 000 000 041 215 000 000 007 165 010 006 000 000 001 206 055 074 > 0000020 336 000 000 000 004 147 101 115 101 000 000 261 217 013 374 141
> I am storing the data in a uint32_t variable using the following code, but > the value keeps showing up with a huge number 218103808 which happens to be > the number that is evaluated by iostream for the value of the whole chunk
> done reading header
> Sizeof Chunk 4 > Raw Chunk Number 0: 218103808 > ***LENGTH**** > Length value => 218103808
The data are stored in big-endian order, i.e. with the most significant byte coming first, but you seem to have read it in as a 32-bit integer on a machine using little-endian byte order, e.g. with a machine with an Intel processor, where the least-significant byte comes first in memory. That's also what they expect when reading in binary data from a file. So simply read in the four bytes separately into e. g. an array
uint8_t bytes[4]
with the first byte you read from the file going into the first array element etc. Then do
uint32_t len = ((bytes[0] * 256 + bytes[1]) * 256 + bytes[2]) * 256 + bytes[3];
and you should get the correct value. The same applies, of course, also for other fields containing values that occupy multiple bytes! This then will work correctly on both big- and little-endian machines. Regards, Jens -- \ Jens Thoms Toerring ___ jt-at-toerring.de \__________________________ http://toerring.de _______________________________________________ Learn mailing list Learn-at-nylxs.com http://lists.mrbrklyn.com/mailman/listinfo/learn
|
|