370919 | 2002-03-20 | Martin Nilsson | | #pike __REAL_VERSION__
|
e567fd | 2004-05-23 | Martin Nilsson | |
|
a59c9e | 2002-01-17 | Martin Nilsson | |
|
039e25 | 2002-09-29 | Manual system | |
|
a59c9e | 2002-01-17 | Martin Nilsson | |
#define M_SOF0 0xC0 /* Start Of Frame N */
#define M_SOF1 0xC1 /* N indicates which compression process */
#define M_SOF2 0xC2 /* Only SOF0-SOF2 are now in common use */
#define M_SOF3 0xC3
#define M_SOF5 0xC5 /* NB: codes C4 and CC are NOT SOF markers */
#define M_SOF6 0xC6
#define M_SOF7 0xC7
#define M_SOF9 0xC9
#define M_SOF10 0xCA
#define M_SOF11 0xCB
#define M_SOF13 0xCD
#define M_SOF14 0xCE
#define M_SOF15 0xCF
#define M_SOI 0xD8 /* Start Of Image (beginning of datastream) */
#define M_EOI 0xD9 /* End Of Image (end of datastream) */
#define M_SOS 0xDA /* Start Of Scan (begins compressed data) */
#define M_COM 0xFE /* COMment */
|
039e25 | 2002-09-29 | Manual system | | static int(0..255) read_1_byte(Stdio.File f)
|
a59c9e | 2002-01-17 | Martin Nilsson | | {
return f->read(1)[0];
}
|
039e25 | 2002-09-29 | Manual system | | static int(0..65535) read_2_bytes(Stdio.File f)
|
a59c9e | 2002-01-17 | Martin Nilsson | | {
int c;
sscanf( f->read(2), "%2c", c );
return c;
}
|
039e25 | 2002-09-29 | Manual system | | static int(0..65535) read_2_bytes_intel(Stdio.File f)
|
a59c9e | 2002-01-17 | Martin Nilsson | | {
int c;
sscanf( f->read(2), "%-2c", c);
return c;
}
|
039e25 | 2002-09-29 | Manual system | | static int first_marker(Stdio.File f)
|
a59c9e | 2002-01-17 | Martin Nilsson | | {
int c1, c2;
|
58eee3 | 2004-05-23 | Martin Nilsson | | sscanf(f->read(2), "%c%c", c1, c2);
|
e567fd | 2004-05-23 | Martin Nilsson | | if (c1==0xFF||c2==M_SOI) return 1;
return 0;
|
a59c9e | 2002-01-17 | Martin Nilsson | | }
|
039e25 | 2002-09-29 | Manual system | | static int next_marker(Stdio.File f)
|
a59c9e | 2002-01-17 | Martin Nilsson | | {
int c;
int discarded_bytes = 0;
c = read_1_byte(f);
while (c != 0xFF) {
discarded_bytes++;
c = read_1_byte(f);
}
do {
c = read_1_byte(f);
} while (c == 0xFF);
return c;
}
|
039e25 | 2002-09-29 | Manual system | | static int skip_variable(Stdio.File f)
|
a59c9e | 2002-01-17 | Martin Nilsson | | {
int length = read_2_bytes(f);
|
58eee3 | 2004-05-23 | Martin Nilsson | | if (length < 2) return 0;
|
a59c9e | 2002-01-17 | Martin Nilsson | | length -= 2;
f->seek(f->tell()+length);
return 1;
}
|
039e25 | 2002-09-29 | Manual system | |
|
a59c9e | 2002-01-17 | Martin Nilsson | | array(int) get_JPEG(Stdio.File f)
{
int marker;
|
58eee3 | 2004-05-23 | Martin Nilsson | |
if (!first_marker(f))
|
a59c9e | 2002-01-17 | Martin Nilsson | | return 0;
for (;;)
{
marker = next_marker(f);
switch (marker) {
case M_SOF0:
case M_SOF1:
case M_SOF2:
case M_SOF3:
case M_SOF5:
case M_SOF6:
case M_SOF7:
case M_SOF9:
case M_SOF10:
case M_SOF11:
case M_SOF13:
case M_SOF14:
case M_SOF15:
|
58eee3 | 2004-05-23 | Martin Nilsson | | int image_height, image_width;
sscanf(f->read(7), "%*3s%2c%2c", image_width, image_height);
|
a59c9e | 2002-01-17 | Martin Nilsson | | return ({ image_width,image_height });
break;
case M_SOS:
return 0;
case M_EOI:
return 0;
default:
|
58eee3 | 2004-05-23 | Martin Nilsson | | if(!skip_variable(f)) return 0;
|
a59c9e | 2002-01-17 | Martin Nilsson | | break;
}
}
}
|
039e25 | 2002-09-29 | Manual system | |
|
a59c9e | 2002-01-17 | Martin Nilsson | | array(int) get_GIF(Stdio.File f)
{
int offs=f->tell();
if(f->read(3)!="GIF") return 0;
f->seek(offs+6);
|
e567fd | 2004-05-23 | Martin Nilsson | | return array_sscanf(file->read(4), "%-2c%-2c");
|
a59c9e | 2002-01-17 | Martin Nilsson | | }
|
039e25 | 2002-09-29 | Manual system | |
|
a59c9e | 2002-01-17 | Martin Nilsson | | array(int) get_PNG(Stdio.File f)
{
int offs=f->tell();
f->seek(offs+1);
if(f->read(3)!="PNG") return 0;
f->seek(offs+12);
if(f->read(4)!="IHDR") return 0;
|
e567fd | 2004-05-23 | Martin Nilsson | | return array_sscanf(file->read(8), "%4c%4c");
|
a59c9e | 2002-01-17 | Martin Nilsson | | }
|
58eee3 | 2004-05-23 | Martin Nilsson | |
|
e567fd | 2004-05-23 | Martin Nilsson | |
|
58eee3 | 2004-05-23 | Martin Nilsson | | array(int) get(string|Stdio.File file) {
if(stringp(file)) file = Stdio.FakeFile(file);
switch(file->read(6)) {
case "GIF87a":
case "GIF89a":
return array_sscanf(file->read(4), "%-2c%-2c") + ({ "gif" });
case "\x89PNG\r\n":
file->read(6+4);
return array_sscanf(file->read(8), "%4c%4c") + ({ "png" });
default:
file->seek(file->tell()-6);
array ret = get_JPEG(file);
return ret ? ret+({ "jpeg" }) : 0;
}
|
a59c9e | 2002-01-17 | Martin Nilsson | | }
|