# Bitmap image class (bmp) # Author : Sylvain Mareschal # License : GPL 3 http://www.gnu.org/licenses/gpl.html library("graphics") setGeneric("plot") setClass( Class = "bmp", representation = representation( fileName = "character", fileSize = "numeric", height = "numeric", width = "numeric", colorMode = "character", pixels = "array" ), validity = function(object) { returns = character(0) # Unique fields if (length(object@fileName) != 1 | is.na(object@fileName)) { returns = c(returns, "'fileName' must be single non-NA character") } if (length(object@fileSize) != 1 | is.na(object@fileSize)) { returns = c(returns, "'fileSize' must be single non-NA numeric") } if (length(object@height) != 1 | is.na(object@height)) { returns = c(returns, "'height' must be single non-NA numeric") } if (length(object@width) != 1 | is.na(object@width)) { returns = c(returns, "'width' must be single non-NA numeric") } if (length(object@colorMode) != 1 | is.na(object@colorMode)) { returns = c(returns, "'colorMode' must be single non-NA character") } # Restricted field if (!object@colorMode %in% c("L", "BGR", "BGRA")) { returns = c(returns, "Invalid 'colorMode'") } # Array if (!identical(dim(object@pixels), c(object@height, object@width, nchar(object@colorMode)))) { returns = c(returns, "Invalid 'pixels' dimensions") } return(returns) } ) setMethod( "show", signature(object="bmp"), definition = function(object) { if (object@colorMode == "L") { cat("\n BMP image (gray levels)\n") } else if (object@colorMode == "BGR") { cat("\n BMP image (no transparency)\n") } else if (object@colorMode == "BGRA") { cat("\n BMP image (with transparency)\n") } cat("\n@fileName ", object@fileName) cat("\n@fileSize ", object@fileSize, "octets") cat("\n@height ", object@height, "pixels") cat("\n@width ", object@width, "pixels") cat("\n@pixels array[ row , column , color ]") cat("\n\n") } ) setMethod( plot, signature(x="bmp"), definition = function(x) { # Device X11( width = x@width / max(x@width, x@height) * 7, height = x@height / max(x@width, x@height) * 7, title = x@fileName ) # Pixel colors if (x@colorMode == "L") { colorVector = rgb(x@pixels[,,"L"], x@pixels[,,"L"], x@pixels[,,"L"], maxColorValue=255) } else if (x@colorMode == "BGR") { colorVector = rgb(x@pixels[,,"R"], x@pixels[,,"G"], x@pixels[,,"B"], maxColorValue=255) } else if (x@colorMode == "BGRA") { colorVector = rgb(x@pixels[,,"R"], x@pixels[,,"G"], x@pixels[,,"B"], x@pixels[,,"A"], maxColorValue=255) } # Pixel positions yPositions = rep(1:x@height, times=x@width) xPositions = rep(1:x@width, each=x@height) # Background savePar = par(mar=rep(0,4), oma=rep(0,4), pty="m") on.exit(par(savePar)) plot( x = NA, y = NA, xlim = c(0.5, x@width+0.5), ylim = c(0.5, x@height+0.5), xlab = "", ylab = "", xaxt = "n", yaxt = "n", xaxs = "i", yaxs = "i", bty = "n" ) # Plot rect( xleft = xPositions - 0.5, xright = xPositions + 0.5, ybottom = yPositions - 0.5, ytop = yPositions + 0.5, col = colorVector, border = NA ) } )