Anyone managed to read the data of a picture to a buffer exporting it to disk? I used the openPictFile function, but it will not read the picture to its end. I guess I will need to export it to a file and read it back. That is what I tried: Buffer readPictureData (Object o) { Stream x = openPictFile (current Object) Stat s = create x int fileSize = size s Buffer b = create (fileSize+100) print "Size: " fileSize "\n" x -> b; if (length b != fileSize) print "Not all data of picture could be read!" (length b) " of " fileSize " bytes read" delete s close x return null Buffer } Buffer b = readPictureData current
Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS
Mathias Mamsch - Fri Feb 18 09:56:51 EST 2011 |
Re: Read picture data to buffer? Should line 7 be "x >> b"? I think what you have will read a "line", not the entire contents. ... from doors.exe: Stream ::->(Stream,Buffer) BufferReadLine Stream ::>>(Stream,Buffer) BufferRead ... Wonder what this will do: int read(Stream,Buffer,int) ReadBuffer
Nit-Picks: <1> Line 2 should use "o" not "current Object". <1> line 11 should return "b" not "null Buffer". <3> line 13 would be easier to understand if you said "current Object". |
Re: Read picture data to buffer? llandale - Fri Feb 18 11:05:03 EST 2011
Nit-Picks: <1> Line 2 should use "o" not "current Object". <1> line 11 should return "b" not "null Buffer". <3> line 13 would be easier to understand if you said "current Object". So if you find your example, that might be very helpful. Regards, Mathias Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS |
Re: Read picture data to buffer? Mathias Mamsch - Fri Feb 18 12:35:30 EST 2011 Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS
Am unsteady ground here, but it appears reading a file will end when it encounters a "null" character, which is just ascii zero. Pictures and other binary files have that character a lot. You can read the entire contents of a picture from the file system using:
Buffer readPictureData (Object o) { Stream x = openPictFile (o) Stat s = create x int fileSize = size s int OldLength = -1 Buffer b = create () print "Size: " fileSize "\n" while(length(b) < fileSize and length(b) > OldLength) { OldLength = length(b) x >> b; // This failed, trying to keep reading the Buffer after it found a null } if (length b != fileSize) print "Not all data of picture could be read! " (length b) " of " fileSize " bytes read" delete s close x return null Buffer } Buffer b = readPictureData current Object //halt string NameFile = "c:/Documents and Settings/landalo/My Documents/TimeCards/Landale-Signature.jpg" Stream x if (confirm("Binary?")) // then x = read(binary NameFile) else x = read(NameFile) char c int Count = 0 while(true) { x >> c if (end x) break if (null c) print Count "\tNull\t" Count++ } print "\n" string NamePict for NamePict in pictures(current Project) do { print NamePict "\n" }
|
Re: Read picture data to buffer / read binary files llandale - Fri Feb 18 16:01:20 EST 2011
Am unsteady ground here, but it appears reading a file will end when it encounters a "null" character, which is just ascii zero. Pictures and other binary files have that character a lot. You can read the entire contents of a picture from the file system using:
Buffer readPictureData (Object o) { Stream x = openPictFile (o) Stat s = create x int fileSize = size s int OldLength = -1 Buffer b = create () print "Size: " fileSize "\n" while(length(b) < fileSize and length(b) > OldLength) { OldLength = length(b) x >> b; // This failed, trying to keep reading the Buffer after it found a null } if (length b != fileSize) print "Not all data of picture could be read! " (length b) " of " fileSize " bytes read" delete s close x return null Buffer } Buffer b = readPictureData current Object //halt string NameFile = "c:/Documents and Settings/landalo/My Documents/TimeCards/Landale-Signature.jpg" Stream x if (confirm("Binary?")) // then x = read(binary NameFile) else x = read(NameFile) char c int Count = 0 while(true) { x >> c if (end x) break if (null c) print Count "\tNull\t" Count++ } print "\n" string NamePict for NamePict in pictures(current Project) do { print NamePict "\n" }
Thanks Louie, you made me think straight again ;-) I tried reading the file char by char, but got a different buffer length than the filesize. I did not think that it just would leave the null characters from the buffer, adding a null char will just do nothing. So here is the final read binary file code (which will escape null character to be \001\002 while \001 characters themself will be encoded \001\001). I added a write binary file method that allows to to write the escaped binary files back, unescaping the null characters. Mathias pragma runLim, 4000000 void readBinaryFile(string NameFile, Buffer b) { Stream x = read binary NameFile char c while(true) { x >> c if (end x) break if (c == '\001') b += "\001\001" else if (null c) b += "\001\002" else { b += c } } close x } void writeBinaryFile(string NameFile, Buffer b) { Stream x = write binary NameFile char c int count = length b int i = 0; while (i < count) { c = b[i]; if (c == '\001') { char cNext = b[++i] if (cNext == '\002') { x << '\000'; i++; continue } } x << c i++ } close x } int fileSize (string fn) { Stat s = create fn int result = size s delete s return result } string NameFile = "H:\\temp\\dp24.png" Buffer b1 = create(), b2 = create() readBinaryFile (NameFile, b1) writeBinaryFile (NameFile ".out", b1) readBinaryFile (NameFile ".out", b2) print "Equal: " (b1 == b2) "\n" print "Buffer Sizes: " (length b1) "/" (length b2) "\n" print "File Sizes:" (fileSize NameFile) "/" (fileSize NameFile ".out") "\n"
Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS
|
Re: Read picture data to buffer / read binary files Mathias Mamsch - Fri Feb 18 16:46:02 EST 2011
Thanks Louie, you made me think straight again ;-) I tried reading the file char by char, but got a different buffer length than the filesize. I did not think that it just would leave the null characters from the buffer, adding a null char will just do nothing. So here is the final read binary file code (which will escape null character to be \001\002 while \001 characters themself will be encoded \001\001). I added a write binary file method that allows to to write the escaped binary files back, unescaping the null characters. Mathias pragma runLim, 4000000 void readBinaryFile(string NameFile, Buffer b) { Stream x = read binary NameFile char c while(true) { x >> c if (end x) break if (c == '\001') b += "\001\001" else if (null c) b += "\001\002" else { b += c } } close x } void writeBinaryFile(string NameFile, Buffer b) { Stream x = write binary NameFile char c int count = length b int i = 0; while (i < count) { c = b[i]; if (c == '\001') { char cNext = b[++i] if (cNext == '\002') { x << '\000'; i++; continue } } x << c i++ } close x } int fileSize (string fn) { Stat s = create fn int result = size s delete s return result } string NameFile = "H:\\temp\\dp24.png" Buffer b1 = create(), b2 = create() readBinaryFile (NameFile, b1) writeBinaryFile (NameFile ".out", b1) readBinaryFile (NameFile ".out", b2) print "Equal: " (b1 == b2) "\n" print "Buffer Sizes: " (length b1) "/" (length b2) "\n" print "File Sizes:" (fileSize NameFile) "/" (fileSize NameFile ".out") "\n"
Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS
Hi; I open a thread in relation with this thread. My thread is 'Export OLE from Doors to a word report'. Can someone help me to resolve my problem Thank you |