diff --git a/CMakeLists.txt b/CMakeLists.txt index 3ca212d..c87b3c7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,6 +31,7 @@ add_executable( "${BIN_TARGET}" ${source} ) set( CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH}" "${CMAKE_SOURCE_DIR}/modules" ) find_package( PNG REQUIRED ) find_package( JPEG REQUIRED ) +find_package( WebP REQUIRED ) find_package( XRandr REQUIRED ) find_package( XRender REQUIRED ) find_package( XFixes REQUIRED ) @@ -54,7 +55,8 @@ include_directories( ${XRANDR_INCLUDE_DIR} ${JPEG_INCLUDE_DIR} ${XRANDR_INCLUDE_DIR} ${XRENDER_INCLUDE_DIR} - ${PNG_INCLUDE_DIRS} ) + ${PNG_INCLUDE_DIRS} + ${WEBP_INCLUDE_DIR} ) # Libraries target_link_libraries( ${BIN_TARGET} @@ -66,7 +68,8 @@ target_link_libraries( ${BIN_TARGET} ${XRANDR_LIBRARY} ${JPEG_LIBRARIES} ${XRENDER_LIBRARY} - ${SLOP_LIBRARIES} ) + ${SLOP_LIBRARIES} + ${WEBP_LIBRARY} ) if( ${CMAKE_VERSION} VERSION_LESS 3.7 ) message( WARNING "CMake version is below 3.7, CMake version >= 3.7 is required for unicode support." ) diff --git a/maim.1 b/maim.1 index 7268bb8..7ac89e2 100644 --- a/maim.1 +++ b/maim.1 @@ -6,7 +6,7 @@ maim \- make image .SH SYNOPSIS maim [OPTIONS] [FILEPATH] .SH DESCRIPTION -maim (make image) is an utility that takes a screenshot of your desktop, and encodes a png, jpg, or bmp image of it. By default it outputs the encoded image data directly to standard output. +maim (make image) is an utility that takes a screenshot of your desktop, and encodes a png, jpg, bmp or webp image of it. By default it outputs the encoded image data directly to standard output. .SH OPTIONS .TP .BR \-h ", " \-\-help @@ -19,7 +19,7 @@ Print version and exit. Sets the xdisplay to use. .TP .BR \-f ", " \-\-format=\fISTRING\fR -Sets the desired output format, by default maim will attempt to determine the desired output format automatically from the output file. If that fails it defaults to a lossless png format. Currently only supports `png`, `jpg`, and `bmp`. +Sets the desired output format, by default maim will attempt to determine the desired output format automatically from the output file. If that fails it defaults to a lossless png format. Currently only supports `png`, `jpg`, `bmp`, and `webp`. .TP .BR \-i ", " \-\-window=\fIWINDOW\fR By default, maim captures the root window. This parameter overrides this and sets the desired window to capture. Allows for an integer, hex, or `root` for input. @@ -40,7 +40,7 @@ Sets the time in seconds to wait before taking a screenshot. Prints a simple mes By default maim super-imposes the cursor onto the image, you can disable that behavior with this flag. .TP .BR \-m ", " \-\-quality -An integer from 1 to 10 that determines the compression quality. 1 is the highest (and lossiest) compression available for the provided format. For example a setting of `1` with png (a lossless format) would increase filesize and decrease decoding time. While a setting of `1` on a jpeg would create a pixel mush. No effect on bmp images. +An integer from 1 to 10 that determines the compression quality. For lossy formats (jpg and webp), lower settings will produce smaller files with lower quality, while higher settings will increase quality at the cost of higher file size. A quality of 10 is lossless for webp. For png, lower settings will compress faster and produce larger files, while higher settings will compress slower, but produce smaller files. No effect on bmp images. .TP .BR \-s ", " \-\-select Enables an interactive selection mode where you may select the desired region or window before a screenshot is captured. Uses the settings below to determine the visuals and settings of slop. diff --git a/modules/FindWebP.cmake b/modules/FindWebP.cmake new file mode 100644 index 0000000..7153b7c --- /dev/null +++ b/modules/FindWebP.cmake @@ -0,0 +1,25 @@ +# - Find WebP +# Find the WebP libraries +# +# This module defines the following variables: +# WEBP_FOUND - 1 if WEBP_INCLUDE_DIR & WEBP_LIBRARY are found, 0 otherwise +# WEBP_INCLUDE_DIR - where to find webp/encode.h, etc. +# WEBP_LIBRARY - the libwebp library + +find_path( WEBP_INCLUDE_DIR + NAMES webp/encode.h + PATH_SUFFIXES /usr/include /include + DOC "The libwebp include directory" ) + +find_library( WEBP_LIBRARY + NAMES libwebp.so + PATHS /usr/lib /lib + DOC "The libwebp library" ) + +if( WEBP_INCLUDE_DIR AND WEBP_LIBRARY ) + set( WEBP_FOUND 1 ) +else() + set( WEBP_FOUND 0 ) +endif() + +mark_as_advanced( WEBP_INCLUDE_DIR WEBP_LIBRARY ) diff --git a/src/image.cpp b/src/image.cpp index 63f4bab..19aae2e 100644 --- a/src/image.cpp +++ b/src/image.cpp @@ -271,6 +271,32 @@ void ARGBImage::writeBMP( std::ostream& streamout ) { delete[] imageData; } +void ARGBImage::writeWEBP( std::ostream& streamout, int quality ) { + // assume 4 channels + if (channels != 4) { + throw new std::runtime_error("WebP tried to save image with more than 4 channels"); + } + + size_t size; + uint8_t* out; + if (quality == 10) { + // encode lossless at highest quality + size = WebPEncodeLosslessRGBA(data, width, height, width * 4, &out); + } + else { + // otherwise, encode lossy + size = WebPEncodeRGBA(data, width, height, width * 4, quality * 10.0f, &out); + } + + if (size == 0) { + throw new std::runtime_error("Failed to encode webp image"); + } + else { + streamout.write((const char*)out, size); + WebPFree(out); + } +} + bool ARGBImage::intersect( XRRCrtcInfo* a, glm::vec4 b ) { if (a->x < b.x + b.z && a->x + a->width > b.x && diff --git a/src/image.hpp b/src/image.hpp index a6bfd64..0b7af89 100644 --- a/src/image.hpp +++ b/src/image.hpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -91,6 +92,7 @@ class ARGBImage { void writePNG( std::ostream& streamout, int quality ); void writeJPEG( std::ostream& streamout, int quality ); void writeBMP( std::ostream& streamout ); + void writeWEBP( std::ostream& streamout, int quality ); }; #endif diff --git a/src/main.cpp b/src/main.cpp index 733826d..f3d2092 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -185,7 +185,7 @@ MaimOptions* getMaimOptions( cxxopts::Options& options, X11* x11 ) { foo->formatGiven = options.count("format") > 0; if ( foo->formatGiven ) { foo->format = options["format"].as(); - if ( foo->format != "png" && foo->format != "jpg" && foo->format != "jpeg" && foo->format != "bmp") { + if ( foo->format != "png" && foo->format != "jpg" && foo->format != "jpeg" && foo->format != "bmp" && foo->format != "webp" ) { throw new std::invalid_argument("Unknown format type: `" + foo->format + "`, only `png`, `jpg`, or `bmp` is allowed." ); } } @@ -272,8 +272,8 @@ SYNOPSIS DESCRIPTION maim (make image) is an utility that takes a screenshot of your desktop, - and encodes a png or jpg image of it. By default it outputs the encoded - image data directly to standard output. + and encodes a png, jpg, bmp or webp image of it. By default it outputs + the encoded image data directly to standard output. OPTIONS -h, --help @@ -289,7 +289,7 @@ OPTIONS Sets the desired output format, by default maim will attempt to determine the desired output format automatically from the output file. If that fails it defaults to a lossless png format. Cur‐ - rently only supports `png` or `jpg`. + rently supports `png`, `jpg`, `bmp` and `webp`. -i, --window=INT Sets the desired window to capture, defaults to the root window. @@ -310,11 +310,14 @@ OPTIONS disable that behavior with this flag. -m, --quality - An integer from 1 to 10 that determines the compression quality. 1 - is the highest (and lossiest) compression available for the pro‐ - vided format. For example a setting of `1` with png (a lossless - format) would increase filesize and speed up encoding dramatical- - ly. While a setting of `1` on a jpeg would create a pixel mush. + An integer from 1 to 10 that determines the compression quality. + For lossy formats (jpg and webp), lower settings will produce + smaller files with lower quality, while higher settings will inc- + rease quality at the cost of higher file size. A quality of 10 is + lossless for webp. + For png, lower settings will compress faster and produce larger + files, while higher settings will compress slower, but produce + smaller files. No effect on bmp images. -s, --select Enables an interactive selection mode where you may select the @@ -408,14 +411,14 @@ int app( int argc, char** argv ) { ("h,help", "Print help and exit.") ("v,version", "Print version and exit.") ("x,xdisplay", "Sets the xdisplay to use", cxxopts::value()) - ("f,format", "Sets the desired output format, by default maim will attempt to determine the desired output format automatically from the output file. If that fails it defaults to a lossless png format. Supports `png`, `jpg`, and `bmp`.", cxxopts::value()) + ("f,format", "Sets the desired output format, by default maim will attempt to determine the desired output format automatically from the output file. If that fails it defaults to a lossless png format. Supports `png`, `jpg`, `bmp` and `webp`.", cxxopts::value()) ("i,window", "Sets the desired window to capture, defaults to the root window. Allows for an integer, hex, or `root` for input.", cxxopts::value()) ("g,geometry", "Sets the region to capture, uses local coordinates from the given window. So -g10x30-5+0 would represent the rectangle wxh+x+y where w=10, h=30, x=-5, and y=0. x and y are the upper left location of this rectangle.", cxxopts::value()) ("w,parent", "By default, maim assumes the --geometry values are in respect to the provided --window (or root if not provided). This parameter overrides this behavior by making the geometry be in respect to whatever window you provide to --parent. Allows for an integer, hex, or `root` for input.", cxxopts::value()) ("B,capturebackground", "By default, when capturing a window, maim will ignore anything beneath the specified window. This parameter overrides this and also captures elements underneath the window.") ("d,delay", "Sets the time in seconds to wait before taking a screenshot. Prints a simple message to show how many seconds are left before a screenshot is taken. See --quiet for muting this message.", cxxopts::value()->implicit_value("5")) ("u,hidecursor", "By default maim super-imposes the cursor onto the image, you can disable that behavior with this flag.") - ("m,quality", "An integer from 1 to 10 that determines the compression quality. 1 is the highest (and lossiest) compression available for the provided format. For example a setting of `1` with png (a loss‐ less format) would increase filesize and decrease decoding time. While a setting of `1` on a jpeg would create a pixel mush. No effect on bmp images.", cxxopts::value()) + ("m,quality", "An integer from 1 to 10 that determines the compression quality. For lossy formats (jpg and webp), lower settings will produce smaller files with lower quality, while higher settings will increase quality at the cost of higher file size. A quality of 10 is lossless for webp. For png, lower settings will compress faster and produce larger files, while higher settings will compress slower, but produce smaller files. No effect on bmp images.", cxxopts::value()) ("s,select", "Enables an interactive selection mode where you may select the desired region or window before a screenshot is captured. Uses the settings below to determine the visuals and settings of slop.") ("b,bordersize", "Sets the selection rectangle's thickness.", cxxopts::value()) ("p,padding", "Sets the padding size for the selection, this can be negative.", cxxopts::value()) @@ -473,8 +476,8 @@ int app( int argc, char** argv ) { if ( !maimOptions->formatGiven && maimOptions->savepathGiven && maimOptions->savepath.find_last_of(".") != std::string::npos ) { maimOptions->format = maimOptions->savepath.substr(maimOptions->savepath.find_last_of(".")+1); - if ( maimOptions->format != "png" && maimOptions->format != "jpg" && maimOptions->format != "jpeg" && maimOptions->format != "bmp") { - throw new std::invalid_argument("Unknown format type: `" + maimOptions->format + "`, only `png`, `jpg`, or `bmp` is allowed." ); + if ( maimOptions->format != "png" && maimOptions->format != "jpg" && maimOptions->format != "jpeg" && maimOptions->format != "bmp" && maimOptions->format != "webp") { + throw new std::invalid_argument("Unknown format type: `" + maimOptions->format + "`, only `png`, `jpg`, `bmp` or `webp` is allowed." ); } } if ( !maimOptions->windowGiven ) { @@ -557,44 +560,37 @@ int app( int argc, char** argv ) { glm::ivec2 imageloc; // Snapshot the image XImage* image = x11->getImage( selection.id, px, py, selection.w, selection.h, imageloc); - if ( maimOptions->format == "png" ) { - // Convert it to an ARGB format, clipping it to the selection. - ARGBImage convert(image, imageloc, glm::vec4(px, py, selection.w, selection.h), 4, x11 ); - if ( !maimOptions->hideCursor ) { - convert.blendCursor( x11 ); - } - // Mask it if we're taking a picture of root - if ( selection.id == x11->root ) { - convert.mask(x11); - } - // Then output it in the desired format. + + int num_channels; + if ( maimOptions->format == "png" || maimOptions->format == "webp" ) { + // Convert it to an ARGB format, clipping it to the selection + num_channels = 4; + } else { + // Otherwise (jpeg/bmp), convert to RGB, also clipping it to the selection + num_channels = 3; + } + + ARGBImage convert(image, imageloc, glm::vec4(px, py, selection.w, selection.h), num_channels, x11 ); + + if ( !maimOptions->hideCursor ) { + convert.blendCursor( x11 ); + } + // Mask it if we're taking a picture of root + if ( selection.id == x11->root ) { + convert.mask(x11); + } + + // then output it into into the desired format + if (maimOptions->format == "png") { convert.writePNG(*out, maimOptions->quality ); } else if ( maimOptions->format == "jpg" || maimOptions->format == "jpeg" ) { - // Convert it to a RGB format, clipping it to the selection. - ARGBImage convert(image, imageloc, glm::vec4(px, py, selection.w, selection.h), 3, x11 ); - if ( !maimOptions->hideCursor ) { - convert.blendCursor( x11 ); - } - // Mask it if we're taking a picture of root - if ( selection.id == x11->root ) { - convert.mask(x11); - } - // Then output it in the desired format. convert.writeJPEG(*out, maimOptions->quality ); - } else if (maimOptions->format == "bmp") { - // Convert it to a RGB format, clipping it to the selection. - ARGBImage convert(image, imageloc, glm::vec4(px, py, selection.w, selection.h), 3, x11 ); - if ( !maimOptions->hideCursor ) { - convert.blendCursor( x11 ); - } - // Mask it if we're taking a picture of root - if ( selection.id == x11->root ) { - convert.mask(x11); - } - // Then output it in the desired format. + } else if ( maimOptions->format == "bmp" ) { convert.writeBMP(*out); - + } else if ( maimOptions->format == "webp" ) { + convert.writeWEBP(*out, maimOptions->quality); } + XDestroyImage( image ); if ( maimOptions->savepathGiven ) {