12-bit JPEG support


git-svn-id: svn+ssh://svn.code.sf.net/p/libjpeg-turbo/code/trunk@1337 632fc199-4ca6-4c93-a231-07263d6284db
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 42cb70a..c34f45e 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -50,7 +50,20 @@
 option(WITH_JPEG7 "Emulate libjpeg v7 API/ABI (this makes libjpeg-turbo backward incompatible with libjpeg v6b)" FALSE)
 option(WITH_JPEG8 "Emulate libjpeg v8 API/ABI (this makes libjpeg-turbo backward incompatible with libjpeg v6b)" FALSE)
 option(WITH_MEM_SRCDST "Include in-memory source/destination manager functions when emulating the libjpeg v6b or v7 API/ABI" TRUE)
+option(WITH_TURBOJPEG "Include the TurboJPEG wrapper library and associated test programs" TRUE)
 option(WITH_JAVA "Build Java wrapper for the TurboJPEG library" FALSE)
+option(WITH_12BIT "Encode/decode JPEG images with 12-bit samples (implies WITH_SIMD=0 WITH_TURBOJPEG=0 WITH_ARITH_ENC=0 WITH_ARITH_DEC=0)" FALSE)
+
+if(WITH_12BIT)
+  set(WITH_SIMD FALSE)
+  set(WITH_TURBOJPEG FALSE)
+  set(WITH_ARITH_ENC FALSE)
+  set(WITH_ARITH_DEC FALSE)
+  set(BITS_IN_JSAMPLE 12)
+  message(STATUS "12-bit JPEG support enabled")
+else()
+  set(BITS_IN_JSAMPLE 8)
+endif()
 
 if(WITH_ARITH_ENC)
   set(C_ARITH_CODING_SUPPORTED 1)
@@ -66,6 +79,12 @@
   message(STATUS "Arithmetic decoding support disabled")
 endif()
 
+if(WITH_TURBOJPEG)
+  message(STATUS "TurboJPEG C wrapper enabled")
+else()
+  message(STATUS "TurboJPEG C wrapper disabled")
+endif()
+
 if(WITH_JAVA)
   message(STATUS "TurboJPEG Java wrapper enabled")
 else()
@@ -209,57 +228,65 @@
   add_dependencies(jpeg-static simd)
 endif()
 
-set(TURBOJPEG_SOURCES turbojpeg.c transupp.c jdatadst-tj.c jdatasrc-tj.c)
-if(WITH_JAVA)
-  set(TURBOJPEG_SOURCES ${TURBOJPEG_SOURCES} turbojpeg-jni.c)
-  include_directories(${JAVA_INCLUDE_PATH} ${JAVA_INCLUDE_PATH2})
+if(WITH_TURBOJPEG)
+  set(TURBOJPEG_SOURCES turbojpeg.c transupp.c jdatadst-tj.c jdatasrc-tj.c)
+  if(WITH_JAVA)
+    set(TURBOJPEG_SOURCES ${TURBOJPEG_SOURCES} turbojpeg-jni.c)
+    include_directories(${JAVA_INCLUDE_PATH} ${JAVA_INCLUDE_PATH2})
+  endif()
+
+  add_library(turbojpeg SHARED ${TURBOJPEG_SOURCES})
+  set_target_properties(turbojpeg PROPERTIES DEFINE_SYMBOL DLLDEFINE)
+  if(MINGW)
+    set_target_properties(turbojpeg PROPERTIES LINK_FLAGS -Wl,--kill-at)
+  endif()
+  target_link_libraries(turbojpeg jpeg-static)
+  set_target_properties(turbojpeg PROPERTIES LINK_INTERFACE_LIBRARIES "")
+
+  add_library(turbojpeg-static STATIC ${JPEG_SOURCES} ${SIMD_OBJS}
+    turbojpeg.c transupp.c jdatadst-tj.c jdatasrc-tj.c)
+  if(NOT MSVC)
+    set_target_properties(turbojpeg-static PROPERTIES OUTPUT_NAME turbojpeg)
+  endif()
+  if(WITH_SIMD)
+    add_dependencies(turbojpeg-static simd)
+  endif()
+
+  add_executable(tjunittest tjunittest.c tjutil.c)
+  target_link_libraries(tjunittest turbojpeg)
+
+  add_executable(tjunittest-static tjunittest.c tjutil.c)
+  target_link_libraries(tjunittest-static turbojpeg-static)
+
+  add_executable(tjbench tjbench.c bmp.c tjutil.c rdbmp.c rdppm.c wrbmp.c
+    wrppm.c)
+  target_link_libraries(tjbench turbojpeg jpeg-static)
+  set_property(TARGET tjbench PROPERTY COMPILE_FLAGS
+    "-DBMP_SUPPORTED -DPPM_SUPPORTED")
+
+  add_executable(tjbench-static tjbench.c bmp.c tjutil.c rdbmp.c rdppm.c wrbmp.c
+    wrppm.c)
+  target_link_libraries(tjbench-static turbojpeg-static jpeg-static)
+  set_property(TARGET tjbench-static PROPERTY COMPILE_FLAGS
+    "-DBMP_SUPPORTED -DPPM_SUPPORTED")
 endif()
 
-add_library(turbojpeg SHARED ${TURBOJPEG_SOURCES})
-set_target_properties(turbojpeg PROPERTIES DEFINE_SYMBOL DLLDEFINE)
-if(MINGW)
-  set_target_properties(turbojpeg PROPERTIES LINK_FLAGS -Wl,--kill-at)
-endif()
-target_link_libraries(turbojpeg jpeg-static)
-set_target_properties(turbojpeg PROPERTIES LINK_INTERFACE_LIBRARIES "")
-
-add_library(turbojpeg-static STATIC ${JPEG_SOURCES} ${SIMD_OBJS}
-  turbojpeg.c transupp.c jdatadst-tj.c jdatasrc-tj.c)
-if(NOT MSVC)
-  set_target_properties(turbojpeg-static PROPERTIES OUTPUT_NAME turbojpeg)
-endif()
-if(WITH_SIMD)
-  add_dependencies(turbojpeg-static simd)
+if(WITH_12BIT)
+  set(COMPILE_FLAGS "-DGIF_SUPPORTED -DPPM_SUPPORTED -DUSE_SETMODE")
+else()
+  set(COMPILE_FLAGS "-DBMP_SUPPORTED -DGIF_SUPPORTED -DPPM_SUPPORTED -DTARGA_SUPPORTED -DUSE_SETMODE")
+	set(CJPEG_BMP_SOURCES rdbmp.c rdtarga.c)
+	set(DJPEG_BMP_SOURCES wrbmp.c wrtarga.c)
 endif()
 
-add_executable(tjunittest tjunittest.c tjutil.c)
-target_link_libraries(tjunittest turbojpeg)
-
-add_executable(tjunittest-static tjunittest.c tjutil.c)
-target_link_libraries(tjunittest-static turbojpeg-static)
-
-add_executable(tjbench tjbench.c bmp.c tjutil.c rdbmp.c rdppm.c wrbmp.c
-  wrppm.c)
-target_link_libraries(tjbench turbojpeg jpeg-static)
-set_property(TARGET tjbench PROPERTY COMPILE_FLAGS
-  "-DBMP_SUPPORTED -DPPM_SUPPORTED")
-
-add_executable(tjbench-static tjbench.c bmp.c tjutil.c rdbmp.c rdppm.c wrbmp.c
-  wrppm.c)
-target_link_libraries(tjbench-static turbojpeg-static jpeg-static)
-set_property(TARGET tjbench-static PROPERTY COMPILE_FLAGS
-  "-DBMP_SUPPORTED -DPPM_SUPPORTED")
-
-add_executable(cjpeg-static cjpeg.c cdjpeg.c rdbmp.c rdgif.c rdppm.c rdswitch.c
-  rdtarga.c)
-set_property(TARGET cjpeg-static PROPERTY COMPILE_FLAGS
-  "-DBMP_SUPPORTED -DGIF_SUPPORTED -DPPM_SUPPORTED -DTARGA_SUPPORTED -DUSE_SETMODE")
+add_executable(cjpeg-static cjpeg.c cdjpeg.c rdgif.c rdppm.c rdswitch.c
+  ${CJPEG_BMP_SOURCES})
+set_property(TARGET cjpeg-static PROPERTY COMPILE_FLAGS ${COMPILE_FLAGS})
 target_link_libraries(cjpeg-static jpeg-static)
 
-add_executable(djpeg-static djpeg.c cdjpeg.c rdcolmap.c rdswitch.c wrbmp.c wrgif.c
-  wrppm.c wrtarga.c)
-set_property(TARGET djpeg-static PROPERTY COMPILE_FLAGS
-  "-DBMP_SUPPORTED -DGIF_SUPPORTED -DPPM_SUPPORTED -DTARGA_SUPPORTED -DUSE_SETMODE")
+add_executable(djpeg-static djpeg.c cdjpeg.c rdcolmap.c rdswitch.c wrgif.c
+  wrppm.c ${DJPEG_BMP_SOURCES})
+set_property(TARGET djpeg-static PROPERTY COMPILE_FLAGS ${COMPILE_FLAGS})
 target_link_libraries(djpeg-static jpeg-static)
 
 add_executable(jpegtran-static jpegtran.c cdjpeg.c rdswitch.c transupp.c)
@@ -283,53 +310,85 @@
 
 enable_testing()
 
-set(MD5_JPEG_RGB_ISLOW 768e970dd57b340ff1b83c9d3d47c77b)
-set(MD5_PPM_RGB_ISLOW 00a257f5393fef8821f2b88ac7421291)
-set(MD5_BMP_RGB_ISLOW_565 f07d2e75073e4bb10f6c6f4d36e2e3be)
-set(MD5_BMP_RGB_ISLOW_565D 4cfa0928ef3e6bb626d7728c924cfda4)
-set(MD5_JPEG_422_IFAST_OPT 2540287b79d913f91665e660303ab2c8)
-set(MD5_PPM_422_IFAST 35bd6b3f833bad23de82acea847129fa)
-set(MD5_PPM_422M_IFAST 8dbc65323d62cca7c91ba02dd1cfa81d)
-set(MD5_BMP_422M_IFAST_565 3294bd4d9a1f2b3d08ea6020d0db7065)
-set(MD5_BMP_422M_IFAST_565D da98c9c7b6039511be4a79a878a9abc1)
-set(MD5_JPEG_420_IFAST_Q100_PROG 990cbe0329c882420a2094da7e5adade)
-set(MD5_PPM_420_Q100_IFAST 5a732542015c278ff43635e473a8a294)
-set(MD5_PPM_420M_Q100_IFAST ff692ee9323a3b424894862557c092f1)
-set(MD5_JPEG_GRAY_ISLOW 72b51f894b8f4a10b3ee3066770aa38d)
-set(MD5_PPM_GRAY_ISLOW 8d3596c56eace32f205deccc229aa5ed)
-set(MD5_PPM_GRAY_ISLOW_RGB 116424ac07b79e5e801f00508eab48ec)
-set(MD5_BMP_GRAY_ISLOW_565 12f78118e56a2f48b966f792fedf23cc)
-set(MD5_BMP_GRAY_ISLOW_565D bdbbd616441a24354c98553df5dc82db)
-set(MD5_JPEG_420S_IFAST_OPT 388708217ac46273ca33086b22827ed8)
-if(WITH_SIMD)
-set(MD5_JPEG_3x2_FLOAT_PROG 343e3f8caf8af5986ebaf0bdc13b5c71)
-set(MD5_PPM_3x2_FLOAT 1a75f36e5904d6fc3a85a43da9ad89bb)
+if(WITH_12BIT)
+  set(TESTORIG testorig12.jpg)
+  set(MD5_JPEG_RGB_ISLOW 9620f424569594bb9242b48498ad801f)
+  set(MD5_PPM_RGB_ISLOW f3301d2219783b8b3d942b7239fa50c0)
+  set(MD5_JPEG_422_IFAST_OPT 7322e3bd2f127f7de4b40d4480ce60e4)
+  set(MD5_PPM_422_IFAST 79807fa552899e66a04708f533e16950)
+  set(MD5_PPM_422M_IFAST 07737bfe8a7c1c87aaa393a0098d16b0)
+  set(MD5_JPEG_420_IFAST_Q100_PROG a1da220b5604081863a504297ed59e55)
+  set(MD5_PPM_420_Q100_IFAST 1b3730122709f53d007255e8dfd3305e)
+  set(MD5_PPM_420M_Q100_IFAST 980a1a3c5bf9510022869d30b7d26566)
+  set(MD5_JPEG_GRAY_ISLOW 235c90707b16e2e069f37c888b2636d9)
+  set(MD5_PPM_GRAY_ISLOW 7213c10af507ad467da5578ca5ee1fca)
+  set(MD5_PPM_GRAY_ISLOW_RGB e96ee81c30a6ed422d466338bd3de65d)
+  set(MD5_JPEG_420S_IFAST_OPT 7af8e60be4d9c227ec63ac9b6630855e)
+  set(MD5_JPEG_3x2_FLOAT_PROG a8c17daf77b457725ec929e215b603f8)
+  set(MD5_PPM_3x2_FLOAT 42876ab9e5c2f76a87d08db5fbd57956)
+  set(MD5_PPM_420M_ISLOW_2_1 4ca6be2a6f326ff9eaab63e70a8259c0)
+  set(MD5_PPM_420M_ISLOW_15_8 12aa9f9534c1b3d7ba047322226365eb)
+  set(MD5_PPM_420M_ISLOW_13_8 f7e22817c7b25e1393e4ec101e9d4e96)
+  set(MD5_PPM_420M_ISLOW_11_8 800a16f9f4dc9b293197bfe11be10a82)
+  set(MD5_PPM_420M_ISLOW_9_8 06b7a92a9bc69f4dc36ec40f1937d55c)
+  set(MD5_PPM_420M_ISLOW_7_8 3ec444a14a4ab4eab88ffc49c48eca43)
+  set(MD5_PPM_420M_ISLOW_3_4 3e726b7ea872445b19437d1c1d4f0d93)
+  set(MD5_PPM_420M_ISLOW_5_8 a8a771abdc94301d20ffac119b2caccd)
+  set(MD5_PPM_420M_ISLOW_1_2 b419124dd5568b085787234866102866)
+  set(MD5_PPM_420M_ISLOW_3_8 343d19015531b7bbe746124127244fa8)
+  set(MD5_PPM_420M_ISLOW_1_4 35fd59d866e44659edfa3c18db2a3edb)
+  set(MD5_PPM_420M_ISLOW_1_8 ccaed48ac0aedefda5d4abe4013f4ad7)
+  set(MD5_JPEG_CROP cdb35ff4b4519392690ea040c56ea99c)
 else()
-set(MD5_JPEG_3x2_FLOAT_PROG 9bca803d2042bd1eb03819e2bf92b3e5)
-set(MD5_PPM_3x2_FLOAT f6bfab038438ed8f5522fbd33595dcdc)
+  set(TESTORIG testorig.jpg)
+  set(MD5_JPEG_RGB_ISLOW 768e970dd57b340ff1b83c9d3d47c77b)
+  set(MD5_PPM_RGB_ISLOW 00a257f5393fef8821f2b88ac7421291)
+  set(MD5_BMP_RGB_ISLOW_565 f07d2e75073e4bb10f6c6f4d36e2e3be)
+  set(MD5_BMP_RGB_ISLOW_565D 4cfa0928ef3e6bb626d7728c924cfda4)
+  set(MD5_JPEG_422_IFAST_OPT 2540287b79d913f91665e660303ab2c8)
+  set(MD5_PPM_422_IFAST 35bd6b3f833bad23de82acea847129fa)
+  set(MD5_PPM_422M_IFAST 8dbc65323d62cca7c91ba02dd1cfa81d)
+  set(MD5_BMP_422M_IFAST_565 3294bd4d9a1f2b3d08ea6020d0db7065)
+  set(MD5_BMP_422M_IFAST_565D da98c9c7b6039511be4a79a878a9abc1)
+  set(MD5_JPEG_420_IFAST_Q100_PROG 990cbe0329c882420a2094da7e5adade)
+  set(MD5_PPM_420_Q100_IFAST 5a732542015c278ff43635e473a8a294)
+  set(MD5_PPM_420M_Q100_IFAST ff692ee9323a3b424894862557c092f1)
+  set(MD5_JPEG_GRAY_ISLOW 72b51f894b8f4a10b3ee3066770aa38d)
+  set(MD5_PPM_GRAY_ISLOW 8d3596c56eace32f205deccc229aa5ed)
+  set(MD5_PPM_GRAY_ISLOW_RGB 116424ac07b79e5e801f00508eab48ec)
+  set(MD5_BMP_GRAY_ISLOW_565 12f78118e56a2f48b966f792fedf23cc)
+  set(MD5_BMP_GRAY_ISLOW_565D bdbbd616441a24354c98553df5dc82db)
+  set(MD5_JPEG_420S_IFAST_OPT 388708217ac46273ca33086b22827ed8)
+  if(WITH_SIMD)
+    set(MD5_JPEG_3x2_FLOAT_PROG 343e3f8caf8af5986ebaf0bdc13b5c71)
+    set(MD5_PPM_3x2_FLOAT 1a75f36e5904d6fc3a85a43da9ad89bb)
+  else()
+    set(MD5_JPEG_3x2_FLOAT_PROG 9bca803d2042bd1eb03819e2bf92b3e5)
+    set(MD5_PPM_3x2_FLOAT f6bfab038438ed8f5522fbd33595dcdc)
+  endif()
+  set(MD5_JPEG_420_ISLOW_ARI e986fb0a637a8d833d96e8a6d6d84ea1)
+  set(MD5_JPEG_444_ISLOW_PROGARI 0a8f1c8f66e113c3cf635df0a475a617)
+  set(MD5_PPM_420M_IFAST_ARI 72b59a99bcf1de24c5b27d151bde2437)
+  set(MD5_JPEG_420_ISLOW 9a68f56bc76e466aa7e52f415d0f4a5f)
+  set(MD5_PPM_420M_ISLOW_2_1 9f9de8c0612f8d06869b960b05abf9c9)
+  set(MD5_PPM_420M_ISLOW_15_8 b6875bc070720b899566cc06459b63b7)
+  set(MD5_PPM_420M_ISLOW_13_8 bc3452573c8152f6ae552939ee19f82f)
+  set(MD5_PPM_420M_ISLOW_11_8 d8cc73c0aaacd4556569b59437ba00a5)
+  set(MD5_PPM_420M_ISLOW_9_8 d25e61bc7eac0002f5b393aa223747b6)
+  set(MD5_PPM_420M_ISLOW_7_8 ddb564b7c74a09494016d6cd7502a946)
+  set(MD5_PPM_420M_ISLOW_3_4 8ed8e68808c3fbc4ea764fc9d2968646)
+  set(MD5_PPM_420M_ISLOW_5_8 a3363274999da2366a024efae6d16c9b)
+  set(MD5_PPM_420M_ISLOW_1_2 e692a315cea26b988c8e8b29a5dbcd81)
+  set(MD5_PPM_420M_ISLOW_3_8 79eca9175652ced755155c90e785a996)
+  set(MD5_PPM_420M_ISLOW_1_4 79cd778f8bf1a117690052cacdd54eca)
+  set(MD5_PPM_420M_ISLOW_1_8 391b3d4aca640c8567d6f8745eb2142f)
+  set(MD5_BMP_420_ISLOW_256 4980185e3776e89bd931736e1cddeee6)
+  set(MD5_BMP_420_ISLOW_565 bf9d13e16c4923b92e1faa604d7922cb)
+  set(MD5_BMP_420_ISLOW_565D 6bde71526acc44bcff76f696df8638d2)
+  set(MD5_BMP_420M_ISLOW_565 8dc0185245353cfa32ad97027342216f)
+  set(MD5_BMP_420M_ISLOW_565D d1be3a3339166255e76fa50a0d70d73e)
+  set(MD5_JPEG_CROP b4197f377e621c4e9b1d20471432610d)
 endif()
-set(MD5_JPEG_420_ISLOW_ARI e986fb0a637a8d833d96e8a6d6d84ea1)
-set(MD5_JPEG_444_ISLOW_PROGARI 0a8f1c8f66e113c3cf635df0a475a617)
-set(MD5_PPM_420M_IFAST_ARI 72b59a99bcf1de24c5b27d151bde2437)
-set(MD5_JPEG_420_ISLOW 9a68f56bc76e466aa7e52f415d0f4a5f)
-set(MD5_PPM_420M_ISLOW_2_1 9f9de8c0612f8d06869b960b05abf9c9)
-set(MD5_PPM_420M_ISLOW_15_8 b6875bc070720b899566cc06459b63b7)
-set(MD5_PPM_420M_ISLOW_13_8 bc3452573c8152f6ae552939ee19f82f)
-set(MD5_PPM_420M_ISLOW_11_8 d8cc73c0aaacd4556569b59437ba00a5)
-set(MD5_PPM_420M_ISLOW_9_8 d25e61bc7eac0002f5b393aa223747b6)
-set(MD5_PPM_420M_ISLOW_7_8 ddb564b7c74a09494016d6cd7502a946)
-set(MD5_PPM_420M_ISLOW_3_4 8ed8e68808c3fbc4ea764fc9d2968646)
-set(MD5_PPM_420M_ISLOW_5_8 a3363274999da2366a024efae6d16c9b)
-set(MD5_PPM_420M_ISLOW_1_2 e692a315cea26b988c8e8b29a5dbcd81)
-set(MD5_PPM_420M_ISLOW_3_8 79eca9175652ced755155c90e785a996)
-set(MD5_PPM_420M_ISLOW_1_4 79cd778f8bf1a117690052cacdd54eca)
-set(MD5_PPM_420M_ISLOW_1_8 391b3d4aca640c8567d6f8745eb2142f)
-set(MD5_BMP_420_ISLOW_256 4980185e3776e89bd931736e1cddeee6)
-set(MD5_BMP_420_ISLOW_565 bf9d13e16c4923b92e1faa604d7922cb)
-set(MD5_BMP_420_ISLOW_565D 6bde71526acc44bcff76f696df8638d2)
-set(MD5_BMP_420M_ISLOW_565 8dc0185245353cfa32ad97027342216f)
-set(MD5_BMP_420M_ISLOW_565D d1be3a3339166255e76fa50a0d70d73e)
-set(MD5_JPEG_CROP b4197f377e621c4e9b1d20471432610d)
 
 if(WITH_JAVA)
   add_test(TJUnitTest
@@ -365,11 +424,13 @@
     set(dir "")
     set(suffix -static)
   endif()
-  add_test(tjunittest${suffix} tjunittest${suffix})
-  add_test(tjunittest${suffix}-alloc tjunittest${suffix} -alloc)
-  add_test(tjunittest${suffix}-yuv tjunittest${suffix} -yuv)
-  add_test(tjunittest${suffix}-yuv-alloc tjunittest${suffix} -yuv -alloc)
-  add_test(tjunittest${suffix}-yuv-nopad tjunittest${suffix} -yuv -noyuvpad)
+  if(WITH_TURBOJPEG)
+    add_test(tjunittest${suffix} tjunittest${suffix})
+    add_test(tjunittest${suffix}-alloc tjunittest${suffix} -alloc)
+    add_test(tjunittest${suffix}-yuv tjunittest${suffix} -yuv)
+    add_test(tjunittest${suffix}-yuv-alloc tjunittest${suffix} -yuv -alloc)
+    add_test(tjunittest${suffix}-yuv-nopad tjunittest${suffix} -yuv -noyuvpad)
+  endif()
 
   # These tests are carefully chosen to provide full coverage of as many of the
   # underlying algorithms as possible (including all of the SIMD-accelerated
@@ -389,22 +450,24 @@
   add_test(djpeg${suffix}-rgb-islow-cmp
     ${CMAKE_COMMAND} -DMD5=${MD5_PPM_RGB_ISLOW} -DFILE=testout_rgb_islow.ppm
       -P ${CMAKE_SOURCE_DIR}/cmakescripts/md5cmp.cmake)
-  # CC: RGB->RGB565  SAMP: fullsize  IDCT: islow  ENT: huff
-  add_test(djpeg${suffix}-rgb-islow-565
-    ${dir}djpeg${suffix} -dct int -rgb565 -dither none -bmp
-      -outfile testout_rgb_islow_565.bmp testout_rgb_islow.jpg)
-  add_test(djpeg${suffix}-rgb-islow-565-cmp
-    ${CMAKE_COMMAND} -DMD5=${MD5_BMP_RGB_ISLOW_565}
-      -DFILE=testout_rgb_islow_565.bmp
-      -P ${CMAKE_SOURCE_DIR}/cmakescripts/md5cmp.cmake)
-  # CC: RGB->RGB565 (dithered)  SAMP: fullsize  IDCT: islow  ENT: huff
-  add_test(djpeg${suffix}-rgb-islow-565D
-    ${dir}djpeg${suffix} -dct int -rgb565 -bmp
-      -outfile testout_rgb_islow_565D.bmp testout_rgb_islow.jpg)
-  add_test(djpeg${suffix}-rgb-islow-565D-cmp
-    ${CMAKE_COMMAND} -DMD5=${MD5_BMP_RGB_ISLOW_565D}
-      -DFILE=testout_rgb_islow_565D.bmp
-      -P ${CMAKE_SOURCE_DIR}/cmakescripts/md5cmp.cmake)
+  if(NOT WITH_12BIT)
+    # CC: RGB->RGB565  SAMP: fullsize  IDCT: islow  ENT: huff
+    add_test(djpeg${suffix}-rgb-islow-565
+      ${dir}djpeg${suffix} -dct int -rgb565 -dither none -bmp
+        -outfile testout_rgb_islow_565.bmp testout_rgb_islow.jpg)
+    add_test(djpeg${suffix}-rgb-islow-565-cmp
+      ${CMAKE_COMMAND} -DMD5=${MD5_BMP_RGB_ISLOW_565}
+        -DFILE=testout_rgb_islow_565.bmp
+        -P ${CMAKE_SOURCE_DIR}/cmakescripts/md5cmp.cmake)
+    # CC: RGB->RGB565 (dithered)  SAMP: fullsize  IDCT: islow  ENT: huff
+    add_test(djpeg${suffix}-rgb-islow-565D
+      ${dir}djpeg${suffix} -dct int -rgb565 -bmp
+        -outfile testout_rgb_islow_565D.bmp testout_rgb_islow.jpg)
+    add_test(djpeg${suffix}-rgb-islow-565D-cmp
+      ${CMAKE_COMMAND} -DMD5=${MD5_BMP_RGB_ISLOW_565D}
+        -DFILE=testout_rgb_islow_565D.bmp
+        -P ${CMAKE_SOURCE_DIR}/cmakescripts/md5cmp.cmake)
+  endif()
 
   # CC: RGB->YCC  SAMP: fullsize/h2v1  FDCT: ifast  ENT: 2-pass huff
   add_test(cjpeg${suffix}-422-ifast-opt
@@ -429,22 +492,24 @@
   add_test(djpeg${suffix}-422m-ifast-cmp
     ${CMAKE_COMMAND} -DMD5=${MD5_PPM_422M_IFAST} -DFILE=testout_422m_ifast.ppm
       -P ${CMAKE_SOURCE_DIR}/cmakescripts/md5cmp.cmake)
-  # CC: YCC->RGB565  SAMP: h2v1 merged  IDCT: ifast  ENT: huff
-  add_test(djpeg${suffix}-422m-ifast-565
-    ${dir}djpeg${suffix} -dct int -nosmooth -rgb565 -dither none -bmp
-      -outfile testout_422m_ifast_565.bmp testout_422_ifast_opt.jpg)
-  add_test(djpeg${suffix}-422m-ifast-565-cmp
-    ${CMAKE_COMMAND} -DMD5=${MD5_BMP_422M_IFAST_565}
-      -DFILE=testout_422m_ifast_565.bmp
-      -P ${CMAKE_SOURCE_DIR}/cmakescripts/md5cmp.cmake)
-  # CC: YCC->RGB565 (dithered)  SAMP: h2v1 merged  IDCT: ifast  ENT: huff
-  add_test(djpeg${suffix}-422m-ifast-565D
-    ${dir}djpeg${suffix} -dct int -nosmooth -rgb565 -bmp
-      -outfile testout_422m_ifast_565D.bmp testout_422_ifast_opt.jpg)
-  add_test(djpeg${suffix}-422m-ifast-565D-cmp
-    ${CMAKE_COMMAND} -DMD5=${MD5_BMP_422M_IFAST_565D}
-      -DFILE=testout_422m_ifast_565D.bmp
-      -P ${CMAKE_SOURCE_DIR}/cmakescripts/md5cmp.cmake)
+  if(NOT WITH_12BIT)
+    # CC: YCC->RGB565  SAMP: h2v1 merged  IDCT: ifast  ENT: huff
+    add_test(djpeg${suffix}-422m-ifast-565
+      ${dir}djpeg${suffix} -dct int -nosmooth -rgb565 -dither none -bmp
+        -outfile testout_422m_ifast_565.bmp testout_422_ifast_opt.jpg)
+    add_test(djpeg${suffix}-422m-ifast-565-cmp
+      ${CMAKE_COMMAND} -DMD5=${MD5_BMP_422M_IFAST_565}
+        -DFILE=testout_422m_ifast_565.bmp
+        -P ${CMAKE_SOURCE_DIR}/cmakescripts/md5cmp.cmake)
+    # CC: YCC->RGB565 (dithered)  SAMP: h2v1 merged  IDCT: ifast  ENT: huff
+    add_test(djpeg${suffix}-422m-ifast-565D
+      ${dir}djpeg${suffix} -dct int -nosmooth -rgb565 -bmp
+        -outfile testout_422m_ifast_565D.bmp testout_422_ifast_opt.jpg)
+    add_test(djpeg${suffix}-422m-ifast-565D-cmp
+      ${CMAKE_COMMAND} -DMD5=${MD5_BMP_422M_IFAST_565D}
+        -DFILE=testout_422m_ifast_565D.bmp
+        -P ${CMAKE_SOURCE_DIR}/cmakescripts/md5cmp.cmake)
+  endif()
 
   # CC: RGB->YCC  SAMP: fullsize/h2v2  FDCT: ifast  ENT: prog huff
   add_test(cjpeg${suffix}-420-q100-ifast-prog
@@ -496,22 +561,24 @@
     ${CMAKE_COMMAND} -DMD5=${MD5_PPM_GRAY_ISLOW_RGB}
       -DFILE=testout_gray_islow_rgb.ppm
       -P ${CMAKE_SOURCE_DIR}/cmakescripts/md5cmp.cmake)
-  # CC: Gray->RGB565  SAMP: fullsize  IDCT: islow  ENT: huff
-  add_test(djpeg${suffix}-gray-islow-565
-    ${dir}djpeg${suffix} -dct int -rgb565 -dither none -bmp
-      -outfile testout_gray_islow_565.bmp testout_gray_islow.jpg)
-  add_test(djpeg${suffix}-gray-islow-565-cmp
-    ${CMAKE_COMMAND} -DMD5=${MD5_BMP_GRAY_ISLOW_565}
-      -DFILE=testout_gray_islow_565.bmp
-      -P ${CMAKE_SOURCE_DIR}/cmakescripts/md5cmp.cmake)
-  # CC: Gray->RGB565 (dithered)  SAMP: fullsize  IDCT: islow  ENT: huff
-  add_test(djpeg${suffix}-gray-islow-565D
-    ${dir}djpeg${suffix} -dct int -rgb565 -bmp
-      -outfile testout_gray_islow_565D.bmp testout_gray_islow.jpg)
-  add_test(djpeg${suffix}-gray-islow-565D-cmp
-    ${CMAKE_COMMAND} -DMD5=${MD5_BMP_GRAY_ISLOW_565D}
-      -DFILE=testout_gray_islow_565D.bmp
-      -P ${CMAKE_SOURCE_DIR}/cmakescripts/md5cmp.cmake)
+  if(NOT WITH_12BIT)
+    # CC: Gray->RGB565  SAMP: fullsize  IDCT: islow  ENT: huff
+    add_test(djpeg${suffix}-gray-islow-565
+      ${dir}djpeg${suffix} -dct int -rgb565 -dither none -bmp
+        -outfile testout_gray_islow_565.bmp testout_gray_islow.jpg)
+    add_test(djpeg${suffix}-gray-islow-565-cmp
+      ${CMAKE_COMMAND} -DMD5=${MD5_BMP_GRAY_ISLOW_565}
+        -DFILE=testout_gray_islow_565.bmp
+        -P ${CMAKE_SOURCE_DIR}/cmakescripts/md5cmp.cmake)
+    # CC: Gray->RGB565 (dithered)  SAMP: fullsize  IDCT: islow  ENT: huff
+    add_test(djpeg${suffix}-gray-islow-565D
+      ${dir}djpeg${suffix} -dct int -rgb565 -bmp
+        -outfile testout_gray_islow_565D.bmp testout_gray_islow.jpg)
+    add_test(djpeg${suffix}-gray-islow-565D-cmp
+      ${CMAKE_COMMAND} -DMD5=${MD5_BMP_GRAY_ISLOW_565D}
+        -DFILE=testout_gray_islow_565D.bmp
+        -P ${CMAKE_SOURCE_DIR}/cmakescripts/md5cmp.cmake)
+  endif()
 
   # CC: RGB->YCC  SAMP: fullsize smooth/h2v2 smooth  FDCT: islow
   # ENT: 2-pass huff
@@ -610,62 +677,63 @@
     add_test(djpeg${suffix}-420m-islow-${scale}
       ${dir}djpeg${suffix} -dct int -scale ${scalearg} -nosmooth -ppm
         -outfile testout_420m_islow_${scale}.ppm
-        ${CMAKE_SOURCE_DIR}/testimages/testorig.jpg)
+        ${CMAKE_SOURCE_DIR}/testimages/${TESTORIG})
     add_test(djpeg${suffix}-420m-islow-${scale}-cmp
       ${CMAKE_COMMAND} -DMD5=${MD5_PPM_420M_ISLOW_${scale}}
         -DFILE=testout_420m_islow_${scale}.ppm
         -P ${CMAKE_SOURCE_DIR}/cmakescripts/md5cmp.cmake)
   endforeach()
 
-  # CC: YCC->RGB (dithered)  SAMP: h2v2 fancy  IDCT: islow  ENT: huff
-  add_test(djpeg${suffix}-420-islow-256
-    ${dir}djpeg${suffix} -dct int -colors 256 -bmp
-      -outfile testout_420_islow_256.bmp
-      ${CMAKE_SOURCE_DIR}/testimages/testorig.jpg)
-  add_test(djpeg${suffix}-420-islow-256-cmp
-    ${CMAKE_COMMAND} -DMD5=${MD5_BMP_420_ISLOW_256}
-      -DFILE=testout_420_islow_256.bmp
-      -P ${CMAKE_SOURCE_DIR}/cmakescripts/md5cmp.cmake)
-  # CC: YCC->RGB565  SAMP: h2v2 fancy  IDCT: islow  ENT: huff
-  add_test(djpeg${suffix}-420-islow-565
-    ${dir}djpeg${suffix} -dct int -rgb565 -dither none -bmp
-      -outfile testout_420_islow_565.bmp
-      ${CMAKE_SOURCE_DIR}/testimages/testorig.jpg)
-  add_test(djpeg${suffix}-420-islow-565-cmp
-    ${CMAKE_COMMAND} -DMD5=${MD5_BMP_420_ISLOW_565}
-      -DFILE=testout_420_islow_565.bmp
-      -P ${CMAKE_SOURCE_DIR}/cmakescripts/md5cmp.cmake)
-  # CC: YCC->RGB565 (dithered)  SAMP: h2v2 fancy  IDCT: islow  ENT: huff
-  add_test(djpeg${suffix}-420-islow-565D
-    ${dir}djpeg${suffix} -dct int -rgb565 -bmp
-      -outfile testout_420_islow_565D.bmp
-      ${CMAKE_SOURCE_DIR}/testimages/testorig.jpg)
-  add_test(djpeg${suffix}-420-islow-565D-cmp
-    ${CMAKE_COMMAND} -DMD5=${MD5_BMP_420_ISLOW_565D}
-      -DFILE=testout_420_islow_565D.bmp
-      -P ${CMAKE_SOURCE_DIR}/cmakescripts/md5cmp.cmake)
-  # CC: YCC->RGB565  SAMP: h2v2 merged  IDCT: islow  ENT: huff
-  add_test(djpeg${suffix}-420m-islow-565
-    ${dir}djpeg${suffix} -dct int -nosmooth -rgb565 -dither none -bmp
-      -outfile testout_420m_islow_565.bmp
-      ${CMAKE_SOURCE_DIR}/testimages/testorig.jpg)
-  add_test(djpeg${suffix}-420m-islow-565-cmp
-    ${CMAKE_COMMAND} -DMD5=${MD5_BMP_420M_ISLOW_565}
-      -DFILE=testout_420m_islow_565.bmp
-      -P ${CMAKE_SOURCE_DIR}/cmakescripts/md5cmp.cmake)
-  # CC: YCC->RGB565 (dithered)  SAMP: h2v2 merged  IDCT: islow  ENT: huff
-  add_test(djpeg${suffix}-420m-islow-565D
-    ${dir}djpeg${suffix} -dct int -nosmooth -rgb565 -bmp
-      -outfile testout_420m_islow_565D.bmp
-      ${CMAKE_SOURCE_DIR}/testimages/testorig.jpg)
-  add_test(djpeg${suffix}-420m-islow-565D-cmp
-    ${CMAKE_COMMAND} -DMD5=${MD5_BMP_420M_ISLOW_565D}
-      -DFILE=testout_420m_islow_565D.bmp
-      -P ${CMAKE_SOURCE_DIR}/cmakescripts/md5cmp.cmake)
-
+  if(NOT WITH_12BIT)
+    # CC: YCC->RGB (dithered)  SAMP: h2v2 fancy  IDCT: islow  ENT: huff
+    add_test(djpeg${suffix}-420-islow-256
+      ${dir}djpeg${suffix} -dct int -colors 256 -bmp
+        -outfile testout_420_islow_256.bmp
+        ${CMAKE_SOURCE_DIR}/testimages/${TESTORIG})
+    add_test(djpeg${suffix}-420-islow-256-cmp
+      ${CMAKE_COMMAND} -DMD5=${MD5_BMP_420_ISLOW_256}
+        -DFILE=testout_420_islow_256.bmp
+        -P ${CMAKE_SOURCE_DIR}/cmakescripts/md5cmp.cmake)
+    # CC: YCC->RGB565  SAMP: h2v2 fancy  IDCT: islow  ENT: huff
+    add_test(djpeg${suffix}-420-islow-565
+      ${dir}djpeg${suffix} -dct int -rgb565 -dither none -bmp
+        -outfile testout_420_islow_565.bmp
+        ${CMAKE_SOURCE_DIR}/testimages/${TESTORIG})
+    add_test(djpeg${suffix}-420-islow-565-cmp
+      ${CMAKE_COMMAND} -DMD5=${MD5_BMP_420_ISLOW_565}
+        -DFILE=testout_420_islow_565.bmp
+        -P ${CMAKE_SOURCE_DIR}/cmakescripts/md5cmp.cmake)
+    # CC: YCC->RGB565 (dithered)  SAMP: h2v2 fancy  IDCT: islow  ENT: huff
+    add_test(djpeg${suffix}-420-islow-565D
+      ${dir}djpeg${suffix} -dct int -rgb565 -bmp
+        -outfile testout_420_islow_565D.bmp
+        ${CMAKE_SOURCE_DIR}/testimages/${TESTORIG})
+    add_test(djpeg${suffix}-420-islow-565D-cmp
+      ${CMAKE_COMMAND} -DMD5=${MD5_BMP_420_ISLOW_565D}
+        -DFILE=testout_420_islow_565D.bmp
+        -P ${CMAKE_SOURCE_DIR}/cmakescripts/md5cmp.cmake)
+    # CC: YCC->RGB565  SAMP: h2v2 merged  IDCT: islow  ENT: huff
+    add_test(djpeg${suffix}-420m-islow-565
+      ${dir}djpeg${suffix} -dct int -nosmooth -rgb565 -dither none -bmp
+        -outfile testout_420m_islow_565.bmp
+        ${CMAKE_SOURCE_DIR}/testimages/${TESTORIG})
+    add_test(djpeg${suffix}-420m-islow-565-cmp
+      ${CMAKE_COMMAND} -DMD5=${MD5_BMP_420M_ISLOW_565}
+        -DFILE=testout_420m_islow_565.bmp
+        -P ${CMAKE_SOURCE_DIR}/cmakescripts/md5cmp.cmake)
+    # CC: YCC->RGB565 (dithered)  SAMP: h2v2 merged  IDCT: islow  ENT: huff
+    add_test(djpeg${suffix}-420m-islow-565D
+      ${dir}djpeg${suffix} -dct int -nosmooth -rgb565 -bmp
+        -outfile testout_420m_islow_565D.bmp
+        ${CMAKE_SOURCE_DIR}/testimages/${TESTORIG})
+    add_test(djpeg${suffix}-420m-islow-565D-cmp
+      ${CMAKE_COMMAND} -DMD5=${MD5_BMP_420M_ISLOW_565D}
+        -DFILE=testout_420m_islow_565D.bmp
+        -P ${CMAKE_SOURCE_DIR}/cmakescripts/md5cmp.cmake)
+  endif()
   add_test(jpegtran${suffix}-crop
     ${dir}jpegtran${suffix} -crop 120x90+20+50 -transpose -perfect
-      -outfile testout_crop.jpg ${CMAKE_SOURCE_DIR}/testimages/testorig.jpg)
+      -outfile testout_crop.jpg ${CMAKE_SOURCE_DIR}/testimages/${TESTORIG})
   add_test(jpegtran${suffix}-crop-cmp
     ${CMAKE_COMMAND} -DMD5=${MD5_JPEG_CROP} -DFILE=testout_crop.jpg
       -P ${CMAKE_SOURCE_DIR}/cmakescripts/md5cmp.cmake)
@@ -721,7 +789,10 @@
     cjpeg djpeg jpegtran tjbench ${JAVA_DEPEND}
   SOURCES libjpeg-turbo.nsi)
 
-install(TARGETS jpeg-static turbojpeg turbojpeg-static rdjpgcom wrjpgcom tjbench
+if(WITH_TURBOJPEG)
+  set(TURBOJPEG_TARGETS turbojpeg turbojpeg-static tjbench)
+endif()
+install(TARGETS jpeg-static rdjpgcom wrjpgcom ${TURBOJPEG_TARGETS}
   ARCHIVE DESTINATION lib
   LIBRARY DESTINATION lib
   RUNTIME DESTINATION bin
diff --git a/ChangeLog.txt b/ChangeLog.txt
index b690133..a463254 100644
--- a/ChangeLog.txt
+++ b/ChangeLog.txt
@@ -80,6 +80,13 @@
 [12] Fixed a bug in the build system that was causing the Windows version of
 wrjpgcom to be built using the rdjpgcom code.
 
+[13] Restored 12-bit-per-component JPEG support.  A 12-bit version of
+libjpeg-turbo can now be built by passing an argument of --with-12bit to
+configure (Unix) or -DWITH_12BIT=1 to cmake (Windows.)  12-bit JPEG support is
+included only for convenience.  Enabling this feature disables all of the
+performance features in libjpeg-turbo, as well as arithmetic coding and the
+TurboJPEG API.  The resulting library behaves no differently than libjpeg v6b.
+
 
 1.3.1
 =====
diff --git a/Makefile.am b/Makefile.am
index cee54eb..dad69df 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -108,21 +108,34 @@
 endif
 
 
-cjpeg_SOURCES = cdjpeg.h cderror.h cdjpeg.c cjpeg.c rdbmp.c rdgif.c \
-	rdppm.c rdswitch.c rdtarga.c
+cjpeg_SOURCES = cdjpeg.h cderror.h cdjpeg.c cjpeg.c rdgif.c rdppm.c rdswitch.c
+if WITH_12BIT
+else
+cjpeg_SOURCES += rdbmp.c rdtarga.c
+endif
 
 cjpeg_LDADD = libjpeg.la
 
-cjpeg_CFLAGS = -DBMP_SUPPORTED -DGIF_SUPPORTED -DPPM_SUPPORTED \
-	-DTARGA_SUPPORTED
+cjpeg_CFLAGS = -DGIF_SUPPORTED -DPPM_SUPPORTED
+if WITH_12BIT
+else
+cjpeg_CFLAGS += -DBMP_SUPPORTED -DTARGA_SUPPORTED
+endif
 
 djpeg_SOURCES = cdjpeg.h cderror.h cdjpeg.c djpeg.c rdcolmap.c rdswitch.c \
-	wrbmp.c wrgif.c wrppm.c wrtarga.c
+	wrgif.c wrppm.c
+if WITH_12BIT
+else
+djpeg_SOURCES += wrbmp.c wrtarga.c
+endif
 
 djpeg_LDADD = libjpeg.la
 
-djpeg_CFLAGS = -DBMP_SUPPORTED -DGIF_SUPPORTED -DPPM_SUPPORTED \
-	-DTARGA_SUPPORTED
+djpeg_CFLAGS = -DGIF_SUPPORTED -DPPM_SUPPORTED
+if WITH_12BIT
+else
+djpeg_CFLAGS += -DBMP_SUPPORTED -DTARGA_SUPPORTED
+endif
 
 jpegtran_SOURCES = jpegtran.c rdswitch.c cdjpeg.c transupp.c transupp.h
 
@@ -163,6 +176,40 @@
 
 SUBDIRS += md5
 
+if WITH_12BIT
+
+TESTORIG = testorig12.jpg
+MD5_JPEG_RGB_ISLOW = 9620f424569594bb9242b48498ad801f
+MD5_PPM_RGB_ISLOW = f3301d2219783b8b3d942b7239fa50c0
+MD5_JPEG_422_IFAST_OPT = 7322e3bd2f127f7de4b40d4480ce60e4
+MD5_PPM_422_IFAST = 79807fa552899e66a04708f533e16950
+MD5_PPM_422M_IFAST = 07737bfe8a7c1c87aaa393a0098d16b0
+MD5_JPEG_420_IFAST_Q100_PROG = a1da220b5604081863a504297ed59e55
+MD5_PPM_420_Q100_IFAST = 1b3730122709f53d007255e8dfd3305e
+MD5_PPM_420M_Q100_IFAST = 980a1a3c5bf9510022869d30b7d26566
+MD5_JPEG_GRAY_ISLOW = 235c90707b16e2e069f37c888b2636d9
+MD5_PPM_GRAY_ISLOW = 7213c10af507ad467da5578ca5ee1fca
+MD5_PPM_GRAY_ISLOW_RGB = e96ee81c30a6ed422d466338bd3de65d
+MD5_JPEG_420S_IFAST_OPT = 7af8e60be4d9c227ec63ac9b6630855e
+MD5_JPEG_3x2_FLOAT_PROG = a8c17daf77b457725ec929e215b603f8
+MD5_PPM_3x2_FLOAT = 42876ab9e5c2f76a87d08db5fbd57956
+MD5_PPM_420M_ISLOW_2_1 = 4ca6be2a6f326ff9eaab63e70a8259c0
+MD5_PPM_420M_ISLOW_15_8 = 12aa9f9534c1b3d7ba047322226365eb
+MD5_PPM_420M_ISLOW_13_8 = f7e22817c7b25e1393e4ec101e9d4e96
+MD5_PPM_420M_ISLOW_11_8 = 800a16f9f4dc9b293197bfe11be10a82
+MD5_PPM_420M_ISLOW_9_8 = 06b7a92a9bc69f4dc36ec40f1937d55c
+MD5_PPM_420M_ISLOW_7_8 = 3ec444a14a4ab4eab88ffc49c48eca43
+MD5_PPM_420M_ISLOW_3_4 = 3e726b7ea872445b19437d1c1d4f0d93
+MD5_PPM_420M_ISLOW_5_8 = a8a771abdc94301d20ffac119b2caccd
+MD5_PPM_420M_ISLOW_1_2 = b419124dd5568b085787234866102866
+MD5_PPM_420M_ISLOW_3_8 = 343d19015531b7bbe746124127244fa8
+MD5_PPM_420M_ISLOW_1_4 = 35fd59d866e44659edfa3c18db2a3edb
+MD5_PPM_420M_ISLOW_1_8 = ccaed48ac0aedefda5d4abe4013f4ad7
+MD5_JPEG_CROP = cdb35ff4b4519392690ea040c56ea99c
+
+else
+
+TESTORIG = testorig.jpg
 MD5_JPEG_RGB_ISLOW = 768e970dd57b340ff1b83c9d3d47c77b
 MD5_PPM_RGB_ISLOW = 00a257f5393fef8821f2b88ac7421291
 MD5_BMP_RGB_ISLOW_565 = f07d2e75073e4bb10f6c6f4d36e2e3be
@@ -212,6 +259,8 @@
 MD5_BMP_420M_ISLOW_565D =d1be3a3339166255e76fa50a0d70d73e
 MD5_JPEG_CROP = b4197f377e621c4e9b1d20471432610d
 
+endif
+
 test: testclean all
 
 if WITH_TURBOJPEG
@@ -241,6 +290,9 @@
 	./djpeg -dct int -ppm -outfile testout_rgb_islow.ppm testout_rgb_islow.jpg
 	md5/md5cmp $(MD5_PPM_RGB_ISLOW) testout_rgb_islow.ppm
 	rm testout_rgb_islow.ppm
+if WITH_12BIT
+	rm testout_rgb_islow.jpg
+else
 # CC: RGB->RGB565  SAMP: fullsize  IDCT: islow  ENT: huff
 	./djpeg -dct int -rgb565 -dither none -bmp -outfile testout_rgb_islow_565.bmp testout_rgb_islow.jpg
 	md5/md5cmp $(MD5_BMP_RGB_ISLOW_565) testout_rgb_islow_565.bmp
@@ -249,6 +301,7 @@
 	./djpeg -dct int -rgb565 -bmp -outfile testout_rgb_islow_565D.bmp testout_rgb_islow.jpg
 	md5/md5cmp $(MD5_BMP_RGB_ISLOW_565D) testout_rgb_islow_565D.bmp
 	rm testout_rgb_islow_565D.bmp testout_rgb_islow.jpg
+endif
 
 # CC: RGB->YCC  SAMP: fullsize/h2v1  FDCT: ifast  ENT: 2-pass huff
 	./cjpeg -sample 2x1 -dct fast -opt -outfile testout_422_ifast_opt.jpg $(srcdir)/testimages/testorig.ppm
@@ -261,6 +314,9 @@
 	./djpeg -dct fast -nosmooth -outfile testout_422m_ifast.ppm testout_422_ifast_opt.jpg
 	md5/md5cmp $(MD5_PPM_422M_IFAST) testout_422m_ifast.ppm
 	rm testout_422m_ifast.ppm
+if WITH_12BIT
+	rm testout_422_ifast_opt.jpg
+else
 # CC: YCC->RGB565  SAMP: h2v1 merged  IDCT: ifast  ENT: huff
 	./djpeg -dct int -nosmooth -rgb565 -dither none -bmp -outfile testout_422m_ifast_565.bmp testout_422_ifast_opt.jpg
 	md5/md5cmp $(MD5_BMP_422M_IFAST_565) testout_422m_ifast_565.bmp
@@ -269,6 +325,7 @@
 	./djpeg -dct int -nosmooth -rgb565 -bmp -outfile testout_422m_ifast_565D.bmp testout_422_ifast_opt.jpg
 	md5/md5cmp $(MD5_BMP_422M_IFAST_565D) testout_422m_ifast_565D.bmp
 	rm testout_422m_ifast_565D.bmp testout_422_ifast_opt.jpg
+endif
 
 # CC: RGB->YCC  SAMP: fullsize/h2v2  FDCT: ifast  ENT: prog huff
 	./cjpeg -sample 2x2 -quality 100 -dct fast -prog -outfile testout_420_q100_ifast_prog.jpg $(srcdir)/testimages/testorig.ppm
@@ -293,6 +350,9 @@
 	./djpeg -dct int -rgb -outfile testout_gray_islow_rgb.ppm testout_gray_islow.jpg
 	md5/md5cmp $(MD5_PPM_GRAY_ISLOW_RGB) testout_gray_islow_rgb.ppm
 	rm testout_gray_islow_rgb.ppm
+if WITH_12BIT
+	rm testout_gray_islow.jpg
+else
 # CC: Gray->RGB565  SAMP: fullsize  IDCT: islow  ENT: huff
 	./djpeg -dct int -rgb565 -dither none -bmp -outfile testout_gray_islow_565.bmp testout_gray_islow.jpg
 	md5/md5cmp $(MD5_BMP_GRAY_ISLOW_565) testout_gray_islow_565.bmp
@@ -301,6 +361,7 @@
 	./djpeg -dct int -rgb565 -bmp -outfile testout_gray_islow_565D.bmp testout_gray_islow.jpg
 	md5/md5cmp $(MD5_BMP_GRAY_ISLOW_565D) testout_gray_islow_565D.bmp
 	rm testout_gray_islow_565D.bmp testout_gray_islow.jpg
+endif
 
 # CC: RGB->YCC  SAMP: fullsize smooth/h2v2 smooth  FDCT: islow
 # ENT: 2-pass huff
@@ -340,75 +401,78 @@
 endif
 
 # CC: YCC->RGB  SAMP: h2v2 merged  IDCT: 16x16 islow  ENT: huff
-	./djpeg -dct int -scale 2/1 -nosmooth -ppm -outfile testout_420m_islow_2_1.ppm $(srcdir)/testimages/testorig.jpg
+	./djpeg -dct int -scale 2/1 -nosmooth -ppm -outfile testout_420m_islow_2_1.ppm $(srcdir)/testimages/$(TESTORIG)
 	md5/md5cmp $(MD5_PPM_420M_ISLOW_2_1) testout_420m_islow_2_1.ppm
 	rm testout_420m_islow_2_1.ppm
 # CC: YCC->RGB  SAMP: h2v2 merged  IDCT: 15x15 islow  ENT: huff
-	./djpeg -dct int -scale 15/8 -nosmooth -ppm -outfile testout_420m_islow_15_8.ppm $(srcdir)/testimages/testorig.jpg
+	./djpeg -dct int -scale 15/8 -nosmooth -ppm -outfile testout_420m_islow_15_8.ppm $(srcdir)/testimages/$(TESTORIG)
 	md5/md5cmp $(MD5_PPM_420M_ISLOW_15_8) testout_420m_islow_15_8.ppm
 	rm testout_420m_islow_15_8.ppm
 # CC: YCC->RGB  SAMP: h2v2 merged  IDCT: 13x13 islow  ENT: huff
-	./djpeg -dct int -scale 13/8 -nosmooth -ppm -outfile testout_420m_islow_13_8.ppm $(srcdir)/testimages/testorig.jpg
+	./djpeg -dct int -scale 13/8 -nosmooth -ppm -outfile testout_420m_islow_13_8.ppm $(srcdir)/testimages/$(TESTORIG)
 	md5/md5cmp $(MD5_PPM_420M_ISLOW_13_8) testout_420m_islow_13_8.ppm
 	rm testout_420m_islow_13_8.ppm
 # CC: YCC->RGB  SAMP: h2v2 merged  IDCT: 11x11 islow  ENT: huff
-	./djpeg -dct int -scale 11/8 -nosmooth -ppm -outfile testout_420m_islow_11_8.ppm $(srcdir)/testimages/testorig.jpg
+	./djpeg -dct int -scale 11/8 -nosmooth -ppm -outfile testout_420m_islow_11_8.ppm $(srcdir)/testimages/$(TESTORIG)
 	md5/md5cmp $(MD5_PPM_420M_ISLOW_11_8) testout_420m_islow_11_8.ppm
 	rm testout_420m_islow_11_8.ppm
 # CC: YCC->RGB  SAMP: h2v2 merged  IDCT: 9x9 islow  ENT: huff
-	./djpeg -dct int -scale 9/8 -nosmooth -ppm -outfile testout_420m_islow_9_8.ppm $(srcdir)/testimages/testorig.jpg
+	./djpeg -dct int -scale 9/8 -nosmooth -ppm -outfile testout_420m_islow_9_8.ppm $(srcdir)/testimages/$(TESTORIG)
 	md5/md5cmp $(MD5_PPM_420M_ISLOW_9_8) testout_420m_islow_9_8.ppm
 	rm testout_420m_islow_9_8.ppm
 # CC: YCC->RGB  SAMP: h2v2 merged  IDCT: 7x7 islow/14x14 islow  ENT: huff
-	./djpeg -dct int -scale 7/8 -nosmooth -ppm -outfile testout_420m_islow_7_8.ppm $(srcdir)/testimages/testorig.jpg
+	./djpeg -dct int -scale 7/8 -nosmooth -ppm -outfile testout_420m_islow_7_8.ppm $(srcdir)/testimages/$(TESTORIG)
 	md5/md5cmp $(MD5_PPM_420M_ISLOW_7_8) testout_420m_islow_7_8.ppm
 	rm testout_420m_islow_7_8.ppm
 # CC: YCC->RGB  SAMP: h2v2 merged  IDCT: 6x6 islow/12x12 islow  ENT: huff
-	./djpeg -dct int -scale 3/4 -nosmooth -ppm -outfile testout_420m_islow_3_4.ppm $(srcdir)/testimages/testorig.jpg
+	./djpeg -dct int -scale 3/4 -nosmooth -ppm -outfile testout_420m_islow_3_4.ppm $(srcdir)/testimages/$(TESTORIG)
 	md5/md5cmp $(MD5_PPM_420M_ISLOW_3_4) testout_420m_islow_3_4.ppm
 	rm testout_420m_islow_3_4.ppm
 # CC: YCC->RGB  SAMP: h2v2 merged  IDCT: 5x5 islow/10x10 islow  ENT: huff
-	./djpeg -dct int -scale 5/8 -nosmooth -ppm -outfile testout_420m_islow_5_8.ppm $(srcdir)/testimages/testorig.jpg
+	./djpeg -dct int -scale 5/8 -nosmooth -ppm -outfile testout_420m_islow_5_8.ppm $(srcdir)/testimages/$(TESTORIG)
 	md5/md5cmp $(MD5_PPM_420M_ISLOW_5_8) testout_420m_islow_5_8.ppm
 	rm testout_420m_islow_5_8.ppm
 # CC: YCC->RGB  SAMP: h2v2 merged  IDCT: 4x4 islow/8x8 islow  ENT: huff
-	./djpeg -dct int -scale 1/2 -nosmooth -ppm -outfile testout_420m_islow_1_2.ppm $(srcdir)/testimages/testorig.jpg
+	./djpeg -dct int -scale 1/2 -nosmooth -ppm -outfile testout_420m_islow_1_2.ppm $(srcdir)/testimages/$(TESTORIG)
 	md5/md5cmp $(MD5_PPM_420M_ISLOW_1_2) testout_420m_islow_1_2.ppm
 	rm testout_420m_islow_1_2.ppm
 # CC: YCC->RGB  SAMP: h2v2 merged  IDCT: 3x3 islow/6x6 islow  ENT: huff
-	./djpeg -dct int -scale 3/8 -nosmooth -ppm -outfile testout_420m_islow_3_8.ppm $(srcdir)/testimages/testorig.jpg
+	./djpeg -dct int -scale 3/8 -nosmooth -ppm -outfile testout_420m_islow_3_8.ppm $(srcdir)/testimages/$(TESTORIG)
 	md5/md5cmp $(MD5_PPM_420M_ISLOW_3_8) testout_420m_islow_3_8.ppm
 	rm testout_420m_islow_3_8.ppm
 # CC: YCC->RGB  SAMP: h2v2 merged  IDCT: 2x2 islow/4x4 islow  ENT: huff
-	./djpeg -dct int -scale 1/4 -nosmooth -ppm -outfile testout_420m_islow_1_4.ppm $(srcdir)/testimages/testorig.jpg
+	./djpeg -dct int -scale 1/4 -nosmooth -ppm -outfile testout_420m_islow_1_4.ppm $(srcdir)/testimages/$(TESTORIG)
 	md5/md5cmp $(MD5_PPM_420M_ISLOW_1_4) testout_420m_islow_1_4.ppm
 	rm testout_420m_islow_1_4.ppm
 # CC: YCC->RGB  SAMP: h2v2 merged  IDCT: 1x1 islow/2x2 islow  ENT: huff
-	./djpeg -dct int -scale 1/8 -nosmooth -ppm -outfile testout_420m_islow_1_8.ppm $(srcdir)/testimages/testorig.jpg
+	./djpeg -dct int -scale 1/8 -nosmooth -ppm -outfile testout_420m_islow_1_8.ppm $(srcdir)/testimages/$(TESTORIG)
 	md5/md5cmp $(MD5_PPM_420M_ISLOW_1_8) testout_420m_islow_1_8.ppm
 	rm testout_420m_islow_1_8.ppm
+if WITH_12BIT
+else
 # CC: YCC->RGB (dithered)  SAMP: h2v2 fancy  IDCT: islow  ENT: huff
-	./djpeg -dct int -colors 256 -bmp -outfile testout_420_islow_256.bmp $(srcdir)/testimages/testorig.jpg
+	./djpeg -dct int -colors 256 -bmp -outfile testout_420_islow_256.bmp $(srcdir)/testimages/$(TESTORIG)
 	md5/md5cmp $(MD5_BMP_420_ISLOW_256) testout_420_islow_256.bmp
 	rm testout_420_islow_256.bmp
 # CC: YCC->RGB565  SAMP: h2v2 fancy  IDCT: islow  ENT: huff
-	./djpeg -dct int -rgb565 -dither none -bmp -outfile testout_420_islow_565.bmp $(srcdir)/testimages/testorig.jpg
+	./djpeg -dct int -rgb565 -dither none -bmp -outfile testout_420_islow_565.bmp $(srcdir)/testimages/$(TESTORIG)
 	md5/md5cmp $(MD5_BMP_420_ISLOW_565) testout_420_islow_565.bmp
 	rm testout_420_islow_565.bmp
 # CC: YCC->RGB565 (dithered)  SAMP: h2v2 fancy  IDCT: islow  ENT: huff
-	./djpeg -dct int -rgb565 -bmp -outfile testout_420_islow_565D.bmp $(srcdir)/testimages/testorig.jpg
+	./djpeg -dct int -rgb565 -bmp -outfile testout_420_islow_565D.bmp $(srcdir)/testimages/$(TESTORIG)
 	md5/md5cmp $(MD5_BMP_420_ISLOW_565D) testout_420_islow_565D.bmp
 	rm testout_420_islow_565D.bmp
 # CC: YCC->RGB565  SAMP: h2v2 merged  IDCT: islow  ENT: huff
-	./djpeg -dct int -nosmooth -rgb565 -dither none -bmp -outfile testout_420m_islow_565.bmp $(srcdir)/testimages/testorig.jpg
+	./djpeg -dct int -nosmooth -rgb565 -dither none -bmp -outfile testout_420m_islow_565.bmp $(srcdir)/testimages/$(TESTORIG)
 	md5/md5cmp $(MD5_BMP_420M_ISLOW_565) testout_420m_islow_565.bmp
 	rm testout_420m_islow_565.bmp
 # CC: YCC->RGB565 (dithered)  SAMP: h2v2 merged  IDCT: islow  ENT: huff
-	./djpeg -dct int -nosmooth -rgb565 -bmp -outfile testout_420m_islow_565D.bmp $(srcdir)/testimages/testorig.jpg
+	./djpeg -dct int -nosmooth -rgb565 -bmp -outfile testout_420m_islow_565D.bmp $(srcdir)/testimages/$(TESTORIG)
 	md5/md5cmp $(MD5_BMP_420M_ISLOW_565D) testout_420m_islow_565D.bmp
 	rm testout_420m_islow_565D.bmp
+endif
 
-	./jpegtran -crop 120x90+20+50 -transpose -perfect -outfile testout_crop.jpg $(srcdir)/testimages/testorig.jpg
+	./jpegtran -crop 120x90+20+50 -transpose -perfect -outfile testout_crop.jpg $(srcdir)/testimages/$(TESTORIG)
 	md5/md5cmp $(MD5_JPEG_CROP) testout_crop.jpg
 	rm testout_crop.jpg
 
diff --git a/configure.ac b/configure.ac
index d2de185..64f3265 100644
--- a/configure.ac
+++ b/configure.ac
@@ -269,6 +269,9 @@
 AC_ARG_WITH([arith-enc],
   AC_HELP_STRING([--without-arith-enc],
     [Do not include arithmetic encoding support]))
+if test "x$with_12bit" = "xyes"; then
+  with_arith_enc=no
+fi
 if test "x$with_arith_enc" = "xno"; then
   AC_MSG_RESULT(no)
   RPM_CONFIG_ARGS="$RPM_CONFIG_ARGS --without-arith-enc"
@@ -282,6 +285,9 @@
 AC_ARG_WITH([arith-dec],
   AC_HELP_STRING([--without-arith-dec],
     [Do not include arithmetic decoding support]))
+if test "x$with_12bit" = "xyes"; then
+  with_arith_dec=no
+fi
 if test "x$with_arith_dec" = "xno"; then
   AC_MSG_RESULT(no)
   RPM_CONFIG_ARGS="$RPM_CONFIG_ARGS --without-arith-dec"
@@ -294,11 +300,26 @@
 AM_CONDITIONAL([WITH_ARITH],
   [test "x$with_arith_dec" != "xno" -o "x$with_arith_enc" != "xno"])
 
+# 12-bit component support
+AC_MSG_CHECKING([whether to use 12-bit samples])
+AC_ARG_WITH([12bit],
+  AC_HELP_STRING([--with-12bit], [Encode/decode JPEG images with 12-bit samples (implies --without-simd --without-turbojpeg --without-arith-dec --without-arith-enc)]))
+if test "x$with_12bit" = "xyes"; then
+  AC_DEFINE([BITS_IN_JSAMPLE], [12], [use 8 or 12])
+  AC_MSG_RESULT(yes)
+else
+  AC_MSG_RESULT(no)
+fi
+AM_CONDITIONAL([WITH_12BIT], [test "x$with_12bit" = "xyes"])
+
 # TurboJPEG support
 AC_MSG_CHECKING([whether to build TurboJPEG C wrapper])
 AC_ARG_WITH([turbojpeg],
   AC_HELP_STRING([--without-turbojpeg],
     [Do not include the TurboJPEG wrapper library and associated test programs]))
+if test "x$with_12bit" = "xyes"; then
+  with_turbojpeg=no
+fi
 if test "x$with_turbojpeg" = "xno"; then
   AC_MSG_RESULT(no)
   RPM_CONFIG_ARGS="$RPM_CONFIG_ARGS --without-turbojpeg"
@@ -330,7 +351,7 @@
 AC_MSG_CHECKING([whether to build TurboJPEG Java wrapper])
 AC_ARG_WITH([java],
   AC_HELP_STRING([--with-java], [Build Java wrapper for the TurboJPEG library]))
-if test "x$with_turbojpeg" = "xno"; then
+if test "x$with_12bit" = "xyes" -o "x$with_turbojpeg" = "xno"; then
   with_java=no
 fi
 
@@ -391,6 +412,9 @@
 # SIMD is optional
 AC_ARG_WITH([simd],
   AC_HELP_STRING([--without-simd], [Do not include SIMD extensions]))
+if test "x$with_12bit" = "xyes"; then
+  with_simd=no
+fi
 if test "x${with_simd}" != "xno"; then
   require_simd=no
   if test "x${with_simd}" = "xyes"; then
diff --git a/jcdctmgr.c b/jcdctmgr.c
index 2d25659..7893572 100644
--- a/jcdctmgr.c
+++ b/jcdctmgr.c
@@ -6,7 +6,7 @@
  * libjpeg-turbo Modifications:
  * Copyright (C) 1999-2006, MIYASAKA Masaru.
  * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
- * Copyright (C) 2011 D. R. Commander
+ * Copyright (C) 2011, 2014 D. R. Commander
  * For conditions of distribution and use, see the accompanying README file.
  *
  * This file contains the forward-DCT management logic.
@@ -72,9 +72,12 @@
 typedef my_fdct_controller * my_fdct_ptr;
 
 
+#if BITS_IN_JSAMPLE == 8
+
 /*
  * Find the highest bit in an integer through binary search.
  */
+
 LOCAL(int)
 flss (UINT16 val)
 {
@@ -105,6 +108,7 @@
   return bit;
 }
 
+
 /*
  * Compute values to do a division using reciprocal.
  *
@@ -163,6 +167,7 @@
  * of in a consecutive manner, yet again in order to allow SIMD
  * routines.
  */
+
 LOCAL(int)
 compute_reciprocal (UINT16 divisor, DCTELEM * dtbl)
 {
@@ -197,6 +202,9 @@
   else return 1;
 }
 
+#endif
+
+
 /*
  * Initialize for a processing pass.
  * Verify that all referenced Q-tables are present, and set up
@@ -238,9 +246,13 @@
       }
       dtbl = fdct->divisors[qtblno];
       for (i = 0; i < DCTSIZE2; i++) {
+#if BITS_IN_JSAMPLE == 8
         if(!compute_reciprocal(qtbl->quantval[i] << 3, &dtbl[i])
           && fdct->quantize == jsimd_quantize)
           fdct->quantize = quantize;
+#else
+        dtbl[i] = ((DCTELEM) qtbl->quantval[i]) << 3;
+#endif
       }
       break;
 #endif
@@ -274,12 +286,19 @@
         }
         dtbl = fdct->divisors[qtblno];
         for (i = 0; i < DCTSIZE2; i++) {
+#if BITS_IN_JSAMPLE == 8
           if(!compute_reciprocal(
             DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
                                   (INT32) aanscales[i]),
                     CONST_BITS-3), &dtbl[i])
             && fdct->quantize == jsimd_quantize)
             fdct->quantize = quantize;
+#else
+           dtbl[i] = (DCTELEM)
+             DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
+                                   (INT32) aanscales[i]),
+                     CONST_BITS-3);
+#endif
         }
       }
       break;
@@ -372,9 +391,12 @@
 {
   int i;
   DCTELEM temp;
+  JCOEFPTR output_ptr = coef_block;
+
+#if BITS_IN_JSAMPLE == 8
+
   UDCTELEM recip, corr, shift;
   UDCTELEM2 product;
-  JCOEFPTR output_ptr = coef_block;
 
   for (i = 0; i < DCTSIZE2; i++) {
     temp = workspace[i];
@@ -393,9 +415,47 @@
       product >>= shift + sizeof(DCTELEM)*8;
       temp = product;
     }
-
     output_ptr[i] = (JCOEF) temp;
   }
+
+#else
+
+  register DCTELEM qval;
+
+  for (i = 0; i < DCTSIZE2; i++) {
+    qval = divisors[i];
+    temp = workspace[i];
+    /* Divide the coefficient value by qval, ensuring proper rounding.
+     * Since C does not specify the direction of rounding for negative
+     * quotients, we have to force the dividend positive for portability.
+     *
+     * In most files, at least half of the output values will be zero
+     * (at default quantization settings, more like three-quarters...)
+     * so we should ensure that this case is fast.  On many machines,
+     * a comparison is enough cheaper than a divide to make a special test
+     * a win.  Since both inputs will be nonnegative, we need only test
+     * for a < b to discover whether a/b is 0.
+     * If your machine's division is fast enough, define FAST_DIVIDE.
+     */
+#ifdef FAST_DIVIDE
+#define DIVIDE_BY(a,b)  a /= b
+#else
+#define DIVIDE_BY(a,b)  if (a >= b) a /= b; else a = 0
+#endif
+    if (temp < 0) {
+      temp = -temp;
+      temp += qval>>1;  /* for rounding */
+      DIVIDE_BY(temp, qval);
+      temp = -temp;    
+    } else {
+      temp += qval>>1;  /* for rounding */
+      DIVIDE_BY(temp, qval);
+    }
+    output_ptr[i] = (JCOEF) temp;
+  }
+
+#endif
+
 }
 
 
diff --git a/jconfig.h.in b/jconfig.h.in
index cdca01f..42d86f2 100644
--- a/jconfig.h.in
+++ b/jconfig.h.in
@@ -12,6 +12,17 @@
 /* Support arithmetic decoding */
 #undef D_ARITH_CODING_SUPPORTED
 
+/*
+ * Define BITS_IN_JSAMPLE as either
+ *   8   for 8-bit sample values (the usual setting)
+ *   12  for 12-bit sample values
+ * Only 8 and 12 are legal data precisions for lossy JPEG according to the
+ * JPEG standard, and the IJG code does not support anything else!
+ * We do not support run-time selection of data precision, sorry.
+ */
+
+#define BITS_IN_JSAMPLE  8      /* use 8 or 12 */
+
 /* Define to 1 if you have the <locale.h> header file. */
 #undef HAVE_LOCALE_H
 
diff --git a/jdct.h b/jdct.h
index c06bf9d..6f8b159 100644
--- a/jdct.h
+++ b/jdct.h
@@ -41,7 +41,6 @@
 #endif
 #else
 typedef INT32 DCTELEM;          /* must have 32 bits */
-typedef UINT32 UDCTELEM;
 typedef unsigned long long UDCTELEM2;
 #endif
 
diff --git a/jmorecfg.h b/jmorecfg.h
index 91fbd15..36fe971 100644
--- a/jmorecfg.h
+++ b/jmorecfg.h
@@ -14,18 +14,6 @@
 
 
 /*
- * Define BITS_IN_JSAMPLE as either
- *   8   for 8-bit sample values (the usual setting)
- *   12  for 12-bit sample values
- * Only 8 and 12 are legal data precisions for lossy JPEG according to the
- * JPEG standard, and the IJG code does not support anything else!
- * We do not support run-time selection of data precision, sorry.
- */
-
-#define BITS_IN_JSAMPLE  8      /* use 8 or 12 */
-
-
-/*
  * Maximum number of components (color channels) allowed in JPEG image.
  * To meet the letter of the JPEG spec, set this to 255.  However, darn
  * few applications need more than 4 channels (maybe 5 for CMYK + alpha
diff --git a/sharedlib/CMakeLists.txt b/sharedlib/CMakeLists.txt
index cd3f268..d423cce 100755
--- a/sharedlib/CMakeLists.txt
+++ b/sharedlib/CMakeLists.txt
@@ -42,16 +42,22 @@
   add_dependencies(jpeg simd)
 endif()
 
-add_executable(cjpeg ../cjpeg.c ../cdjpeg.c ../rdbmp.c ../rdgif.c ../rdppm.c
-  ../rdswitch.c ../rdtarga.c)
-set_property(TARGET cjpeg PROPERTY COMPILE_FLAGS
-  "-DBMP_SUPPORTED -DGIF_SUPPORTED -DPPM_SUPPORTED -DTARGA_SUPPORTED -DUSE_SETMODE")
+if(WITH_12BIT)
+  set(COMPILE_FLAGS "-DGIF_SUPPORTED -DPPM_SUPPORTED -DUSE_SETMODE")
+else()
+  set(COMPILE_FLAGS "-DBMP_SUPPORTED -DGIF_SUPPORTED -DPPM_SUPPORTED -DTARGA_SUPPORTED -DUSE_SETMODE")
+	set(CJPEG_BMP_SOURCES ../rdbmp.c ../rdtarga.c)
+	set(DJPEG_BMP_SOURCES ../wrbmp.c ../wrtarga.c)
+endif()
+
+add_executable(cjpeg ../cjpeg.c ../cdjpeg.c ../rdgif.c ../rdppm.c
+  ../rdswitch.c ${CJPEG_BMP_SOURCES})
+set_property(TARGET cjpeg PROPERTY COMPILE_FLAGS ${COMPILE_FLAGS})
 target_link_libraries(cjpeg jpeg)
 
 add_executable(djpeg ../djpeg.c ../cdjpeg.c ../rdcolmap.c ../rdswitch.c
-  ../wrbmp.c ../wrgif.c ../wrppm.c ../wrtarga.c)
-set_property(TARGET djpeg PROPERTY COMPILE_FLAGS
-  "-DBMP_SUPPORTED -DGIF_SUPPORTED -DPPM_SUPPORTED -DTARGA_SUPPORTED -DUSE_SETMODE")
+  ../wrgif.c ../wrppm.c ${DJPEG_BMP_SOURCES})
+set_property(TARGET djpeg PROPERTY COMPILE_FLAGS ${COMPILE_FLAGS})
 target_link_libraries(djpeg jpeg)
 
 add_executable(jpegtran ../jpegtran.c ../cdjpeg.c ../rdswitch.c ../transupp.c)
diff --git a/testimages/testorig12.jpg b/testimages/testorig12.jpg
new file mode 100644
index 0000000..861aff9
--- /dev/null
+++ b/testimages/testorig12.jpg
Binary files differ
diff --git a/win/jconfig.h.in b/win/jconfig.h.in
index 6865faf..8783900 100644
--- a/win/jconfig.h.in
+++ b/win/jconfig.h.in
@@ -7,6 +7,17 @@
 #cmakedefine D_ARITH_CODING_SUPPORTED
 #cmakedefine MEM_SRCDST_SUPPORTED
 
+/*
+ * Define BITS_IN_JSAMPLE as either
+ *   8   for 8-bit sample values (the usual setting)
+ *   12  for 12-bit sample values
+ * Only 8 and 12 are legal data precisions for lossy JPEG according to the
+ * JPEG standard, and the IJG code does not support anything else!
+ * We do not support run-time selection of data precision, sorry.
+ */
+
+#define BITS_IN_JSAMPLE  @BITS_IN_JSAMPLE@      /* use 8 or 12 */
+
 #define HAVE_UNSIGNED_CHAR
 #define HAVE_UNSIGNED_SHORT
 /* #define void char */