-
Notifications
You must be signed in to change notification settings - Fork 0
/
jpegHedder.hs
54 lines (47 loc) · 1.82 KB
/
jpegHedder.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import System.IO
import Numeric (showHex)
import Data.ByteString as B (ByteString, hGetContents, unpack)
import GHC.Word (Word8)
import Data.List
import Data.Char
hex :: Word8 -> String
hex x | (x < 15) = "0" ++ (showHex (x) "")
| otherwise = showHex (x) ""
hex2dec :: [Char] -> Int
hex2dec (x:xs) = (digitToInt x)*(16^(length xs)) + hex2dec(xs)
hex2dec x = 0
markerDiv :: [String] -> [String]
markerDiv (x:xs) | x == "ff" = ("\n" ++ x ) : markerDiv(xs)
| otherwise = x :markerDiv(xs)
markerDiv x = []
markerFilter :: String -> [String] -> [String]
markerFilter _ [] = []
markerFilter x (y:ys) = if isPrefixOf x y then y :markerFilter x ys
else markerFilter x ys
byteLoder :: Int -> String -> String
byteLoder x y = [y !! (x*2) ] ++ [y !! ((x*2)+1)]
main :: IO ()
main = do
targetFile <- openFile "test.jpg" ReadMode
binaryData <- B.hGetContents targetFile
let binaryList = map hex (B.unpack binaryData)
markerDivList = tail $ lines $ concat $ markerDiv binaryList
exifData = markerFilter "ffe" markerDivList
quantizationTable = markerFilter "ffdb" markerDivList
metaData = markerFilter "ffc0" markerDivList
huffmanTable = markerFilter "ffc4" markerDivList
--print quantizationTable
--print exifData
--print metaData
--print huffmanTable
putStr "width:"
let metaData' = concat metaData
let imgwidth = byteLoder 5 metaData' ++ byteLoder 6 metaData'
print(hex2dec imgwidth)
putStr "length:"
let imglength = byteLoder 7 metaData' ++ byteLoder 8 metaData'
print(hex2dec imglength)
--putStrLn $ concat $ map (++['\n']) (take 9 markerDivList)
--saveFile <- openFile "test.txt" WriteMode
--hPutStr saveFile (concat$markerDiv binaryList)
--print binaryList