-- | Create a thumbnail from a specific subregion of the image
mkThumbnail' :: ((Int,Int),(Int,Int)) -> L.ByteString -> IO (Either String Thumbnail)
mkThumbnail' sizeBounds = thumbnail . L.unpack
  where
    thumbnail ws | length ws >= 3 = thumbnail' ws -- FIXME!
                 | otherwise = return $ Left "unsupported image format"
    
    thumbnail' ws@(0xff:0xd8:_) = thumbnailJpeg ws
    thumbnail' ws@(0x89:0x50:_) = thumbnailPng ws
    thumbnail' ws@(0x47:0x49:0x46:_) = thumbnailGif ws
    thumbnail' _ = return $ Left "unsupported image format"
    
    thumbnailJpeg ws = do
      src <- loadJpegByteString $ BS.pack ws
      size <- imageSize src
      dest <- copyImage src
      let size' = newSize sizeBounds size
      thm <- uncurry resizeImage size' dest
      bs <- saveJpegByteString 90 thm
      let save fp = saveJpegFile 90 fp thm
      return $ Right Thumbnail { fmt=Jpeg
                               , img=thm
                               , sz=size'
                               , lbs=strictToLazy bs
                               , orgImg=src
                               , orgSZ=size
                               , saveFile=save
                               }
    
    thumbnailPng ws = do
      src <- loadPngByteString $ BS.pack ws
      size <- imageSize src
      dest <- copyImage src
      let size' = newSize sizeBounds size
      thm <- uncurry resizeImage size' dest
      bs <- savePngByteString thm
      let save fp = savePngFile fp thm
      return $ Right Thumbnail { fmt=Png
                               , img=thm
                               , sz=size'
                               , lbs=strictToLazy bs
                               , orgImg=src
                               , orgSZ=size
                               , saveFile=save
                               }
      
    thumbnailGif ws = do
      src <- loadGifByteString $ BS.pack ws
      size <- imageSize src
      dest <- copyImage src
      let size' = newSize sizeBounds size
      thm <- uncurry resizeImage size' dest
      bs <- saveGifByteString thm
      let save fp = saveGifFile fp thm
      return $ Right Thumbnail { fmt=Gif
                               , img=thm
                               , sz=size'
                               , lbs=strictToLazy bs
                               , orgImg=src
                               , orgSZ=size
                               , saveFile=save
                               }
        
    strictToLazy = L.pack . BS.unpack
mkThumbnail' :: ((Int,Int),(Int,Int)) -> L.ByteString -> IO (Either String Thumbnail)
mkThumbnail' sizeBounds = thumbnail . L.unpack
  where
    thumbnail ws | length ws >= 3 = thumbnail' ws -- FIXME!
                 | otherwise = return $ Left "unsupported image format"
    
    thumbnail' ws@(0xff:0xd8:_) = thumbnailJpeg ws
    thumbnail' ws@(0x89:0x50:_) = thumbnailPng ws
    thumbnail' ws@(0x47:0x49:0x46:_) = thumbnailGif ws
    thumbnail' _ = return $ Left "unsupported image format"
    
    thumbnailJpeg ws = do
      src <- loadJpegByteString $ BS.pack ws
      size <- imageSize src
      dest <- copyImage src
      let size' = newSize sizeBounds size
      thm <- uncurry resizeImage size' dest
      bs <- saveJpegByteString 90 thm
      let save fp = saveJpegFile 90 fp thm
      return $ Right Thumbnail { fmt=Jpeg
                               , img=thm
                               , sz=size'
                               , lbs=strictToLazy bs
                               , orgImg=src
                               , orgSZ=size
                               , saveFile=save
                               }
    
    thumbnailPng ws = do
      src <- loadPngByteString $ BS.pack ws
      size <- imageSize src
      dest <- copyImage src
      let size' = newSize sizeBounds size
      thm <- uncurry resizeImage size' dest
      bs <- savePngByteString thm
      let save fp = savePngFile fp thm
      return $ Right Thumbnail { fmt=Png
                               , img=thm
                               , sz=size'
                               , lbs=strictToLazy bs
                               , orgImg=src
                               , orgSZ=size
                               , saveFile=save
                               }
      
    thumbnailGif ws = do
      src <- loadGifByteString $ BS.pack ws
      size <- imageSize src
      dest <- copyImage src
      let size' = newSize sizeBounds size
      thm <- uncurry resizeImage size' dest
      bs <- saveGifByteString thm
      let save fp = saveGifFile fp thm
      return $ Right Thumbnail { fmt=Gif
                               , img=thm
                               , sz=size'
                               , lbs=strictToLazy bs
                               , orgImg=src
                               , orgSZ=size
                               , saveFile=save
                               }
        
    strictToLazy = L.pack . BS.unpack
mkThumbnail' sizeBounds = thumbnail . L.unpack
  where
    thumbnail ws | length ws >= 3 = thumbnail' ws -- FIXME!
                 | otherwise = return $ Left "unsupported image format"
    
    thumbnail' ws@(0xff:0xd8:_) = thumbnailJpeg ws
    thumbnail' ws@(0x89:0x50:_) = thumbnailPng ws
    thumbnail' ws@(0x47:0x49:0x46:_) = thumbnailGif ws
    thumbnail' _ = return $ Left "unsupported image format"
    
    thumbnailJpeg ws = do
      src <- loadJpegByteString $ BS.pack ws
      size <- imageSize src
      dest <- copyImage src
      let size' = newSize sizeBounds size
      thm <- uncurry resizeImage size' dest
      bs <- saveJpegByteString 90 thm
      let save fp = saveJpegFile 90 fp thm
      return $ Right Thumbnail { fmt=Jpeg
                               , img=thm
                               , sz=size'
                               , lbs=strictToLazy bs
                               , orgImg=src
                               , orgSZ=size
                               , saveFile=save
                               }
    
    thumbnailPng ws = do
      src <- loadPngByteString $ BS.pack ws
      size <- imageSize src
      dest <- copyImage src
      let size' = newSize sizeBounds size
      thm <- uncurry resizeImage size' dest
      bs <- savePngByteString thm
      let save fp = savePngFile fp thm
      return $ Right Thumbnail { fmt=Png
                               , img=thm
                               , sz=size'
                               , lbs=strictToLazy bs
                               , orgImg=src
                               , orgSZ=size
                               , saveFile=save
                               }
      
    thumbnailGif ws = do
      src <- loadGifByteString $ BS.pack ws
      size <- imageSize src
      dest <- copyImage src
      let size' = newSize sizeBounds size
      thm <- uncurry resizeImage size' dest
      bs <- saveGifByteString thm
      let save fp = saveGifFile fp thm
      return $ Right Thumbnail { fmt=Gif
                               , img=thm
                               , sz=size'
                               , lbs=strictToLazy bs
                               , orgImg=src
                               , orgSZ=size
                               , saveFile=save
                               }
        
    strictToLazy = L.pack . BS.unpack