Code Monkey home page Code Monkey logo

jgpixeldata's Introduction

JGPixelData

JGPixelData simplifies the process of editing the raw pixel RGBA values of a UIImage. Simply create a JGPixelData object and directly modify the RGBA values of each pixel independently. Furthermore, looping over the pixels is effortless!

First, we create an instance of JGPixelData from a given UIImage.

JGPixelData *pixelData = [JGPixelData pixelDataWithImage:image];

Then, we use a block function to loop over all the pixels in an image and swap the values of their red and blue color components.

[pixelData processPixelsWithBlock:^(JGPixel *pixel, int x, int y) {
    UInt8 temp = pixel->red;
    if (x > y) {
        pixel->red = pixel->blue;
        pixel->blue = temp;
    }
    else{
        pixel->red = pixel->green;
        pixel->green = temp;
    }
}];

Once you make the desired changes to the individual pixels, simply use the image property of a JGPixelData instance to get the newly modified image.

UIImage *editedImage = pixelData.image;

It's as easy as that!

Advanced Looping

JGPixelData provides a convenient pointer to a start and a stop pixel that can be used in iteration. In the following example, we will invert the color of our image. Because we don't care about the x or y value of each pixel, we can forget about the block and just iterate using a for loop.

for (JGPixel *pixel = pixelData.start; pixel < pixelData.stop; pixel++) {
    pixel->red   = JGPixelColorMax - pixel->red;
    pixel->green = JGPixelColorMax - pixel->green;
    pixel->blue  = JGPixelColorMax - pixel->blue;
}

Notice that we can create even more complicated loops since we have direct access to the pixel. For example, we can change a pixel based off its surrounding pixels. There is an advanced example of this in the example project.

Individual Pixel Access

Individual pixels can be accessed using the method pixelWithX:y:.

Other

For convenience, JGPixelData provides a width and height method that returns the pixel width and pixel height, respectively, or the pixel data.

jgpixeldata's People

Contributors

jadengeller avatar

Stargazers

Damiaan Dufaux avatar Nonecy avatar  avatar Henry Hardy avatar General Westerfeld avatar Cameron Ehrlich avatar Alexander Böhn avatar Robert S Mozayeni avatar  avatar Kenneth Ackerson avatar Furkan Mustafa avatar Nicolas Gomollon avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.