{"id":13548936,"url":"https://github.com/devongovett/pixel-stream","last_synced_at":"2025-06-11T21:05:27.309Z","repository":{"id":23213639,"uuid":"26570656","full_name":"devongovett/pixel-stream","owner":"devongovett","description":"A base transform stream class for image pixel data","archived":false,"fork":false,"pushed_at":"2014-11-19T18:58:01.000Z","size":148,"stargazers_count":18,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-18T12:19:13.393Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/devongovett.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-11-13T04:28:45.000Z","updated_at":"2023-04-12T07:27:51.000Z","dependencies_parsed_at":"2022-09-03T17:31:52.776Z","dependency_job_id":null,"html_url":"https://github.com/devongovett/pixel-stream","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/devongovett/pixel-stream","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devongovett%2Fpixel-stream","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devongovett%2Fpixel-stream/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devongovett%2Fpixel-stream/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devongovett%2Fpixel-stream/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/devongovett","download_url":"https://codeload.github.com/devongovett/pixel-stream/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devongovett%2Fpixel-stream/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259340515,"owners_count":22843007,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T12:01:16.178Z","updated_at":"2025-06-11T21:05:27.290Z","avatar_url":"https://github.com/devongovett.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# pixel-stream\n\n`PixelStream` is a base transform stream class for image pixel data.\nIt propagates image metadata such as size and color space between piped streams,\nand makes working with images of multiple frames (e.g. animated GIFs!) much easier.\n\n## Installation\n\n    npm install pixel-stream\n\n## API for Consumers\n\n`PixelStream` is a Node [transform stream](http://nodejs.org/api/stream.html#stream_class_stream_transform).\nYou can write data to it manually, or pipe data to it from another stream.  You can pipe more than one\nframe of data (as in an animated image), and the `PixelStream` will handle this properly.\n\n### `PixelStream(width = 0, height = 0, options = {})`\n\nThe constructor for a `PixelStream` accepts three optional arguments: `width`, `height`, and an \nobject for other options.  If you are not piping another stream into this one that has these\nproperties, `width` and `height` are required.  One additional option handled by the `PixelStream`\nbase class is `colorSpace`, described below, which is set to 'rgb' by default. Other options can\nbe handled by subclasses.\n\n### `format`\n\nAn object describing characteristics about the image, such as its `width`, `height`,\n`colorSpace` (e.g. rgb, rgba, gray, cmyk, etc.), and other properties.\n\n### `addFrame(frame)`\n\nThis method adds a frame metadata object describing the characteristics of a frame in an\nanimated image. This does not include the actual pixel data for the frame, which is written\nthrough the stream in the usual way. It just describes characteristics such as frame size, etc.\nThis frame object can be used by `PixelStream` subclasses, such as encoders, and is \npassed on to `PixelStream`s further down the pipes by emitting `frame` events (described below).\n\n### `'format'` event\n\nIf this event is emitted by a source stream (e.g. image decoder), which is then piped to a \n`PixelStream`, the `PixelStream` will use this opportunity to learn about the above image\ncharacteristics from the source stream automatically. A format object, as described above,\nshould be passed as an argument to the event.\n\n```javascript\nfs.createReadStream('in.png')\n  .pipe(new PNGDecoder)\n  .pipe(new MyPixelStream)\n```\n\nIn the above example, the instance of `MyPixelStream` is not initialized with a `width`, `height`,\nor `colorSpace` since it learns those characteristics automatically when the `PNGDecoder` emits\na `'format'` event.\n\n### `'frame'` event\n\nIf this event is emitted by a source stream (e.g. image decoder), which is then piped to a \n`PixelStream`, the frame object is added to the frame queue through the `addFrame` method \ndescribed above.\n\n## API for Subclasses\n\nThe following methods can be implemented by subclasses to provide useful behavior. \nOnly `_writePixels` is required.\n\n### `_start(callback)`\n\nThis method is called at the start of the stream, before any data has been passed to `_writePixels`.\nYou should call the provided callback when you are done.\n\n### `_startFrame(frame, callback)`\n\nThis method is called at the start of each frame, before any data for this frame has been passed to\n`_writePixels`. It is passed a frame metadata object, which either came from a call to `addFrame` or\npiped from another stream (described above).  You should call the provided callback when you are done.\n\n### `_writePixels(data, callback)`\n\nThis method is called with the actual pixel data for a frame (not necessarily all at once).\nYou should call the provided callback when you are done. This is the only method that you MUST implement.\n\n### `_endFrame(callback)`\n\nThis method is called at the end of each frame, after all data for the frame has been passed to `_writePixels`.\nYou should call the provided callback when you are done.\n\n### `_end(callback)`\n\nThis method is called at the end of the entire data stream for all frames.\nYou should call the provided callback when you are done.\n\n## Example\n\n`PixelStream` is an abstract class, which means it doesn't do much by itself. You need to extend it\nwith your functionality to make it useful.\n\nThe following example converts RGB images to grayscale.\n\n```javascript\nvar PixelStream = require('pixel-stream');\nvar inherits = require('util').inherits;\n\nfunction MyPixelStream() {\n  PixelStream.apply(this, arguments);\n}\n\ninherits(MyPixelStream, PixelStream);\n\nMyPixelStream.prototype._writePixels = function(data, done) {\n  if (this.colorSpace !== 'rgb')\n    return done(new Error('Only supports rgb input'));\n  \n  var res = new Buffer(data.length / 3);\n  var i = 0, j = 0;\n  \n  while (i \u003c data.length) {\n    res[j++] = 0.2126 * data[i++] + \n               0.7152 * data[i++] + \n               0.0722 * data[i++];\n  }\n  \n  this.push(res);\n  done();\n};\n```\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevongovett%2Fpixel-stream","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevongovett%2Fpixel-stream","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevongovett%2Fpixel-stream/lists"}