template<bool avx, bool aligned> void typeToBGRA(const void* const src, void* const dest, const PixelTypes::PixelType type, const size_t size) { const __m_auto_i* const pSrc = reinterpret_cast<const __m_auto_i*>(src); const __m_auto_i* const srcEnd = reinterpret_cast<const __m_auto_i*>(src) + (size * PixelTypes::pixelSize(type)) / sizeof(__m_auto_i); __m_auto_i* pDest = reinterpret_cast<__m_auto_i*>(dest); switch(type) { case PixelTypes::PixelType::RGB: rgbToBGRA<avx, aligned>(pSrc, srcEnd, pDest); break; case PixelTypes::PixelType::YUV: yuvToBGRA<avx, aligned>(pSrc, srcEnd, pDest); break; case PixelTypes::PixelType::YUYV: yuyvToBGRA<avx, aligned>(pSrc, srcEnd, pDest); break; case PixelTypes::PixelType::Colored: coloredToBGRA<avx, aligned>(pSrc, srcEnd, pDest); break; case PixelTypes::PixelType::Grayscale: grayscaledToBGRA<avx, aligned>(pSrc, srcEnd, pDest); break; default: ASSERT(false); } size_t rest = (size * PixelTypes::pixelSize(type)) % sizeof(__m_auto_i); if(rest != 0) { PixelTypes::BGRAPixel* const ppDest = reinterpret_cast<PixelTypes::BGRAPixel*>(dest) + size - rest; switch(type) { case PixelTypes::PixelType::RGB: rgbToBGRA(reinterpret_cast<const PixelTypes::RGBPixel*>(src) + size - rest, reinterpret_cast<const PixelTypes::RGBPixel*>(src) + size, ppDest); break; case PixelTypes::PixelType::YUV: yuvToBGRA(reinterpret_cast<const PixelTypes::YUVPixel*>(src) + size - rest, reinterpret_cast<const PixelTypes::YUVPixel*>(src) + size, ppDest); break; case PixelTypes::PixelType::YUYV: yuyvToBGRA(reinterpret_cast<const PixelTypes::YUYVPixel*>(src) + size - rest, reinterpret_cast<const PixelTypes::YUYVPixel*>(src) + size, ppDest); break; case PixelTypes::PixelType::Colored: coloredToBGRA(reinterpret_cast<const PixelTypes::ColoredPixel*>(src) + size - rest, reinterpret_cast<const PixelTypes::ColoredPixel*>(src) + size, ppDest); break; case PixelTypes::PixelType::Grayscale: grayscaledToBGRA(reinterpret_cast<const PixelTypes::GrayscaledPixel*>(src) + size - rest, reinterpret_cast<const PixelTypes::GrayscaledPixel*>(src) + size, ppDest); break; default: ASSERT(false); } } }
static Bool readJPEGFileToImage (FILE *file, int *width, int *height, void **data) { struct jpeg_decompress_struct cinfo; struct jpegErrorMgr jerr; JSAMPLE *buf; JSAMPROW *rows; int i; Bool result; if (!file) return FALSE; cinfo.err = jpeg_std_error (&jerr.pub); jerr.pub.error_exit = jpegErrorExit; if (setjmp (jerr.setjmp_buffer)) { /* this is called on decompression errors */ jpeg_destroy_decompress (&cinfo); return FALSE; } jpeg_create_decompress (&cinfo); jpeg_stdio_src (&cinfo, file); jpeg_read_header (&cinfo, TRUE); cinfo.out_color_space = JCS_RGB; jpeg_start_decompress (&cinfo); *height = cinfo.output_height; *width = cinfo.output_width; buf = calloc (cinfo.output_height * cinfo.output_width * cinfo.output_components, sizeof (JSAMPLE)); if (!buf) { jpeg_finish_decompress (&cinfo); jpeg_destroy_decompress (&cinfo); return FALSE; } rows = malloc (cinfo.output_height * sizeof (JSAMPROW)); if (!rows) { free (buf); jpeg_finish_decompress (&cinfo); jpeg_destroy_decompress (&cinfo); return FALSE; } for (i = 0; i < cinfo.output_height; i++) rows[i] = &buf[i * cinfo.output_width * cinfo.output_components]; while (cinfo.output_scanline < cinfo.output_height) jpeg_read_scanlines (&cinfo, &rows[cinfo.output_scanline], cinfo.output_height - cinfo.output_scanline); jpeg_finish_decompress (&cinfo); jpeg_destroy_decompress (&cinfo); /* convert the rgb data into BGRA format */ result = rgbToBGRA (buf, data, *height, *width, 255); free (rows); free(buf); return result; }
template<bool avx> void typeToBGRA(const void* const src, void* const dest, const PixelTypes::PixelType type, const size_t size) { const __m_auto_i* const pSrc = reinterpret_cast<const __m_auto_i*>(src); const __m_auto_i* const srcEnd = reinterpret_cast<const __m_auto_i*>(src) + (size * PixelTypes::pixelSize(type)) / sizeof(__m_auto_i); __m_auto_i* pDest = reinterpret_cast<__m_auto_i*>(dest); switch(type) { case PixelTypes::PixelType::RGB: rgbToBGRA<avx>(pSrc, srcEnd, pDest); break; case PixelTypes::PixelType::YUV: yuvToBGRA<avx>(pSrc, srcEnd, pDest); break; case PixelTypes::PixelType::YUYV: yuyvToBGRA<avx>(pSrc, srcEnd, pDest); break; case PixelTypes::PixelType::Colored: coloredToBGRA<avx>(pSrc, srcEnd, pDest); break; case PixelTypes::PixelType::Grayscale: grayscaledToBGRA<avx>(pSrc, srcEnd, pDest); break; case PixelTypes::PixelType::Hue: hueToBGRA(reinterpret_cast<const PixelTypes::HuePixel*>(src), reinterpret_cast<const PixelTypes::HuePixel*>(srcEnd), reinterpret_cast<PixelTypes::BGRAPixel*>(dest)); break; case PixelTypes::PixelType::Edge2: edge2ToBGRA<avx>(pSrc, srcEnd, pDest, reinterpret_cast<const __m_auto_i*>(dest) + (size * PixelTypes::pixelSize(PixelTypes::BGRA)) / sizeof(__m_auto_i)); break;/* case PixelTypes::PixelType::Edge2MonoAvg: //edge2MonoAvgToBGRA<avx>(pSrc, srcEnd, pDest); break; case PixelTypes::PixelType::Edge2MonoAbsAvg: //edge2MonoAbsAvgBGRA<avx>(pSrc, srcEnd, pDest); break;*/ case PixelTypes::PixelType::Binary: binaryToBGRA<avx>(pSrc, srcEnd, pDest); break; default: FAIL("Unknown pixel type."); } size_t rest = (size * PixelTypes::pixelSize(type)) % sizeof(__m_auto_i); if(rest != 0) { PixelTypes::BGRAPixel* const ppDest = reinterpret_cast<PixelTypes::BGRAPixel*>(dest) + size - rest; switch(type) { case PixelTypes::PixelType::RGB: rgbToBGRA(reinterpret_cast<const PixelTypes::RGBPixel*>(src) + size - rest, reinterpret_cast<const PixelTypes::RGBPixel*>(src) + size, ppDest); break; case PixelTypes::PixelType::YUV: yuvToBGRA(reinterpret_cast<const PixelTypes::YUVPixel*>(src) + size - rest, reinterpret_cast<const PixelTypes::YUVPixel*>(src) + size, ppDest); break; case PixelTypes::PixelType::YUYV: yuyvToBGRA(reinterpret_cast<const PixelTypes::YUYVPixel*>(src) + size - rest, reinterpret_cast<const PixelTypes::YUYVPixel*>(src) + size, ppDest); break; case PixelTypes::PixelType::Colored: coloredToBGRA(reinterpret_cast<const PixelTypes::ColoredPixel*>(src) + size - rest, reinterpret_cast<const PixelTypes::ColoredPixel*>(src) + size, ppDest); break; case PixelTypes::PixelType::Grayscale: grayscaledToBGRA(reinterpret_cast<const PixelTypes::GrayscaledPixel*>(src) + size - rest, reinterpret_cast<const PixelTypes::GrayscaledPixel*>(src) + size, ppDest); break; case PixelTypes::PixelType::Hue: hueToBGRA(reinterpret_cast<const PixelTypes::HuePixel*>(src) + size - rest, reinterpret_cast<const PixelTypes::HuePixel*>(src) + size, ppDest); break; case PixelTypes::PixelType::Edge2: edge2ToBGRA(reinterpret_cast<const PixelTypes::Edge2Pixel*>(src) + size - rest, reinterpret_cast<const PixelTypes::Edge2Pixel*>(src) + size, ppDest, reinterpret_cast<PixelTypes::BGRAPixel*>(dest) + size); break; case PixelTypes::PixelType::Binary: binaryToBGRA(reinterpret_cast<const PixelTypes::BinaryPixel*>(src) + size - rest, reinterpret_cast<const PixelTypes::BinaryPixel*>(src) + size, ppDest); break; default: FAIL("Unknown pixel type."); } } }