|
|
| version 1.78, 2002/11/21 09:47:34 | version 1.79, 2002/11/21 11:04:39 |
|---|---|
| Line 45 public: // Methoded | Line 45 public: // Methoded |
| class Measure_reader { | class Measure_reader { |
| public: | public: |
| virtual size_t read(const void *&buf, size_t limit)=0; | virtual size_t read(const void *&buf, size_t limit)=0; |
| virtual void seek(long delta)=0; | virtual void seek(long value, int whence)=0; |
| virtual long tell()=0; | |
| }; | }; |
| class Measure_file_reader: public Measure_reader { | class Measure_file_reader: public Measure_reader { |
| Line 69 public: | Line 70 public: |
| return read_size; | return read_size; |
| } | } |
| /*override*/void seek(long delta) { | /*override*/void seek(long value, int whence) { |
| if(lseek(f, delta, SEEK_CUR)<0) | if(lseek(f, value, whence)<0) |
| throw Exception("file.seek", | throw Exception("file.seek", |
| &file_name, | &file_name, |
| "seek(delta=%ld) failed: %s (%d), actual filename '%s'", | "seek(value=%ld, whence=%d) failed: %s (%d), actual filename '%s'", |
| delta, strerror(errno), errno, fname); | value, whence, strerror(errno), errno, fname); |
| } | } |
| /*override*/long tell() { return ::tell(f); } | |
| private: | private: |
| Pool& pool; | Pool& pool; |
| const String& file_name; const char *fname; | const String& file_name; const char *fname; |
| Line 96 public: | Line 99 public: |
| return to_read; | return to_read; |
| } | } |
| /*override*/void seek(long delta) { | /*override*/void seek(long value, int whence) { |
| size_t new_offset=offset+delta; | size_t new_offset; |
| switch(whence) { | |
| case SEEK_CUR: new_offset=offset+value; break; | |
| case SEEK_SET: new_offset=(size_t)value; break; | |
| default: throw Exception("file.seek", 0, "whence #%d not supported", 0, whence); break; | |
| } | |
| if((ssize_t)new_offset<0 || new_offset>size) | if((ssize_t)new_offset<0 || new_offset>size) |
| throw Exception("file.seek", | throw Exception("file.seek", |
| &file_name, | &file_name, |
| "seek(offset=%l) failed: out of buffer, new_offset>size (%l>%l) or new_offset<0", | "seek(value=%l, whence=%d) failed: out of buffer, new_offset>size (%l>%l) or new_offset<0", |
| delta, new_offset, size); | value, whence, new_offset, size); |
| offset=new_offset; | offset=new_offset; |
| } | } |
| /*override*/long tell() { return offset; } | |
| private: | private: |
| const void *buf; size_t size; | const void *buf; size_t size; |
| Line 151 struct JPG_Size_segment_body { | Line 162 struct JPG_Size_segment_body { |
| char numComponents; //< number of color components | char numComponents; //< number of color components |
| }; | }; |
| /// JPEG frame header | |
| struct JPG_Exif_segment_start { | |
| char signature[6]; // Exif\0\0 | |
| }; | |
| // | // |
| inline ushort x_endian_to_ushort(unsigned char L, unsigned char H) { | inline ushort x_endian_to_ushort(unsigned char L, unsigned char H) { |
| Line 190 void measure_jpeg(Pool& pool, const Stri | Line 206 void measure_jpeg(Pool& pool, const Stri |
| Measure_reader& reader, ushort& width, ushort& height) { | Measure_reader& reader, ushort& width, ushort& height) { |
| // JFIF format markers | // JFIF format markers |
| const unsigned char MARKER=0xFF; | const unsigned char MARKER=0xFF; |
| const unsigned char CODE_SIZE_FIRST=0xC0; | const unsigned char CODE_SIZE_A=0xC0; |
| const unsigned char CODE_SIZE_LAST=0xC3; | const unsigned char CODE_SIZE_B=0xC1; |
| const unsigned char CODE_SIZE_C=0xC2; | |
| const unsigned char CODE_SIZE_D=0xC3; | |
| const unsigned char CODE_EXIF=0xE1; | |
| void *buf; | void *buf; |
| const size_t prefix_size=2; | const size_t prefix_size=2; |
| Line 206 void measure_jpeg(Pool& pool, const Stri | Line 225 void measure_jpeg(Pool& pool, const Stri |
| origin_string, | origin_string, |
| "not JPEG file - wrong signature"); | "not JPEG file - wrong signature"); |
| bool found=false; | |
| while(true) { | while(true) { |
| void *buf; | long segment_base_offset=reader.tell()+2/*marker,code*/; |
| // Extract the segment header. | |
| if(reader.read(buf, sizeof(JPG_Segment_head))<sizeof(JPG_Segment_head)) | if(reader.read(buf, sizeof(JPG_Segment_head))<sizeof(JPG_Segment_head)) |
| break; | break; |
| JPG_Segment_head *head=(JPG_Segment_head *)buf; | JPG_Segment_head *head=(JPG_Segment_head *)buf; |
| // Verify that it's a valid segment. | // Verify that it's a valid segment. |
| if(head->marker!=MARKER) | if(head->marker!=MARKER) |
| break; | throw Exception("image.format", |
| origin_string, | |
| "not JPEG file - marker not found"); | |
| switch(head->code) { | |
| // http://www.ba.wakwak.com/~tsuruzoh/Computer/Digicams/exif-e.html | |
| case CODE_EXIF: | |
| { | |
| if(reader.read(buf, sizeof(JPG_Exif_segment_start))<sizeof(JPG_Exif_segment_start)) | |
| throw Exception("image.format", | |
| origin_string, | |
| "not JPEG file - can not fully read Exif segment start"); | |
| JPG_Exif_segment_start *start=(JPG_Exif_segment_start *)buf; | |
| if(memcmp(start->signature, "Exif\0\0", 4+2)!=0) //signature invalid? | |
| break; // ignore invalid block | |
| if(head->code >= CODE_SIZE_FIRST && head->code <= CODE_SIZE_LAST) { | // parse Exif block |
| // Segments that contain size info | } |
| if(reader.read(buf, sizeof(JPG_Size_segment_body))<sizeof(JPG_Size_segment_body)) | |
| break; | |
| JPG_Size_segment_body *body=(JPG_Size_segment_body *)buf; | |
| width=big_endian_to_ushort(body->width); | |
| height=big_endian_to_ushort(body->height); | |
| found=true; | |
| break; | break; |
| } else { | |
| // Dummy read to skip over data | |
| long offset=big_endian_to_ushort(head->length) - 2; | |
| reader.seek(offset); | |
| } | |
| } | |
| if(!found) | case CODE_SIZE_A: |
| throw Exception("image.format", | case CODE_SIZE_B: |
| origin_string, | case CODE_SIZE_C: |
| "broken JPEG file - size frame not found"); | case CODE_SIZE_D: |
| { | |
| // Segments that contain size info | |
| if(reader.read(buf, sizeof(JPG_Size_segment_body))<sizeof(JPG_Size_segment_body)) | |
| throw Exception("image.format", | |
| origin_string, | |
| "not JPEG file - can not fully read Size segment"); | |
| JPG_Size_segment_body *body=(JPG_Size_segment_body *)buf; | |
| width=big_endian_to_ushort(body->width); | |
| height=big_endian_to_ushort(body->height); | |
| return; | |
| } | |
| }; | |
| reader.seek(segment_base_offset+big_endian_to_ushort(head->length), SEEK_SET); | |
| } | |
| throw Exception("image.format", | |
| origin_string, | |
| "broken JPEG file - size frame not found"); | |
| } | } |
| void measure_png(Pool& pool, const String *origin_string, | void measure_png(Pool& pool, const String *origin_string, |