Skip to content

Instantly share code, notes, and snippets.

@symisc
Last active April 1, 2018 06:19
Show Gist options
  • Save symisc/eb92d8eae1a57a942ff253ef11ac38e3 to your computer and use it in GitHub Desktop.
Save symisc/eb92d8eae1a57a942ff253ef11ac38e3 to your computer and use it in GitHub Desktop.
Extract a region (crop) from a given image - https://sod.pixlab.io
/*
* Programming introduction with the SOD Embedded Image Processing API.
* Copyright (C) PixLab | Symisc Systems, https://sod.pixlab.io
*/
/*
* Compile this file together with the SOD embedded source code to generate
* the executable. For example:
*
* gcc sod.c crop_image.c -lm -Ofast -march=native -Wall -std=c99 -o sod_img_proc
*
* Under Microsoft Visual Studio (>= 2015), just drop `sod.c` and its accompanying
* header files on your source tree and you're done. If you have any trouble
* integrating SOD in your project, please submit a support request at:
* https://sod.pixlab.io/support.html
*/
/*
* This simple program is a quick introduction on how to embed and start
* experimenting with SOD without having to do a lot of tedious
* reading and configuration.
*
* Make sure you have the latest release of SOD from:
* https://pixlab.io/downloads
* The SOD Embedded C/C++ documentation is available at:
* https://sod.pixlab.io/api.html
*/
#include <stdio.h>
#include "sod.h"
/*
* Extract a region (crop) from a given image.
*/
int main(int argc, char *argv[])
{
/* Input image (pass a path or use the test image shipped with the samples ZIP archive) */
const char *zInput = argc > 1 ? argv[1] : "./flower.jpg";
/* Processed output image path */
const char *zOut = argc > 2 ? argv[2] : "./out_crop.png";
/* Load the input image in full color */
sod_img imgIn = sod_img_load_from_file(zInput, SOD_IMG_COLOR /* full color channels */);
if (imgIn.data == 0) {
/* Invalid path, unsupported format, memory failure, etc. */
puts("Cannot load input image..exiting");
return 0;
}
/* Crop offset: Target region coordinates, height & width */
int x = imgIn.w / 2;
int y = imgIn.h / 2;
int width = 128;
int height = 128;
/* Crop */
sod_img crop = sod_crop_image(imgIn, x, y, width, height);
/* Save the cropped region to the specified path */
sod_img_save_as_png(crop, zOut);
/* Cleanup */
sod_free_image(imgIn);
sod_free_image(crop);
return 0;
}
@symisc
Copy link
Author

symisc commented Apr 1, 2018

Extract a region (crop) from a given image. The interface is documented at https://sod.pixlab.io/c_api/sod_crop_image.html

SOD Embedded Homepage: https://sod.pixlab.io
SOD C/C++ API documentation: https://sod.pixlab.io/api.html
Getting Started with SOD Embedded: https://sod.pixlab.io/intro.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment