Read width & Height of jpeg file
-
Hi Guys, I'm trying to get into XNA v1.0 (..last time I touched it was Beta 1.) ...So, I'm building myself a terrain loader. Trying to get my Terrain class set up so that it can be given a heightmap file and it can initialize the size of the data array based upon the width & height of the file that is passed to it. Does anyone know of a way(preferrably a simple way) of accepting the path of a jpeg and getting the width and height out of the FileInfo?(I'm making an assumption that is where it is located, but I do not know for a fact)
Welcome my son...Welcome..to the Machine
-
Hi Guys, I'm trying to get into XNA v1.0 (..last time I touched it was Beta 1.) ...So, I'm building myself a terrain loader. Trying to get my Terrain class set up so that it can be given a heightmap file and it can initialize the size of the data array based upon the width & height of the file that is passed to it. Does anyone know of a way(preferrably a simple way) of accepting the path of a jpeg and getting the width and height out of the FileInfo?(I'm making an assumption that is where it is located, but I do not know for a fact)
Welcome my son...Welcome..to the Machine
-
Hi Guys, I'm trying to get into XNA v1.0 (..last time I touched it was Beta 1.) ...So, I'm building myself a terrain loader. Trying to get my Terrain class set up so that it can be given a heightmap file and it can initialize the size of the data array based upon the width & height of the file that is passed to it. Does anyone know of a way(preferrably a simple way) of accepting the path of a jpeg and getting the width and height out of the FileInfo?(I'm making an assumption that is where it is located, but I do not know for a fact)
Welcome my son...Welcome..to the Machine
Hi, the only way to get JPEG size without using Image class is by reading (part of) the JPEG file yourself, as in the following code:
public static Size GetJpegImageSize(string filename) {
FileStream stream=null;
BinaryReader rdr=null;
try {
stream=File.OpenRead(filename);
rdr=new BinaryReader(stream);
// keep reading packets until we find one that contains Size info
for(; ; ) {
byte code=rdr.ReadByte();
if(code!=0xFF) throw new ApplicationException(
"Unexpected value in file "+filename);
code=rdr.ReadByte();
switch(code) {
// filler byte
case 0xFF:
stream.Position--;
break;
// packets without data
case 0xD0: case 0xD1: case 0xD2: case 0xD3: case 0xD4:
case 0xD5: case 0xD6: case 0xD7: case 0xD8: case 0xD9:
break;
// packets with size information
case 0xC0: case 0xC1: case 0xC2: case 0xC3:
case 0xC4: case 0xC5: case 0xC6: case 0xC7:
case 0xC8: case 0xC9: case 0xCA: case 0xCB:
case 0xCC: case 0xCD: case 0xCE: case 0xCF:
ReadBEUshort(rdr);
rdr.ReadByte();
ushort h=ReadBEUshort(rdr);
ushort w=ReadBEUshort(rdr);
return new Size(w, h);
// irrelevant variable-length packets
default:
int len=ReadBEUshort(rdr);
stream.Position+=len-2;
break;
}
}
} finally {
if(rdr!=null) rdr.Close();
if(stream!=null) stream.Close();
}
}private static ushort ReadBEUshort(BinaryReader rdr) {
ushort hi=rdr.ReadByte();
hi<<=8;
ushort lo=rdr.ReadByte();
return (ushort)(hi|lo);
}A JPEG file contains a lot of packets (with Hufman tables and everything). The above code does not know much about JPEG, it simply tries to jump from one packet to the next until it finds one with size info. :)
Luc Pattyn [My Articles] [Forum Guidelines]
-
Hi, the only way to get JPEG size without using Image class is by reading (part of) the JPEG file yourself, as in the following code:
public static Size GetJpegImageSize(string filename) {
FileStream stream=null;
BinaryReader rdr=null;
try {
stream=File.OpenRead(filename);
rdr=new BinaryReader(stream);
// keep reading packets until we find one that contains Size info
for(; ; ) {
byte code=rdr.ReadByte();
if(code!=0xFF) throw new ApplicationException(
"Unexpected value in file "+filename);
code=rdr.ReadByte();
switch(code) {
// filler byte
case 0xFF:
stream.Position--;
break;
// packets without data
case 0xD0: case 0xD1: case 0xD2: case 0xD3: case 0xD4:
case 0xD5: case 0xD6: case 0xD7: case 0xD8: case 0xD9:
break;
// packets with size information
case 0xC0: case 0xC1: case 0xC2: case 0xC3:
case 0xC4: case 0xC5: case 0xC6: case 0xC7:
case 0xC8: case 0xC9: case 0xCA: case 0xCB:
case 0xCC: case 0xCD: case 0xCE: case 0xCF:
ReadBEUshort(rdr);
rdr.ReadByte();
ushort h=ReadBEUshort(rdr);
ushort w=ReadBEUshort(rdr);
return new Size(w, h);
// irrelevant variable-length packets
default:
int len=ReadBEUshort(rdr);
stream.Position+=len-2;
break;
}
}
} finally {
if(rdr!=null) rdr.Close();
if(stream!=null) stream.Close();
}
}private static ushort ReadBEUshort(BinaryReader rdr) {
ushort hi=rdr.ReadByte();
hi<<=8;
ushort lo=rdr.ReadByte();
return (ushort)(hi|lo);
}A JPEG file contains a lot of packets (with Hufman tables and everything). The above code does not know much about JPEG, it simply tries to jump from one packet to the next until it finds one with size info. :)
Luc Pattyn [My Articles] [Forum Guidelines]
-
Thanks a TON for that...did you just pull that off the top of your head? Or did you have reference material for that? What information did you refer to?
Welcome my son...Welcome..to the Machine
Hi, I once wrote a JPEG file viewer based on the JPEG standard[^]. And today I threw out everything that was not needed when only Size matters... :)
Luc Pattyn [My Articles] [Forum Guidelines]
-
Hi, I once wrote a JPEG file viewer based on the JPEG standard[^]. And today I threw out everything that was not needed when only Size matters... :)
Luc Pattyn [My Articles] [Forum Guidelines]
-
...how long did it take you to parse through all of the W3C info to turn it into something useful?
Welcome my son...Welcome..to the Machine
Actually not very long, I did not read most of the spec, I just looked for the overall structure (packets with 2B code and 2B length field) and a couple of specifics that did interest me at the time. And with a simple viewer, I just looked inside a lot of JPEG files to see what kind of information typically gets included. BTW a lot of cameras add exposure information (make and model, datetime, image corrections, etc) to the JPEG files they create, and the Image class allows you to retrieve these using the getPropertyItem() method. :)
Luc Pattyn [My Articles] [Forum Guidelines]
-
Hi, the only way to get JPEG size without using Image class is by reading (part of) the JPEG file yourself, as in the following code:
public static Size GetJpegImageSize(string filename) {
FileStream stream=null;
BinaryReader rdr=null;
try {
stream=File.OpenRead(filename);
rdr=new BinaryReader(stream);
// keep reading packets until we find one that contains Size info
for(; ; ) {
byte code=rdr.ReadByte();
if(code!=0xFF) throw new ApplicationException(
"Unexpected value in file "+filename);
code=rdr.ReadByte();
switch(code) {
// filler byte
case 0xFF:
stream.Position--;
break;
// packets without data
case 0xD0: case 0xD1: case 0xD2: case 0xD3: case 0xD4:
case 0xD5: case 0xD6: case 0xD7: case 0xD8: case 0xD9:
break;
// packets with size information
case 0xC0: case 0xC1: case 0xC2: case 0xC3:
case 0xC4: case 0xC5: case 0xC6: case 0xC7:
case 0xC8: case 0xC9: case 0xCA: case 0xCB:
case 0xCC: case 0xCD: case 0xCE: case 0xCF:
ReadBEUshort(rdr);
rdr.ReadByte();
ushort h=ReadBEUshort(rdr);
ushort w=ReadBEUshort(rdr);
return new Size(w, h);
// irrelevant variable-length packets
default:
int len=ReadBEUshort(rdr);
stream.Position+=len-2;
break;
}
}
} finally {
if(rdr!=null) rdr.Close();
if(stream!=null) stream.Close();
}
}private static ushort ReadBEUshort(BinaryReader rdr) {
ushort hi=rdr.ReadByte();
hi<<=8;
ushort lo=rdr.ReadByte();
return (ushort)(hi|lo);
}A JPEG file contains a lot of packets (with Hufman tables and everything). The above code does not know much about JPEG, it simply tries to jump from one packet to the next until it finds one with size info. :)
Luc Pattyn [My Articles] [Forum Guidelines]
...I was thinking that I was going to have to find a way to pull the FileHeader off, store it in the .Net equivalent of a BITMAPFILEHEADER. Then, pull the InfoHeader off of the file and store it in the .NET equivalent of a BITMAPINFOHEADER...& pull the width & height from it ...or by using WMI. That was more along my line of thinking.
Welcome my son...Welcome..to the Machine
-
...I was thinking that I was going to have to find a way to pull the FileHeader off, store it in the .Net equivalent of a BITMAPFILEHEADER. Then, pull the InfoHeader off of the file and store it in the .NET equivalent of a BITMAPINFOHEADER...& pull the width & height from it ...or by using WMI. That was more along my line of thinking.
Welcome my son...Welcome..to the Machine
Hi, The structs you mentioned exist in a .bmp file, not in a JPEG file. And before you ask, no I dont have a BMP Viewer yet. As for WMI, I dont think WMI would help looking at JPEGs; it is involved in system resources. :)
Luc Pattyn [My Articles] [Forum Guidelines]
-
Hi, The structs you mentioned exist in a .bmp file, not in a JPEG file. And before you ask, no I dont have a BMP Viewer yet. As for WMI, I dont think WMI would help looking at JPEGs; it is involved in system resources. :)
Luc Pattyn [My Articles] [Forum Guidelines]
I know that WMI is for system resources, but I saw an example while I was doing online research where somebody went to the WMI classes for the owner of a specific file (I think he went to one of the Operating System's Win32_Security classes) ...I was just thinking that the information might be included with it *somewhere*
Welcome my son...Welcome..to the Machine
-
Hi, The structs you mentioned exist in a .bmp file, not in a JPEG file. And before you ask, no I dont have a BMP Viewer yet. As for WMI, I dont think WMI would help looking at JPEGs; it is involved in system resources. :)
Luc Pattyn [My Articles] [Forum Guidelines]
right...I knew that the BITMAPINFOHEADER was for a bmp & not a jpg ;P ...but I was hoping that something could be done to mimic its usage. (My bmp viewer is in C++...never ported it to .NET) ...bmp's are actually extremely simple, barely a step up from a PPM. I'm sure you could find it yourself, but the header just looks like this:
height field bits per pixel field compression field colors field important colors field
...then you just have to remember that bitmaps are encoded as BGR values instead of RGB, so you have to map them accordingly
Welcome my son...Welcome..to the Machine
-
Hi, the only way to get JPEG size without using Image class is by reading (part of) the JPEG file yourself, as in the following code:
public static Size GetJpegImageSize(string filename) {
FileStream stream=null;
BinaryReader rdr=null;
try {
stream=File.OpenRead(filename);
rdr=new BinaryReader(stream);
// keep reading packets until we find one that contains Size info
for(; ; ) {
byte code=rdr.ReadByte();
if(code!=0xFF) throw new ApplicationException(
"Unexpected value in file "+filename);
code=rdr.ReadByte();
switch(code) {
// filler byte
case 0xFF:
stream.Position--;
break;
// packets without data
case 0xD0: case 0xD1: case 0xD2: case 0xD3: case 0xD4:
case 0xD5: case 0xD6: case 0xD7: case 0xD8: case 0xD9:
break;
// packets with size information
case 0xC0: case 0xC1: case 0xC2: case 0xC3:
case 0xC4: case 0xC5: case 0xC6: case 0xC7:
case 0xC8: case 0xC9: case 0xCA: case 0xCB:
case 0xCC: case 0xCD: case 0xCE: case 0xCF:
ReadBEUshort(rdr);
rdr.ReadByte();
ushort h=ReadBEUshort(rdr);
ushort w=ReadBEUshort(rdr);
return new Size(w, h);
// irrelevant variable-length packets
default:
int len=ReadBEUshort(rdr);
stream.Position+=len-2;
break;
}
}
} finally {
if(rdr!=null) rdr.Close();
if(stream!=null) stream.Close();
}
}private static ushort ReadBEUshort(BinaryReader rdr) {
ushort hi=rdr.ReadByte();
hi<<=8;
ushort lo=rdr.ReadByte();
return (ushort)(hi|lo);
}A JPEG file contains a lot of packets (with Hufman tables and everything). The above code does not know much about JPEG, it simply tries to jump from one packet to the next until it finds one with size info. :)
Luc Pattyn [My Articles] [Forum Guidelines]
ok...one thing I absolutely hate is how .NET throws this for the code you provided:
Error 6 Control cannot fall through from one case label ('case 207:') to another D:\XNA\Files\Projects\Terrain\TerrainWIP\Terrain\Terrain\JPEG.cs 61 58 Terrain
Was your code C#.NET code? How were you able to force it to fall through the cases?
Welcome my son...Welcome..to the Machine
-
ok...one thing I absolutely hate is how .NET throws this for the code you provided:
Error 6 Control cannot fall through from one case label ('case 207:') to another D:\XNA\Files\Projects\Terrain\TerrainWIP\Terrain\Terrain\JPEG.cs 61 58 Terrain
Was your code C#.NET code? How were you able to force it to fall through the cases?
Welcome my son...Welcome..to the Machine
Hi, I did not actually run the code I posted, but it seems OK to me. cases should be empty (that is how you can list cases to share all their code) or end on a change-of-flow (break, return, throw...) Did you somehow change the code and violate the above ? I guess you did, since 207 is 0xCF and that one ended on return... :)
Luc Pattyn [My Articles] [Forum Guidelines]
-
Hi, I did not actually run the code I posted, but it seems OK to me. cases should be empty (that is how you can list cases to share all their code) or end on a change-of-flow (break, return, throw...) Did you somehow change the code and violate the above ? I guess you did, since 207 is 0xCF and that one ended on return... :)
Luc Pattyn [My Articles] [Forum Guidelines]
no...identical code. Cut & pasted it. .NET's saying that it won't let a case fall through to execute code from another case. ...OxCF was the case that actually had code to execute. ..the others fell through to 0xCF.
Welcome my son...Welcome..to the Machine
-
no...identical code. Cut & pasted it. .NET's saying that it won't let a case fall through to execute code from another case. ...OxCF was the case that actually had code to execute. ..the others fell through to 0xCF.
Welcome my son...Welcome..to the Machine
-
Hi, I did not actually run the code I posted, but it seems OK to me. cases should be empty (that is how you can list cases to share all their code) or end on a change-of-flow (break, return, throw...) Did you somehow change the code and violate the above ? I guess you did, since 207 is 0xCF and that one ended on return... :)
Luc Pattyn [My Articles] [Forum Guidelines]
-
...even if you have a return, it still wants you to have the break after it
Welcome my son...Welcome..to the Machine
My Visual Studio C# 2005 Express Edition is happy without a break after a return, and produces a warning "unreachable code" if there is such a break; both seem logical to me. Are you using an different, maybe older, IDE ? :)
Luc Pattyn [My Articles] [Forum Guidelines]
-
...even if you have a return, it still wants you to have the break after it
Welcome my son...Welcome..to the Machine
I have now tried with Visual Studio 7.1 and it behaves identically: return without break is fine, return+break gives warning. :)
Luc Pattyn [My Articles] [Forum Guidelines]
-
I have now tried with Visual Studio 7.1 and it behaves identically: return without break is fine, return+break gives warning. :)
Luc Pattyn [My Articles] [Forum Guidelines]