Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Image Properties

Author  Topic 

budgie
Starting Member

18 Posts

Posted - 2004-11-02 : 07:43:37
Disclaimer: Before you reply you should know that I already know that storing images - in most circumstances - is a really dumb idea. I have also read the site FAQ with regards to this matter and I have also searched the forums for help on this without luck.

The problem that has arisen is not of my making but I have been brought in - as per bloody usual - to fix someone else's mess. The problem needs a solution based on the current setup as redoing it would be far to time consuming and out of the reach of this.

The problem lies in an SP that was rewritten to return the image dimensions and most of the time it works great. However recently an image was entered that caused the SP to return incorrect dimensions which isn't good for the app.

From my understanding of the SP - asking the orginal author isn't an option - the problem lies in how it handles JPG files, but I can't figure out what to change. I have written similar code but in VB and it doesn't translate well to T-SQL i.e. it just don't work captian! Anyway here is the <sarcasm>wonderful piece of code</sarcasm> that is breaking:

ALTER PROCEDURE ImageSize
@ImgID as int
AS

/* *******************************************************************************

Procedure usp_ImageSize returns the height and width of a specified image in the
Images table.

Inputs:

@ImgID - Integer containing the ID of the image for which to return
height and width.

Outputs:

A recorset is returned containing two columns - the first is the height in
pixels of the image, and the second is the width in pixels of the image.

If an invalid image ID is passed to the procedure, or the specified image
has an invalid extension or MIME type, a null value is returned.

Example call:

exec ImageSize 24

******************************************************************************* */

set nocount on
declare @MIMEType int
set @MIMEType = (select MIMEType from Images where [ID]=@ImgID)

if @MIMEType > 0 begin
declare @ext char(3)
declare @width int
declare @height int
declare @buf varbinary(10)
declare @hb smallint
declare @lb smallint
set @ext = upper((select Extension from MIMETypes where [ID]=@MIMEType))
if @ext = 'GIF' begin
set @buf = (SELECT SUBSTRING(ImageData, 1, 10) FROM images where [ID]=@ImgID)
set @hb = substring(@buf, 8, 1)
set @lb = substring(@buf, 7, 1)
set @width = (256 * @hb) + @lb
set @hb = substring(@buf, 10, 1)
set @lb = substring(@buf, 9, 1)
set @height = (256 * @hb) + @lb
end else begin
if @ext = 'JPG' begin
declare @fp int
declare @subcode int
set @subcode = 0
set @fp = 3
set @buf = (SELECT SUBSTRING(ImageData, 1, 10) FROM images where [ID]=@ImgID)
set @hb = substring(@buf, 2, 1)
set @lb = substring(@buf, 1, 1)
if (@hb = 216) and (@lb = 255) begin
while @subcode != 49407 begin
set @buf = (SELECT SUBSTRING(ImageData, @fp, 10) FROM images where [ID]=@ImgID)
set @hb = substring(@buf, 2, 1)
set @lb = substring(@buf, 1, 1)
set @hb = @hb & 240
set @subcode = (256 * @hb) + @lb
if @subcode = 49407 begin
set @hb = substring(@buf, 6, 1)
set @lb = substring(@buf, 7, 1)
set @height = (256 * @hb) + @lb
set @hb = substring(@buf, 8, 1)
set @lb = substring(@buf, 9, 1)
set @width = (256 * @hb) + @lb
end else begin
set @hb = substring(@buf, 3, 1)
set @lb = substring(@buf, 4, 1)
set @fp = @fp + 2 + (256 * @hb) + @lb
end
end
end else begin
set @ext = 'nil'
end
end else begin
set @ext = 'nil'
end
end
end
set nocount off
if @ext = 'nil' begin
select null
end else begin
select @height, @width
end
end else begin
set nocount off
select null
end

Any help debugging this would be greatly appreciated.

Regards,

Graham

robvolk
Most Valuable Yak

15732 Posts

Posted - 2004-11-02 : 07:53:49
I don't know if this helps, I couldn't find a clear description of the width and height:

http://www.dcs.ed.ac.uk/home/mxr/gfx/2d-hi.html

Might help for future reference though. There's a PDF version of the JPEG/JFIF here:

http://www.w3.org/Graphics/JPEG/jfif3.pdf

But it's the same as the JPEG spec in the other link. So far this seems to be the best one:

http://www.funducode.com/freec/Fileformats/format3/format3b.htm

BTW, perhaps the image is corrupted? Have you tried exporting it to a file and opening the file?
Go to Top of Page

budgie
Starting Member

18 Posts

Posted - 2004-11-02 : 09:05:06
quote:
Originally posted by robvolk
BTW, perhaps the image is corrupted? Have you tried exporting it to a file and opening the file?


Those links do not tell me how to programatically get the image dimensions.

Yeas I am sure it isn't the image I have exported the image and I have another piece of code that does on the fly resizing which displays the image correctly. It is just this SP that incorrectly gets the sizes. Returns them as 1281x1!

Graham
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2004-11-02 : 12:57:40
Ummmm, no they don't. It's most likely because no one uses SQL to process image data (I know, you're stuck with it and can't change it, but them's the facts) However, they should at least give you some idea as to how to extract that information based on the offsets in the file header. You can at least match it to your existing code to validate that it's doing it correctly. It could be that the SQL code has always been wrong (offset by 1 byte, for example) but that no other image had dimensions that would cause a problem.
Go to Top of Page
   

- Advertisement -