Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Tidied up some of the code. Removed most, if not all dependency on Ut…

…il.whiley
  • Loading branch information...
commit ea602217d60832fd8cfa9561f9949b81e2d684fa 1 parent cb9a376
@LeeTrezise authored
View
113 sequential/imagelib/src/imagelib/bmp/BMP.whiley
@@ -6,39 +6,35 @@ import RGB from imagelib.core.Color
import imagelib.core.Image
public void ::write(Image img, string filename):
- debug "Writing BMP File: " + filename + "\n"
writer = File.Writer(filename)
//Write out Magic Header
- writer.write([01000010b, 01001101b])
- debug "Height: " + img.height + "\n"
- debug "Width: " + img.width + "\n"
- paddingVal = 3*img.width
- debug "Padding Value: " + paddingVal + "\n"
+ writer.write([01000010b, 01001101b]) // "BM"
+ paddingVal = 3*img.width //This is the width of a row in bytes.
+
+ //The total width of a row in the BMP format must be a multiple of Four.
blankBytes = paddingVal % 4
if blankBytes != 0:
blankBytes = 4 - blankBytes
- debug "Blank Bytes: " + blankBytes + "\n"
- debug "Padded up to: " + (paddingVal + blankBytes) + "\n"
size = 54 + 3*(img.height * img.width) + (blankBytes * img.height)
- debug "WRITING SIZE: " + size + "\n"
- writer.write(Util.padUnsignedInt(size,4))
- writer.write(Util.padUnsignedInt(0, 4))
- writer.write(Util.padUnsignedInt(54, 4))
+
+ writer.write(padUnsignedInt(size,4)) //Total Size of the BMP file, including header information
+ writer.write(padUnsignedInt(0, 4)) // Two reserved 2 byte blocks. The values can be used for whatever the creation software wants. Leave blank
+ writer.write(padUnsignedInt(54, 4)) // Offset of the Pixel array (Always 54, 14 byte file header, 40 byte Information Header)
//Finished Writing Data Header. Writing Info Header
- writer.write(Util.padUnsignedInt(40, 4)) // Header Size
- writer.write(Util.padUnsignedInt(img.width, 4)) //Width
- writer.write(Util.padUnsignedInt(img.height, 4)) //Height
- writer.write(Util.padUnsignedInt(1, 2)) //Color Planes (MUST BE ONE)
+ writer.write(padUnsignedInt(40, 4)) // Header Size
+ writer.write(padUnsignedInt(img.width, 4)) //Width
+ writer.write(padUnsignedInt(img.height, 4)) //Height
+ writer.write(padUnsignedInt(1, 2)) //Color Planes (MUST BE ONE)
- writer.write(Util.padUnsignedInt(24, 2)) // Bit Depth
- writer.write(Util.padUnsignedInt(0, 4)) // Compression Value
- writer.write(Util.padUnsignedInt(size - 54, 4)) // Size of raw Bitmap Data
- writer.write(Util.padUnsignedInt(2834, 4)) // Horizontal Resolution
- writer.write(Util.padUnsignedInt(2834, 4)) // Vertical Resolution
- writer.write(Util.padUnsignedInt(0, 4))
- writer.write(Util.padUnsignedInt(0, 4))
- debug "Size of Data: " + |img.data| + "\n"
+ writer.write(padUnsignedInt(24, 2)) // Bit Depth
+ writer.write(padUnsignedInt(0, 4)) // Compression Value
+ writer.write(padUnsignedInt(size - 54, 4)) // Size of raw Bitmap Data
+ writer.write(padUnsignedInt(2834, 4)) // Horizontal Resolution
+ writer.write(padUnsignedInt(2834, 4)) // Vertical Resolution
+ writer.write(padUnsignedInt(0, 4))
+ writer.write(padUnsignedInt(0, 4)) //Important Colours used. This is generally ignored
+
currWidth = 0
for col in img.data:
writer.write([Int.toUnsignedByte(col.b)])
@@ -48,7 +44,7 @@ public void ::write(Image img, string filename):
if currWidth == img.width:
currWidth = 0
if blankBytes != 0:
- writer.write(Util.padUnsignedInt(0, blankBytes))
+ writer.write(padUnsignedInt(0, blankBytes))
writer.close()
public int getBitDepth(Image img, [int] potential):
@@ -59,30 +55,37 @@ public int getBitDepth(Image img, [int] potential):
return i
return -1
+[byte] padUnsignedInt(int i, int padLength):
+ data = Int.toUnsignedBytes(i)
+
+ for j in |data|..padLength:
+ data = data + [00000000b]
+ return data
+
int getDistinctColors(Image img):
table = {}
for col in img.data:
table = table + {col}
return |table|
-public [[RGB]] ::readBMP(Reader file):
+public Image ::readBMP(Reader file):
debug "Reading Bitmap File\n"
- BMPSize = Byte.toUnsignedInt(file.read(4))
- //debug "BMP File Size: " + BMPSize + "\n"
- reservedBlockA = file.read(2)
- reservedBlockB = file.read(2)
+ size = Byte.toUnsignedInt(file.read(4))
+ file.read(4) // Skip the two application Reserved Blocks
pixelArrayOffset = Byte.toUnsignedInt(file.read(4))
- //debug "Pixel Array offset: " + pixelArrayOffset + "\n"
//Reading the Information Header
//This Contains Size, resolution, bpp, compression and color information
headerSize = Byte.toUnsignedInt(file.read(4))
- debug "Reading Header Size: " + headerSize + "\n"
- assert headerSize == 40
+ assert headerSize == 40 //No reason this would ever fail, except if corrupt
+
DIBInfo = file.read(36) // Read the rest of the 40 byte header, minus the information already read
- bitmapWidth = Byte.toInt(DIBInfo[0..3])
- bitmapHeight = Byte.toInt(DIBInfo[4..7])
+ width = Byte.toInt(DIBInfo[0..3])
+ height = Byte.toInt(DIBInfo[4..7])
colorPlanes = Byte.toUnsignedInt(DIBInfo[8..9])
+ assert colorPlanes == 1
+ //Not Doing Anything with this information
+
bitsPerPixel = Byte.toUnsignedInt(DIBInfo[10..11])
compressionMethod = Byte.toUnsignedInt(DIBInfo[12..15])
imageSize = Byte.toUnsignedInt(DIBInfo[16..19])
@@ -90,40 +93,26 @@ public [[RGB]] ::readBMP(Reader file):
verticalResolution = Byte.toInt(DIBInfo[24..27])
numColors = Byte.toUnsignedInt(DIBInfo[28..31])
importantColors = Byte.toUnsignedInt(DIBInfo[32..])
- //debug "Bitmap Width: " + bitmapWidth + "\n"
- //debug "Bitmap Height: " + bitmapHeight + "\n"
- //debug "Colour Planes: " + colorPlanes + "\n"
- //debug "Bits Per Pixel: " + bitsPerPixel + "\n"
- //debug "Compression Method: " + compressionMethod + "\n"
- //debug "imageSize: " + imageSize + "\n"
- ////debug "Horizontal Res: " + horizResolution + "\n"
- //debug "Vertical Res: " + verticalResolution + "\n"
- //debug "Number of Colours: " + numColors + "\n"
- //debug "Important Colours: " + importantColors + "\n" */
- dArray = []
+ data = []
if bitsPerPixel <= 8:
//Need to read in a Colour Table
else:
- paddingVal = 3*bitmapWidth
- width = 4
- //while width < paddingVal:
- // width = width *2
+ paddingVal = 3*width
blankBytes = paddingVal % 4
if blankBytes != 0:
blankBytes = 4 - blankBytes
- debug "Padding Val: " + paddingVal + "\n"
- debug "Blank Bytes: " + blankBytes + "\n"
- widthArray = []
- for i in 0..bitmapHeight:
- for j in 0..bitmapWidth:
- values={r:Byte.toUnsignedInt(file.read(1)),g:Byte.toUnsignedInt(file.read(1)), b:Byte.toUnsignedInt(file.read(1))}
- widthArray = widthArray + [values]
+ //Need to read in the data
+ dataSize = height*width
+ currWidth = 0
+ for i in 0..dataSize:
+ values={r:Byte.toUnsignedInt(file.read(1)),g:Byte.toUnsignedInt(file.read(1)), b:Byte.toUnsignedInt(file.read(1))}
//When we get here. There might need to be more padding.
- //debug "Width Array: " + widthArray + "\n"
- dArray = dArray+[widthArray]
- widthArray = []
- file.read(blankBytes)
+ data = data+[values]
+ currWidth = currWidth + 1
+ if currWidth == width:
+ file.read(blankBytes)
+ currWidth = 0
- return dArray
+ return Image.Image(width, height, data)
View
49 sequential/imagelib/src/imagelib/core/Color.whiley
@@ -7,21 +7,8 @@ define RGB as {int r, int g, int b}
public RGB RGB(int red, int green, int blue):
return {r: red, g: green, b: blue}
-
define HSB as {real h, real s, real b}
-real realMax(real a, real b):
- if a >= b:
- return a
- else:
- return b
-
-real realMin(real a, real b):
- if a <= b:
- return a
- else:
- return b
-
public RGB HSBtoRGB(HSB h):
n,d = h.h
hi = ((n/d)/60) % 6
@@ -37,28 +24,30 @@ public RGB HSBtoRGB(HSB h):
q = n*255 / d
n,d = t
t = n*255 / d
- if hi == 0:
- return {r:v, g:t, b:p}
- else if hi == 1:
- return {r:q, g:v, b:p}
- else if hi == 2:
- return {r:p, g:v, b:t}
- else if hi == 3:
- return {r:p, g:q, b:v}
- else if hi == 4:
- return {r:t, g:p, b:v}
- else if hi == 5:
- return {r:v, g:p, b:q}
- return {r:0, g:0, b:0}
+ switch hi:
+ case 0:
+ return {r:v, g:t, b:p}
+ case 1:
+ return {r:q, g:v, b:p}
+ case 2:
+ return {r:p, g:v, b:t}
+ case 3:
+ return {r:p, g:q, b:v}
+ case 4:
+ return {r:t, g:p, b:v}
+ case 5:
+ return {r:v, g:p, b:q}
+ default:
+ return {r:0, g:0, b:0}
public HSB RGBtoHSB(RGB a):
r = (real)a.r/255
g = (real)a.g/255
b = (real)a.b/255
- M = realMax(r, g)
- M = realMax(M, b)
- m = realMin(r, g)
- m = realMin(m, b)
+ M = Math.max(r, g)
+ M = Math.max(M, b)
+ m = Math.min(r, g)
+ m = Math.min(m, b)
chroma = M - m
H = 0.0
// Adjusted Values
Please sign in to comment.
Something went wrong with that request. Please try again.