def identifyColourModel()

in common-lib/src/main/scala/com/gu/mediaservice/lib/imaging/ImageOperations.scala [256:313]


  def identifyColourModel(sourceFile: File, mimeType: MimeType)(implicit ec: ExecutionContext, logMarker: LogMarker): Future[Option[String]] = {
    // TODO: use mimeType to lookup other properties once we support other formats

    mimeType match {
      case Jpeg =>
        val source = addImage(sourceFile)
        val formatter = format(source)("%[JPEG-Colorspace-Name]")

        for {
          output <- runIdentifyCmd(formatter, false)
          colourModel = output.headOption
        } yield colourModel match {
          case Some("GRAYSCALE") => Some("Greyscale")
          case Some("CMYK") => Some("CMYK")
          case _ => Some("RGB")
        }
      case Tiff =>
        val op = new IMOperation()
        val formatter = format(op)("%[colorspace]")
        val withSource = addDestImage(formatter)(sourceFile)

        for {
          output <- runIdentifyCmd(withSource, true)
          colourModel = output.headOption
        } yield colourModel match {
          case Some("sRGB") => Some("RGB")
          case Some("Gray") => Some("Greyscale")
          case Some("CIELab") => Some("LAB")
          // IM returns doubles for TIFFs with transparency…
          case Some("sRGBsRGB") => Some("RGB")
          case Some("GrayGray") => Some("Greyscale")
          case Some("CIELabCIELab") => Some("LAB")
          case Some("CMYKCMYK") => Some("CMYK")
          // …and triples for TIFFs with transparency and alpha channel(s). I think.
          case Some("sRGBsRGBsRGB") => Some("RGB")
          case Some("GrayGrayGray") => Some("Greyscale")
          case Some("CIELabCIELabCIELab") => Some("LAB")
          case Some("CMYKCMYKCMYK") => Some("CMYK")
          case _ => colourModel
        }
      case Png =>
        val op = new IMOperation()
        val formatter = format(op)("%[colorspace]")
        val withSource = addDestImage(formatter)(sourceFile)

        for {
          output <- runIdentifyCmd(withSource, true)
          colourModel = output.headOption
        } yield colourModel match {
          case Some("sRGB") => Some("RGB")
          case Some("Gray") => Some("Greyscale")
          case _ => Some("RGB")
        }
      case _ =>
        // assume that the colour model is RGB for other image types
        Future.successful(Some("RGB"))
    }
  }