Skip to content

Commit

Permalink
fix: correct readme and clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
duniul committed May 13, 2024
1 parent 34b6552 commit 17c47c2
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/quiet-ants-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'pixel-scale': patch
---

Code clean up and corrected readme.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ art!
- [`scalePixels(imageData, to, options)`](#scalepixelsimagedata-to-options)
- [`getPixelScale(imageData, options)`](#getpixelscaleimagedata-options)
- [`multiplyPixelScale(imageData, by, options)`](#multiplypixelscaleimagedata-by-options)
- [`multiplyPixelScale(imageData, by, options)`](#multiplypixelscaleimagedata-by-options-1)
- [`dividePixelScale(imageData, by, options)`](#divideixelscaleimagedata-by-options)
- [About](#about)
- [ImageData](#imagedata)
- [Pixel scale](#pixel-scale-1)
Expand Down Expand Up @@ -139,9 +139,10 @@ const doubledImageData = multiplyPixelScale(imageData, 2);
const tenfoldImageData = multiplyPixelScale(imageData, 10, { from: 5 });
```

### `multiplyPixelScale(imageData, by, options)`
### `dividePixelScale(imageData, by, options)`

Similar to `scalePixels`, but downscales the image by the specified amount of times instead of to a specific scale. Detects the current scale of the image if no `options.from` value is provided.

**Parameters:**

- `imageData` (ImageData instance) - The ImageData instance to downscale.
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/imageData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ export function isValidImageData(imageData: ImageDataLike): imageData is ImageDa
imageData &&
typeof imageData.width === 'number' &&
typeof imageData.height === 'number' &&
imageData?.data?.length === imageData.width * imageData.height * 4
imageData.data?.length === imageData.width * imageData.height * 4
);
}
17 changes: 6 additions & 11 deletions src/scalePixels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,10 @@ export function scalePixels(
// create one row per pixel scale
for (let rowCount = 0; rowCount < newScale; rowCount++) {
// loop through each scaled pixel on the row
for (let pixelStart = rowStart; pixelStart < rowEnd; pixelStart += currentScale * 4) {
for (let pStart = rowStart; pStart < rowEnd; pStart += currentScale * 4) {
// add the corresponding colors according to the new scale
for (let colCount = 0; colCount < newScale; colCount++) {
newData.push(data[pixelStart]);
newData.push(data[pixelStart + 1]);
newData.push(data[pixelStart + 2]);
newData.push(data[pixelStart + 3]);
newData.push(data[pStart], data[pStart + 1], data[pStart + 2], data[pStart + 3]);
}
}
}
Expand All @@ -40,20 +37,18 @@ export function scalePixels(

export function multiplyPixelScale(
imageData: ImageData,
by: number,
multiplier: number,
options?: ScalePixelsOptions | undefined | null
) {
const scale = getPixelScale(imageData, options);
const to = scale * by;
return scalePixels(imageData, to, options);
return scalePixels(imageData, scale * multiplier, options);
}

export function dividePixelScale(
imageData: ImageData,
by: number,
divider: number,
options?: ScalePixelsOptions | undefined | null
) {
const scale = getPixelScale(imageData, options);
const to = scale / by;
return scalePixels(imageData, to, options);
return scalePixels(imageData, scale / divider, options);
}

0 comments on commit 17c47c2

Please sign in to comment.